简体   繁体   中英

Grabbing an Element from a variable with node-set in XSLT

I am trying to get a value from a variable within an XSLT document. Here is my code:

<?xml version="1.0" encoding="utf-8"?>

<xsl:template match="/">

  <Composition xmlns="Playlist">
    <xsl:variable name="source_var">
        <Source identifier="0">Inside Source</Source>
    </xsl:variable>

    <Video>
        <xsl:value-of select="ms:node-set($source_var)/Source"/>
    </Video>
    </Composition>

</xsl:template>

Its not working. The output is this:

<Composition xmlns="Playlist"> <Video></Video> </Composition>

When it should be this:

<Composition xmlns="Playlist"> <Video>Inside Source</Video> </Composition>

When I remove the namespace xmlns="Playlist" from the <Composition> tag it works. So I am think it has to do with the name space, but I have to keep the namespace in.

I feel the ms:node-set($source_var)/Source is looking in the wrong namespace, but I dont know how to fix the probelem.

Change

<xsl:variable name="source_var">
    <Source identifier="0">Inside Source</Source>
</xsl:variable>

to

<xsl:variable name="source_var" xmlns="">
    <Source identifier="0">Inside Source</Source>
</xsl:variable>

if you can, or to

<xsl:variable name="source_var">
    <Source xmlns="" identifier="0">Inside Source</Source>
</xsl:variable>

If you can't change the namespace of the element then you need to use a prefix in your path eg you need

<xsl:value-of select="ms:node-set($source_var)/ns1:Source" xmlns:ns1="Playlist"/>

Of course instead of putting the declaration on the value-of you can also put it on the xsl:stylesheet .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM