Reputation: 1
<?xml version="1.0" encoding="UTF-8"?>
<CATALOG YEAR="1988">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<VINYL>
<TITLE>Seventh Son of a Seventh Son</TITLE>
<ARTIST>Iron Maiden</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>EMI</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1988</YEAR>
</VINYL>
</CATALOG>
In the above xml file, I want to check that the year of the Catalog is 1987, 1988, or 1989. However, when I use my search below
//CATALOG[matches(@YEAR, "198[789]")]
I get a match on both the Catalog year and the year of Seventh Son of a Seventh Son. How can I search only the root node? I tried searching with "self" or "." with no success. Thanks
Upvotes: 0
Views: 205
Reputation: 29022
Your expression is a correct XPath-2.0 expression. So a valid XSLT-2.0 stylesheet could look like this:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//CATALOG[matches(@YEAR, '198[789]')]">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
This stylesheet matches and copies all root elements (probably one) with the given condition. Your expression is correct.
And if this doesn't work as desired, just use
/CATALOG[matches(@YEAR, '198[789]')]
This will (for sure) select the root element with the given properties.
Upvotes: 1