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

使用键函数将元素添加到XML无法使用xslt

我正在尝试使用XSLT中的“key”函数提取值并将其添加XML中的特定位置.但我没有把它做对.下面是输入和XSLT.

请帮忙.

输入XML:

<input  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd">
            <info>User Info </info>
            <team>User Team </team>
            <nodeA id="test">
                <inodeAA> Sample </inodeAA>
                <inodeAB> Samples </inodeAB>
            </nodeA>
            <nodeA id="test2">
                <inodeAA> Sample </inodeAA>
                <inodeAB> Samples </inodeAB>
            </nodeA>
            <ns3:output xmlns:ns3="http://mysample.org">
                <ns3:reply id="test">
                    <ns3:pkey>55555</ns3:pkey>
                    <ns3:place>SampleLoc</ns3:place>
                </ns3:reply>
                <ns3:reply id="test2">
                    <ns3:pkey>55557</ns3:pkey>
                    <ns3:place>SampleLoc2</ns3:place>
                </ns3:reply>
            </ns3:output>
        </input>

预期产出:

<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd">
        <info>User Info </info>
        <team>User Team </team>
        <nodeA id="test">
            <inodeAA> Sample </inodeAA>
            <pkey>55555</pkey>
            <inodeAB> Samples </inodeAB>
        </nodeA>
        <nodeA id="test2">
            <inodeAA> Sample </inodeAA>
            <pkey>55557</pkey>
            <inodeAB> Samples </inodeAB>
        </nodeA>            
    </input>

鉴于以下是我的XSL:

<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:ns3="http://mysample.org"
        exclude-result-prefixes="soap xsl">
        <xsl:output omit-xml-declaration="no" indent="yes" />

        <xsl:key name="parKey" match="ns3:output/ns3:reply/ns3:pkey" use="/input/ns3:output/ns3:reply/@id"></xsl:key>

        <xsl:template match="@*|node()">
            <xsl:copy copy-namespaces="no">
                <xsl:apply-templates select="@*|node()" />                  
            </xsl:copy>     
        </xsl:template>

        <xsl:template match="/input">
            <xsl:variable name="output_186" select="ns3:output"/>
            <xsl:copy>
                <xsl:copy-of copy-namespaces="no" select="./@*" />
                <xsl:copy-of select=" * except $output_186"></xsl:copy-of>
            </xsl:copy> 
        </xsl:template>

        <xsl:template match="/input//nodeA">
            <xsl:copy>
                <xsl:copy-of copy-namespaces="no" select="./@*" />
                <pkey>
                    <xsl:copy-of select="key('parKey',@id)|." />           
                </pkey>                 
                <xsl:copy-of copy-namespaces="no" select="node()" />
            </xsl:copy>             
        </xsl:template> 
    </xsl:stylesheet>

输出如下:未显示我的必填字段

<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"        
        xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd" >
        <info>User Info </info>
        <team>User Team </team>
        <nodeA id="test">
            <inodeAA> Sample </inodeAA>
            <inodeAB> Samples </inodeAB>
        </nodeA>
        <nodeA id="test2">
            <inodeAA> Sample </inodeAA>
            <inodeAB> Samples </inodeAB>
        </nodeA>
    </input>

请尽我所能帮助我.

谢谢…

解决方法

更短的解决方案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns3="http://mysample.org">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:key name="kpKeyByParentId" match="ns3:pkey" use="../@id"/>

 <xsl:template match="node()|@*" name="identity">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="inodeAA">
  <xsl:call-template name="identity"/>
  <xsl:apply-templates select="key('kpKeyByParentId',../@id)"/>
 </xsl:template>

 <xsl:template match="ns3:pkey">
   <xsl:element name="{local-name()}">
    <xsl:apply-templates/>
   </xsl:element>
 </xsl:template>
 <xsl:template match="ns3:output"/>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<input  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd">
    <info>User Info </info>
    <team>User Team </team>
    <nodeA id="test">
        <inodeAA> Sample </inodeAA>
        <inodeAB> Samples </inodeAB>
    </nodeA>
    <nodeA id="test2">
        <inodeAA> Sample </inodeAA>
        <inodeAB> Samples </inodeAB>
    </nodeA>
    <ns3:output xmlns:ns3="http://mysample.org">
        <ns3:reply id="test">
            <ns3:pkey>55555</ns3:pkey>
            <ns3:place>SampleLoc</ns3:place>
        </ns3:reply>
        <ns3:reply id="test2">
            <ns3:pkey>55557</ns3:pkey>
            <ns3:place>SampleLoc2</ns3:place>
        </ns3:reply>
    </ns3:output>
</input>

产生了想要的正确结果:

<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd">
   <info>User Info </info>
   <team>User Team </team>
   <nodeA id="test">
      <inodeAA> Sample </inodeAA>
      <pkey>55555</pkey>
      <inodeAB> Samples </inodeAB>
   </nodeA>
   <nodeA id="test2">
      <inodeAA> Sample </inodeAA>
      <pkey>55557</pkey>
      <inodeAB> Samples </inodeAB>
   </nodeA>
</input>

说明:

>标识规则复制为其选择执行的每个节点“按原样”.>与inodeAA匹配的ovveriding模板调用身份模板复制自身,然后在任何ns3:pkey元素上应用模板,其父ID的id属性与此inodeAA父级的id属性具有相同的值.为了方便和有效,这是通过调用引用“kpKeyByParentId”键的key()函数来完成的,该键将ns3:pkey定义为其父项的id属性函数.>与ns3:pkey元素匹配的模板会创建一个元素(在无命名空间中),其名称是匹配元素的本地名称,并且还复制其内容.>元素ns3:输出及其整个子树被具有空体的匹配模板从处理(“删除”)中排除.

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