使用 xsl:evaluate 计算 XPath 的值
Using xsl:evaluate to evaluate value of XPath
我正在尝试使用 xsl:evaluate 来获取 XPath 引用的值,但无法正常工作。
这是一个示例(在实际应用程序中为此使用 xsl:evaluate 是没有意义的)。
所以我在我的代码中期望的是评估定义的 XPath 表达式并将该 referecend 属性的值存储在一个变量中以在我的代码中使用。
xsl:evaluate.
的使用方法一定有不明白的地方
输入XML
<?xml version="1.0" encoding="UTF-8"?>
<Country name="USA">
<State name="California">
<City name="Los Angeles"/>
<City name="San Francisco"/>
</State>
</Country>
预期产出
<?xml version="1.0" encoding="UTF-8"?>
<Country name="USA">
<State name="California">
<City name="USA"/>
<City name="San Francisco"/>
</State>
</Country>
我的 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:output method="xml" indent="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@name[.='Los Angeles']">
<xsl:variable name="xPathString" select="'ancestor::Country/@name'"/>
<xsl:variable name="input" as="xs:string">
<xsl:evaluate xpath="$xPathString"/>
</xsl:variable>
<xsl:attribute name="name" select="$input"/>
</xsl:template>
</xsl:stylesheet>
我收到错误消息
Error at char 0 in expression in xsl:evaluate/@xpath on line 19 column 40 of Test.xsl:
XPDY0002 Dynamic error in expression {ancestor::Country/@name} called using xsl:evaluate.
Found while atomizing the value of variable $input
In template rule with match="@name[xs:string(.) eq "Los Angeles"]" on line 15 of Test.xsl
invoked by xsl:apply-templates at file:/C:/Users/ck3503/Documents/Repositories/outilscrea/specifications/profil/xslt/Test.xsl#11
In template rule with match="(comment()|(processing-instruction()|(element()|text())))" on line 9 of Test.xsl
invoked by xsl:apply-templates at file:/C:/Users/ck3503/Documents/Repositories/outilscrea/specifications/profil/xslt/Test.xsl#11
In template rule with match="(comment()|(processing-instruction()|(element()|text())))" on line 9 of Test.xsl
invoked by xsl:apply-templates at file:/C:/Users/ck3503/Documents/Repositories/outilscrea/specifications/profil/xslt/Test.xsl#11
In template rule with match="(comment()|(processing-instruction()|(element()|text())))" on line 9 of Test.xsl
invoked by built-in template rule (text-only)
Dynamic error in expression {ancestor::Country/@name} called using xsl:evaluate. Found while atomizing the value of variable $input
我想你想要
<xsl:variable name="input" as="xs:string">
<xsl:evaluate xpath="$xPathString" context-item="."/>
</xsl:variable>
我正在尝试使用 xsl:evaluate 来获取 XPath 引用的值,但无法正常工作。
这是一个示例(在实际应用程序中为此使用 xsl:evaluate 是没有意义的)。
所以我在我的代码中期望的是评估定义的 XPath 表达式并将该 referecend 属性的值存储在一个变量中以在我的代码中使用。
xsl:evaluate.
输入XML
<?xml version="1.0" encoding="UTF-8"?>
<Country name="USA">
<State name="California">
<City name="Los Angeles"/>
<City name="San Francisco"/>
</State>
</Country>
预期产出
<?xml version="1.0" encoding="UTF-8"?>
<Country name="USA">
<State name="California">
<City name="USA"/>
<City name="San Francisco"/>
</State>
</Country>
我的 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:output method="xml" indent="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@name[.='Los Angeles']">
<xsl:variable name="xPathString" select="'ancestor::Country/@name'"/>
<xsl:variable name="input" as="xs:string">
<xsl:evaluate xpath="$xPathString"/>
</xsl:variable>
<xsl:attribute name="name" select="$input"/>
</xsl:template>
</xsl:stylesheet>
我收到错误消息
Error at char 0 in expression in xsl:evaluate/@xpath on line 19 column 40 of Test.xsl:
XPDY0002 Dynamic error in expression {ancestor::Country/@name} called using xsl:evaluate.
Found while atomizing the value of variable $input
In template rule with match="@name[xs:string(.) eq "Los Angeles"]" on line 15 of Test.xsl
invoked by xsl:apply-templates at file:/C:/Users/ck3503/Documents/Repositories/outilscrea/specifications/profil/xslt/Test.xsl#11
In template rule with match="(comment()|(processing-instruction()|(element()|text())))" on line 9 of Test.xsl
invoked by xsl:apply-templates at file:/C:/Users/ck3503/Documents/Repositories/outilscrea/specifications/profil/xslt/Test.xsl#11
In template rule with match="(comment()|(processing-instruction()|(element()|text())))" on line 9 of Test.xsl
invoked by xsl:apply-templates at file:/C:/Users/ck3503/Documents/Repositories/outilscrea/specifications/profil/xslt/Test.xsl#11
In template rule with match="(comment()|(processing-instruction()|(element()|text())))" on line 9 of Test.xsl
invoked by built-in template rule (text-only)
Dynamic error in expression {ancestor::Country/@name} called using xsl:evaluate. Found while atomizing the value of variable $input
我想你想要
<xsl:variable name="input" as="xs:string">
<xsl:evaluate xpath="$xPathString" context-item="."/>
</xsl:variable>