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

6

我已经创建了一个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>

输入

<?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>
4个回答

10

这个简短而简单的转换:

<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>

说明

  1. 身份规则会将其选择执行的每个节点完全复制。

  2. 一个覆盖模板匹配product,通过其空主体从输出中“删除”了此元素。

  3. 另一个覆盖模板匹配productId并生成此元素,其文本节点子节点来自product/article


2
您的代码有一个观察点。不要使用这个:

<xsl:variable name="ean">
    <xsl:value-of select="../ean"/>
</xsl:variable>

当您可以编写以下内容时:

当你能够编写这样的代码:

<xsl:variable name="ean" select="../ean"/>

这种方法不仅冗长,而且效率极低:它不是将$ean绑定到现有节点,而是提取现有节点的字符串值,用该字符串值形成文本节点,创建一个新的XML文档树,并将此文本节点添加到该新文档的内容中。(我曾经通过消除这种可怕的结构使样式表运行速度提高了3倍。)


1

这应该很简单,只需将您的有效载荷 xsl: copy-of select = "@ *"/ 更改为

<xsl:copy-of select="*[local-name() != 'productId'] | @*"/>

即复制所有内容,除了productId,因为您需要手动构建它。

这将提供您所需的输出。

<?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>

1

这个XSLT应该可以完成任务,并使用更多的COPY标签和模板。也许不要在一个xsl:template中完成所有操作(我的意见)。

    <?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">
        <xsl:copy>
            <xsl:apply-templates select="*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="csvImportSchema/payload/productId">
        <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>
    </xsl:template>

    <xsl:template match="csvImportSchema/product">
        <!-- do not copy -->
    </xsl:template>

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接