Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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.

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

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">
                <xsl:value-of select="@name"/>
            <xsl:value-of select="."/>

How can I change this to only return values for the attribute called "shortdescription"
1 Solution
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']">
    <xsl:value-of select="@name"/>
    <xsl:value-of select="."/> 

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...
PlanmasterAuthor Commented:
Great solution! Thanks a lot. Worked like a charm.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now