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

x
  • 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.

<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"
0
Planmaster
Asked:
Planmaster
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']">
  <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

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