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

Posted on 2011-05-11
Last Modified: 2012-06-21
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"
Question by:Planmaster
    LVL 35

    Accepted Solution

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

    Author Closing Comment

    Great solution! Thanks a lot. Worked like a charm.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now