DaveF
DaveF

Reputation: 173

Why is this xslt copy-of example not returning expected result

Using latest SaxonHE.

This 'copy-of' example isn't returning the defined result, What I'm getting is shown at the bottom of the page. What is the fix?

XML

<?xml version="1.0" encoding="UTF-8"?>
<library>
    <category name="dogs">
        <book>
            <name>All about dogs</name>
            <author>Someone</author>
            <isin>true</isin>
            <daysuntilreturn>0</daysuntilreturn>
        </book>
    </category>
     <category name="cats">
        <book>
            <name>All about cats</name>
            <author>Someone</author>
            <isin>false</isin>
            <daysuntilreturn>3</daysuntilreturn>
        </book>
    </category>
</library>

XSL

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <myoutput>
            <xsl:copy-of select="/*/category[@name=cats]" />
        </myoutput>
    </xsl:template>
</xsl:stylesheet>

Defined outout

<?xml version="1.0" encoding="UTF-8"?>
<myoutput>
     <category name="cats">
        <book>
            <name>All about cats</name>
            <author>Someone</author>
            <isin>false</isin>
            <daysuntilreturn>3</daysuntilreturn>
        </book>
    </category>
</myoutput>

My output:

<?xml version="1.0" encoding="UTF-8"?><myoutput/>

Upvotes: 0

Views: 67

Answers (2)

Tim C
Tim C

Reputation: 70618

The issue is that you (or rather the xml tutorial website!) have missed out apostrophes in the condition. It should be this...

<xsl:copy-of select="/*/category[@name='cats']" />

Without apostrophes like this, it will be looking for an element named "cats" in the XML, which does not exist. Adding apostrophes makes it a string literal, so it will be literally looking for the word "cats".

Upvotes: 2

imran
imran

Reputation: 461

Please use

<xsl:copy-of select="/*/category[@name='cats']" />

You have missed the apostrophes around the attribute name.

Upvotes: 0

Related Questions