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

xml – XSLT – 复制所有其他节点,添加1个新节点

我创建了一个XSLT,我想知道如何在一组标签之间复制所有节点,并在底部添加一个标签.我创建了XSLT,它具有确定要添加哪个标记以及应该调用标记的所有逻辑.但是我现在遇到的问题是我无法复制所有其他标签.以下是有问题的文件

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.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="/csvImportSchema">
        <csvImportSchema>
            <xsl:for-each select="payload">
                <payload>
                    <xsl:copy-of select="@*"/>
                    <xsl:variable name="ean">
                        <xsl:value-of select="ean"/>
                    </xsl:variable>
                    <xsl:for-each select="../product">
                        <xsl:if test="ean = $ean">
                            <productId><xsl:value-of select="article"/></productId>
                        </xsl:if>
                    </xsl:for-each>
                </payload>
            </xsl:for-each>
        </csvImportSchema>
    </xsl:template>

</xsl:stylesheet>

INPUT

<?xml version="1.0" encoding="UTF-8"?>
<csvImportSchema>
    <payload>
        <test>1</test>
        <test2>2</test2>
        <test3>3</test3>
        <ean>1111111111</ean>
        <productId/>
    </payload>
    <product>
        <article>722619</article>
        <ean>1111111111</ean>
    </product>
</csvImportSchema>

当前输出

<?xml version="1.0" encoding="utf-8"?>
<csvImportSchema>
    <payload>
        <productId>722619</productId>
    </payload>
</csvImportSchema>

期望的输出

<?xml version="1.0" encoding="UTF-8"?>
<csvImportSchema>
    <payload>
        <test>1</test>
        <test2>2</test2>
        <test3>3</test3>
        <ean>1111111111</ean>
        <productId>722619</productId>
    </payload>
</csvImportSchema>

解决方法

这个简短而简单的转变:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="productId">
  <productId>
    <xsl:value-of select="../../product/article"/>
  </productId>
 </xsl:template>
 <xsl:template match="product"/>
</xsl:stylesheet>

当应用于提供的XML文档时:

<csvImportSchema>
    <payload>
        <test>1</test>
        <test2>2</test2>
        <test3>3</test3>
        <ean>1111111111</ean>
        <productId/>
    </payload>
    <product>
        <article>722619</article>
        <ean>1111111111</ean>
    </product>
</csvImportSchema>

产生想要的,正确的结果:

<csvImportSchema>
   <payload>
      <test>1</test>
      <test2>2</test2>
      <test3>3</test3>
      <ean>1111111111</ean>
      <productId>722619</productId>
   </payload>
</csvImportSchema>

说明:

> identity rule副本“按原样”复制选择执行的每个节点.>覆盖模板匹配产品从输出中“删除”此元素(通过其空体).>另一个重写模板与productId匹配,并使用取自product / article的文本节点子项生成此元素.

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