2

I have an xml of the following format

<catalog>
 <cd>
  <title>CD name</title>
 </cd>
</catalog>

I can use xslt to get the element value using the following:

<xsl:template match="/">
<xsl:for-each select="catalog/cd">
<xsl:value-of select="title" />
</xsl:for-each>

But, I am trying to figure out the xsl code to read the xml in the following format:

<catalog>
 <cd title="CD name"/>
</catalog>

How do I do this? And if anyone can post some xslt tutorial link, it will be much appreciated.

Thanks in advance

2
  • Good question (+1). See my answer for a complete solution. :) Commented Sep 28, 2010 at 21:53
  • 2
    Did you mean that instead of child element you want to select an attribute? Then you need attribute axis or its abbreviated form @. Example: <xsl:value-of select="@title" /> Commented Sep 28, 2010 at 22:08

2 Answers 2

3
I have an xml of the following format

    <catalog>
     <cd>
      <title>CD name</title>
     </cd>
    </catalog>

I can use xslt to get the element value using the following:

    <xsl:template match="/">
    <xsl:for-each select="catalog/cd">
    <xsl:value-of select="title" />
    </xsl:for-each>

But, I am trying to figure out the xsl code to read the xml in the following format:


    <catalog>
     <cd title="CD name"/>
    </catalog>

How do I do this? And if anyone can post some xslt tutorial link, it will be much appreciated.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="cd">
  <xsl:value-of select="concat(@title, '&#xA;')"/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<catalog>
    <cd title="CD1 name"/>
    <cd title="CD2 name"/>
    <cd title="CD3 name"/>
</catalog>

produces the wanted result:

CD1 name
CD2 name
CD3 name

For tutorials and books see my answer to this question:

https://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589

Sign up to request clarification or add additional context in comments.

Comments

1

Another site that is useful for tutorials is: link text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.