微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用xsl:evaluate评估XPath的值

如何解决使用xsl:evaluate评估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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。