using xslt params to search for elements
-
I'd like to pass a parameter into an XSLT to search for a specific element within an XML document. My document has this form: I want to be able to pass in a parameter into my XSLT and search for a particular folder - let's say id="4". This is how I've been approaching it: But I get this error: System.Xml.Xsl.XsltException: '/library//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: /library//folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function. What might be a better or more correct way to approach this? Michael Hodnick
-
I'd like to pass a parameter into an XSLT to search for a specific element within an XML document. My document has this form: I want to be able to pass in a parameter into my XSLT and search for a particular folder - let's say id="4". This is how I've been approaching it: But I get this error: System.Xml.Xsl.XsltException: '/library//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: /library//folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function. What might be a better or more correct way to approach this? Michael Hodnick
Hi Michael. At first glance, I think the syntax
<xsl:template match="/library//folder[@id=$folderID]">
is wrong. You have two slashes between library and folder - did you want instead something like this?
<xsl:template match="//folder[@id=$folderID]">
to match a
folder
node regardless of where it is in the heirarchy? -
Hi Michael. At first glance, I think the syntax
<xsl:template match="/library//folder[@id=$folderID]">
is wrong. You have two slashes between library and folder - did you want instead something like this?
<xsl:template match="//folder[@id=$folderID]">
to match a
folder
node regardless of where it is in the heirarchy?I've also tried what you are suggesting, but still receive the following error:
'//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: //folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function.
Michael Hodnick www.kindohm.com blogs.kindohm.com -
I'd like to pass a parameter into an XSLT to search for a specific element within an XML document. My document has this form: I want to be able to pass in a parameter into my XSLT and search for a particular folder - let's say id="4". This is how I've been approaching it: But I get this error: System.Xml.Xsl.XsltException: '/library//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: /library//folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function. What might be a better or more correct way to approach this? Michael Hodnick
I've just tried your approach in xsltproc (part of libxslt) and it works fine. I presume you are passing a value for folderID into the transform engine??? I used the command 'xsltproc --param folderID 4 a.xsl a.xml' to perform the transform (the xsl and xml names are obvious :-)) This was the XSL I used (based on yours - the template selector is the same as yours)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"> <xsl:output method="xml" encoding="ISO8859-1" indent="yes" omit-xml-declaration="no"/> <xsl:param name="folderID"/> <xsl:template match="/library//folder[@id=$folderID]"> <Wib> <xsl:value-of select="count(./file)"/> </Wib> </xsl:template> </xsl:stylesheet>
Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p' -
I'd like to pass a parameter into an XSLT to search for a specific element within an XML document. My document has this form: I want to be able to pass in a parameter into my XSLT and search for a particular folder - let's say id="4". This is how I've been approaching it: But I get this error: System.Xml.Xsl.XsltException: '/library//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: /library//folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function. What might be a better or more correct way to approach this? Michael Hodnick
My previous reply was wrong - you can't use a variable reference in a template selector - see this You'll have to use something like the following instead (i.e. put the predicate on a higher-level select expression and keep the template matching rule wide):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl"><xsl:output method="xml" encoding="ISO8859-1" indent="yes" omit-xml-declaration="no"/>
<xsl:param name="folderID"/>
<xsl:template match="/">
<xsl:apply-templates select="/library//folder[@id=$folderID]"/>
</xsl:template>
<xsl:template match="/library//folder">
<Wib>
<xsl:value-of select="count(./file)"/>
</Wib>
</xsl:template>
</xsl:stylesheet>HTH Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
-
My previous reply was wrong - you can't use a variable reference in a template selector - see this You'll have to use something like the following instead (i.e. put the predicate on a higher-level select expression and keep the template matching rule wide):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl"><xsl:output method="xml" encoding="ISO8859-1" indent="yes" omit-xml-declaration="no"/>
<xsl:param name="folderID"/>
<xsl:template match="/">
<xsl:apply-templates select="/library//folder[@id=$folderID]"/>
</xsl:template>
<xsl:template match="/library//folder">
<Wib>
<xsl:value-of select="count(./file)"/>
</Wib>
</xsl:template>
</xsl:stylesheet>HTH Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
That did the trick. Thanks for the w3c link and the example. Michael Hodnick www.kindohm.com blogs.kindohm.com