0
votes

XML:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
        <location>DL</location>
    </book>
    <book category="children">
        <title lang="es">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
        <location>UP</location>
    </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>
        <location>  DL</location>
    </book>
    <book category="web">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
        <location>dl </location>
    </book>
    <category>web</category>
</bookstore>

XPath expression:

string(/bookstore/book/@category)

The above XPath expression is returning only the value of the first attribute under book element:

cooking

Expected output:

cooking

children

web

web

I want my XPath expression to return the string value of all the attributes of book element. I referred several posts but I couldn't find onw in which I could get all the attribute values.

1
Apparently you are using an XSLT 1.0 processor, despite your xpath-2.0 tag. In XPath 2.0, your expression would produce an error because the string() function takes only a single item as its argument.michael.hor257k
Is there a way I can display all the values from the attributes and achieve the expected output? Because sometimes there is a scenario where we have to compare the attributes one by one with some other value which is also dynamic.Deepak Jain
Comparing and displaying are two very different things. In XSLT 2.0, <xsl:value-of select="/bookstore/book/@category"/> will display "cooking children web web". In XSLT 1.0, you can select all categories using the same expression and use this selection for comparison - but xsl:value-of (and other expressions, such as string()) will return only the first one of these. -- Please edit your question to clarify what exactly you want to do, incl. the expected output.michael.hor257k

1 Answers

0
votes

In XPath 2 and later you can use /bookstore/book/@category/string().