我不知道为什么我的queryString变量没有在totalRecords的select语句中读取。我错过了什么或做错了什么?
<?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>
XSLT3示例显示使用带静态参数的shadow属性或xsl:evaluate
从字符串构造谓词表达式:
<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给出例如
<?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>
我一直在试图找到一个扩展,以在v1或v2中实现这一点,但没有成功
在一些XSLT 1.0处理器(例如libxslt
或Xalan
(中,您可以使用EXSLT dyn:evaluate((扩展函数-例如:
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>
结果
<?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>
不知道你为什么需要这个。在您的示例中,谓词是硬编码到样式表中的变量,而不是可以在运行时动态提供的parameter。在这种情况下,您可以简单地将谓词直接硬编码到select
表达式中,而无需首先遍历字符串。