Using xsl:evaluate to evaluate value of XPath

724 views Asked by At

I am trying to use xsl:evaluate to get the value referecend by an XPath, but can't get it to work. Here is an example (it would not make sense to use xsl:evaluate for this in a real application).
So what I expected in my code was to evaluate the XPath expression defined and store the value of that referecend attribute in a variable to be used in my code.
There must be something I don't understand in the way to use xsl:evaluate.

Input XML

<?xml version="1.0" encoding="UTF-8"?>
<Country name="USA">
    <State name="California">
        <City name="Los Angeles"/>
        <City name="San Francisco"/>
    </State>
</Country>

Expected output

<?xml version="1.0" encoding="UTF-8"?>
<Country name="USA">
    <State name="California">
        <City name="USA"/>
        <City name="San Francisco"/>
    </State>
</Country>

My XSLT code

<?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 message I am getting

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
1

There are 1 answers

0
Martin Honnen On BEST ANSWER

I think you want

    <xsl:variable name="input" as="xs:string">
        <xsl:evaluate xpath="$xPathString" context-item="."/>
    </xsl:variable>