Jump to content

Filter XPATH through variables


grantdb

Recommended Posts

Let's say I have something like this through a selectDATABASE[@NAME = 'john' and @DEFAULT = '1']What I want to do is assign the full filter part to a variable (as this will be coming from one source), so something like thisDATABASE[$allfiltervalues]Burt is doesn't seem to work. Should it?ThanksGrant

Link to comment
Share on other sites

Well using variables is possible with XSLT/XPath but for reasons I don't understand lots of people want to put an XPath expression as a string in a variable and then put that variable somewhere in another expression expecting to be able to construct expressions on the fly from strings. That is not something that is possible, at least not with pure XSLT/XPath.With XSLT 2.0 you can always write a function e.g.

<xsl:function name="my:predicate" as="xs:boolean">   <xsl:param name="context-node" as="node()"/>   <xsl:sequence select="$context-node/@NAME = 'John' and $context-node/@DEFAULT = '1'"/> </xsl:function>...  <xsl:for-each select="DATABASE[my:predicate(.)]">   ...  </xsl:for-each>

Link to comment
Share on other sites

Great, I can get this workingNow I want to populate this dynamically, soXML<forrendering> <customvalue>$context-node/IMPRINT = 'Collins' or $context-node/ACTIVE = '1'</customvalue></forrendering>XSL<xsl:function name="my:predicate"> <xsl:param name="context-node" as="node()"/> <xsl:variable name="valuefromfile" select="/forrendering/customvalue"/> <xsl:sequence select="$valuefromfile"/></xsl:function><xsl:for-each select="DATABASE[my:predicate(.)]"> ...</xsl:for-each>But I get this errorLeading "/" cannot select the root node of the tree containing the context itemI am presuming it is because I have a string. I am happy to be as flexible as necessary in the xml file where $valuefromfile is stored if it means creating the full xpath.Am I trying to do too much?Any help would be appreciatedThanksGrant

Well using variables is possible with XSLT/XPath but for reasons I don't understand lots of people want to put an XPath expression as a string in a variable and then put that variable somewhere in another expression expecting to be able to construct expressions on the fly from strings. That is not something that is possible, at least not with pure XSLT/XPath.With XSLT 2.0 you can always write a function e.g.
<xsl:function name="my:predicate" as="xs:boolean">   <xsl:param name="context-node" as="node()"/>   <xsl:sequence select="$context-node/@NAME = 'John' and $context-node/@DEFAULT = '1'"/> </xsl:function>...  <xsl:for-each select="DATABASE[my:predicate(.)]">   ...  </xsl:for-each>

Link to comment
Share on other sites

I am sorry but you can't construct your XSLT/XPath from strings stored elsewhere, unless you use extension functions like http://www.saxonica.com/documentation/exte...luate-node.html or http://manual.altova.com/AltovaXML/index.html?xextaltova.htm, depending on which XSLT processor you use.Thus if you really want to go that route then you could use such an extension function. A pure XSLT solution might also be possible but then only in two steps where a first stylesheet takes some input to generate a second stylesheet which you then run on some other input.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...