0

我有这个输入

<?xml version="1.0" encoding="utf-8"?>
        <Native xmlns="http://www.cargowise.com/Schemas/Native/2011/11" version="2.0">
                <Header>
                    <OwnerCode>VISSHIMEL</OwnerCode>
                </Header>
        </Native>

我的 xslt 中有一个声明的节点集:

<xsl:variable name="SupplierSCAC">
           <m>Yellow</m>
           <m>Red</m>
           <m>Green</m>
         </xsl:variable>

我正在尝试为此节点集执行每个操作并选择 OwnerCode 值“VISSHIMEL”,但由于某种原因,我的模板调用没有获得该值。预期输出为

<TariffCollection>
Yellow
<test>VISSHIMEL</test>
</TariffCollection>
<TariffCollection>
Red
<test>VISSHIMEL</test>
</TariffCollection>
<TariffCollection>
Green
<test>VISSHIMEL</test>
</TariffCollection>**

然而,<test>始终是空的。这是我的代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0 s1 exsl" version="1.0" xmlns:s0="http://www.cargowise.com/Schemas/Native/2011/11" xmlns:s1="http://www.cargowise.com/Schemas/Universal/2011/11" xmlns:exsl="http://exslt.org/common" >
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0"/>
    <xsl:template match="/">
        <xsl:variable name="SupplierSCAC">
           <m>Yellow</m>
           <m>Red</m>
           <m>Green</m>
        </xsl:variable> 
        <xsl:for-each select="exsl:node-set($SupplierSCAC)/m">
            <xsl:call-template name="Test">
                <xsl:with-param name="SupplierSCAC" select="."/>
            </xsl:call-template>
            <!-- <xsl:apply-templates select="/s0:Native" > -->
                <!-- <xsl:with-param name="SupplierSCAC" select="."/> -->
            <!-- </xsl:apply-templates> -->
        </xsl:for-each> 
    </xsl:template>
        
    <xsl:template name="Test" match="/s0:Native">
    <xsl:param name="SupplierSCAC"/>
        <TariffCollection>
            <xsl:value-of select="$SupplierSCAC"/>
            <test>
                <xsl:value-of select="s0:Header/s0:OwnerCode"/>
            </test>
        </TariffCollection>
    </xsl:template>
</xsl:stylesheet>

我知道 for-each 并且参数已成功显示在输出中,但我不知道为什么我无法访问 xpath OwnerCode 的值。我尝试使用 apply-template ,但更糟糕的是因为我根本没有输出。如果您需要更多信息,请与我们联系。谢谢!

4

1 回答 1

0

xsl:call-template当指令调用模板时,该match属性将被忽略。因此,您仍然处于由以下人员建立的上下文中:

<xsl:for-each select="exsl:node-set($SupplierSCAC)/m">

和你的指示

<xsl:value-of select="s0:Header/s0:OwnerCode"/>

什么都不选择,因为这些节点在另一个文档中。

尝试类似:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:s0="http://www.cargowise.com/Schemas/Native/2011/11" 
xmlns:exsl="http://exslt.org/common" 
exclude-result-prefixes="s0 exsl" >
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0"/>
    
<xsl:template match="/">
    <xsl:variable name="SupplierSCAC">
        <m>Yellow</m>
        <m>Red</m>
        <m>Green</m>
    </xsl:variable> 
    <xsl:variable name="root" select="/s0:Native" />
    <xsl:for-each select="exsl:node-set($SupplierSCAC)/m">
        <TariffCollection>
            <xsl:value-of select="."/>
            <test>
                <xsl:value-of select="$root/s0:Header/s0:OwnerCode"/>
            </test>
        </TariffCollection>
    </xsl:for-each> 
</xsl:template>
        
</xsl:stylesheet>

请注意,结果是一个XML 片段,没有单个根元素。

于 2021-07-09T07:52:25.153 回答