How do I return attributes by their names in an XSLT file.

Dear Experts,

I'm trying to transform my XML file and I'm having an issue with attributes.
I have a sample XML file (part of which is displayed below.

<Texts>
<Text name="tenuretype">Freehold</Text>
<Text name="shortdescription">Weekend retreat in polo country</Text>
</Texts>

Using the following snippet of code, I have been able to get all of the values of the Text attributes ("tenuretype and shortdescription).

<xsl:for-each select="Texts/Text">
            <tr>          
                <xsl:value-of select="@name"/>
            <xsl:value-of select="."/>
            </tr>
        </xsl:for-each>

How can I change this to only return values for the attribute called "shortdescription"
PlanmasterAsked:
Who is Participating?
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You just need to add a little bit to the select criteria in the first line, like below...

<xsl:for-each select="Texts/Text[@name='shortdescription']">
  <tr>          
    <xsl:value-of select="@name"/>
    <xsl:value-of select="."/> 
  </tr>
</xsl:for-each>

Open in new window


Note the [@name='shortdescription'] in the select criteria. It is basically saying to restrict the result of the select to those Text elements (within Texts elements) that have a 'name' attribute that equals 'shortdescription'. Check out an XPath tutorial for more ideas on what/how you can select things.

Hope this helps...
0
 
PlanmasterAuthor Commented:
Great solution! Thanks a lot. Worked like a charm.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.