i have xml file and xslt transformation below:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet version="1.0" type="text/xml" href="pets.xsl" ?>
<pets>
<pet name="Jace" type="dog" />
<pet name="Babson" type="" />
<pet name="Oakley" type="cat" />
<pet name="Tabby" type="dog" />
</pets>
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:key name="pets-key" match="pet" use="type" />
<xsl:template match="/" >
<html>
<head><title></title></head>
<body>
<xsl:for-each select="key('pets-key', '' )" >
<xsl:value-of select="@name" />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
How can i select all pets which types not empty using key function?
Two points to note:
You need a set difference to select all pets whose type is non-empty, and still use the key() function. The general recipe for set difference in XPATH 1.0 is...
$node-set1[count(. | $node-set2) != count($node-set2)]
Putting it altogether, a correct but inefficent XSLT 1.0 style-sheet to use the key() and list all pets with not empty type is...
This yields output...
Having said that, the question does not really fit as a good excersize in using keys. In real life, if you wanted to achieve this outcome, a better, more efficient XSLT 1.0 solution would be ...