XSLT - copy all other node s, add 1 new node

I created XSLT, and I was wondering how you can copy all the nodes between one set of tags and add another tag at the bottom. I created XSLT, which has all the logic to determine which tag to add and what it should call. However, the problem I'm getting right now is that I cannot copy all the other tags. The following are the files:

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>

CURRENT EXIT

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

DESIRED EXIT

<?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>
+5
source share
4 answers

This is a short and simple conversion :

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

when applied to the provided XML document:

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

creates the desired, correct result :

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

Explanation

  • " " node, . p >

  • product "" ( ).

  • productId node, product/article.

+7

. :

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

:

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

, : $ean node node, node , XML node . ( , 3 , .)

+2

, 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: ( ).

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

All Articles