[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1459
  • Last Modified:

XSLT - select multiple attributes

Hello experts,

I'm new to XSLT and can't solve something that looks trivial to me.

I have an XML file like this:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="books.xslt"?>
<bookstore>
      <book category="COOKING">
            <title lang="en">Everyday Italian</title>
            <author>Giada De Laurentiis</author>
            <year>2005</year>
            <price>30.00</price>
      </book>
      <book category="CHILDREN">
            <title lang="en">Harry Potter</title>
            <author>J K. Rowling</author>
            <year>2005</year>
            <price>29.99</price>
      </book>
      <book category="WEB">
            <title lang="en">XQuery Kick Start</title>
            <author>James McGovern</author>
            <author>Per Bothner</author>
            <author>Kurt Cagle</author>
            <author>James Linn</author>
            <author>Vaidyanathan Nagarajan</author>
            <year>2003</year>
            <price>49.99</price>
      </book>
</bookstore>

and XSLT like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
      <xsl:output method="html" indent="yes"/>

      <xsl:template match="/">
            <xsl:value-of select="/bookstore/book/@category" />
      </xsl:template>
</xsl:stylesheet>

I'm trying to print all categories. It should be like this:
COOKING
CHILDREN
WEB

but I'm seeing only the first item.

Please help.
0
sasapopovic
Asked:
sasapopovic
  • 4
  • 2
1 Solution
 
Geert BormansCommented:
<xsl:value-of select="/bookstore/book/@category" />

should be

<xsl:for-each select="/bookstore/book">
   <xsl:value-of select="@category" />
</xsl:for-each>
0
 
sasapopovicAuthor Commented:
Hi Gertone,

Thank you so far. The thing is that I need those attrbiutes' values in a xsl variable. That is why I didn't use xsl:for-each.

Is there a way to have the attributes printed into a variable somehow? I need the atributes' values in a variable to be able to use them in a JavaScript function for some randomization, etc.
0
 
Geert BormansCommented:
well, what do you want the content of the variable to be?
You can wrap the for-each inside a variable (semi-colon seperated in this example)

<xsl:variable name="categories>
 <xsl:for-each select="/bookstore/book">
   <xsl:value-of select="@category" />
   <xsl:if test="not(position() = last())">
     <xsl:text>;</xsl:text>
   </xsl:if>
 </xsl:for-each>
</xsl:variable>
0
Independent Software Vendors: 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!

 
Geert BormansCommented:
This can be done outside the templates if you need a globaly accessible variable
0
 
sasapopovicAuthor Commented:
This will work, thx!
0
 
Geert BormansCommented:
welcome
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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