I'm not sure why my queryString variable not being read in totalRecords's select statement. What am I missing or doing wrong?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="carMake">
<xsl:value-of select="//input_payload/carMake"/>
</xsl:variable>
<xsl:variable name="queryString">Sold='False'</xsl:variable>
{
carMake: <xsl:value-of select="$carMake"/>,
querystring: <xsl:value-of select="$queryString"/>,
"totalRecords": <xsl:value-of select="count(//responseAfterTransform[$queryString])"/>,
}
</xsl:template>
</xsl:stylesheet>
CodePudding user response:
XSLT 3 example showing the use of a shadow attribute with a static parameter or of xsl:evaluate
to construct a predicate expression from a string:
<root>
<item>
<name>item 1</name>
<category>foo</category>
</item>
<item>
<name>item 2</name>
<category>bar</category>
</item>
<item>
<name>item 3</name>
<category>foo</category>
</item>
<item>
<name>item 4</name>
<category>baz</category>
</item>
</root>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="predicate" static="yes" as="xs:string" select="'category = ''foo'''"/>
<xsl:template match="/">
<shadow-attribute-example>
<xsl:sequence _select="//item[{$predicate}]"/>
</shadow-attribute-example>
<evaluate-example>
<xsl:evaluate xpath="'//item[' || $predicate || ']'" context-item="."/>
</evaluate-example>
</xsl:template>
<xsl:output indent="yes"/>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/jypqQ9e gives e.g.
<?xml version="1.0" encoding="UTF-8"?>
<shadow-attribute-example>
<item>
<name>item 1</name>
<category>foo</category>
</item>
<item>
<name>item 3</name>
<category>foo</category>
</item>
</shadow-attribute-example>
<evaluate-example>
<item>
<name>item 1</name>
<category>foo</category>
</item>
<item>
<name>item 3</name>
<category>foo</category>
</item>
</evaluate-example>
CodePudding user response:
I've been trying to find an extension to make this possible in v1 or v2 but have not been sucessful
In some XSLT 1.0 processors (e.g. libxslt
or Xalan
) you can use the EXSLT dyn:evaluate() extension function - for example:
XML
<root>
<item>
<name>item 1</name>
<category>foo</category>
</item>
<item>
<name>item 2</name>
<category>bar</category>
</item>
<item>
<name>item 3</name>
<category>foo</category>
</item>
<item>
<name>item 4</name>
<category>baz</category>
</item>
</root>
XSLT 1.0 EXSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dyn="http://exslt.org/dynamic"
extension-element-prefixes="dyn">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="predicate">category="foo"</xsl:param>
<xsl:template match="/root">
<output>
<xsl:copy-of select="item[dyn:evaluate($predicate)]" />
</output>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>
<item>
<name>item 1</name>
<category>foo</category>
</item>
<item>
<name>item 3</name>
<category>foo</category>
</item>
</output>
Not sure why you need this at all. In your example, the predicate is a variable hard-coded into the stylesheet, not a parameter that can be supplied dynamically during runtime. In these circumstances, you could simply hard-code the predicate directly into the select
expression, without going through a string first.