简体   繁体   中英

Rename XML tags using XSLT

I want to restructure XML using an xslt. In the input XML has ?xml-multiple tags as well.

The Input

<?xml version="1.0" encoding="UTF-8"?>
<jsonObject>
<aaaa>
  <details>
     <name>abc</name>
  </details>
  <approved>true</approved>
</aaaa>
<payload>
  <NICNumber>
     <name>New</name>
     <address>
        <ffff>122</ffff>
        <gggg>true</gggg>
     </address>
     <gccid>1</gccid>
     <?xml-multiple NICNumberAlias?>
     <NICNumberAlias>
        <id>123</id>
     </NICNumberAlias>
     <?xml-multiple managersNICNumber?>
     <managersNICNumber>
        <userId>bbbbee</userId>
     </managersNICNumber>
  </NICNumber>
</payload>
</jsonObject>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/">
    <jsonArray>
        <xsl:for-each select="jsonObject">
            <jsonObject>
                <xsl:apply-templates select="aaaa"/>
                <payload>
                    <xsl:copy-of select="payload/NICNumber/address" />
                    <xsl:apply-templates select="payload/NICNumber/address" />
                    <xsl:processing-instruction name="xml-multiple">NICNumbers</xsl:processing-instruction>
                    <NICNumbers>
                        <xsl:apply-templates select="payload/NICNumber/child::node()"/>
                    </NICNumbers>
                </payload>
            </jsonObject>
        </xsl:for-each>
    </jsonArray>
</xsl:template>

<xsl:template match="payload/NICNumber/address"/>

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

<xsl:template match="payload/NICNumber/managersNICNumber">
    <xsl:if test="count(../managersNICNumber)=1">
      <xsl:processing-instruction name="xml-multiple">xxxx</xsl:processing-instruction>
    </xsl:if>
    <xxxx>
        <xsl:apply-templates select="@*|node()" />
    </xxxx>
</xsl:template>


<xsl:template match="payload/NICNumber/NICNumberAlias">
    <xsl:if test="count(../NICNumberAlias)=1">
      <xsl:processing-instruction name="xml-multiple">NICNumberAliases</xsl:processing-instruction>
    </xsl:if>
    <NICNumberAliases>
        <xsl:apply-templates select="@*|node()" />
    </NICNumberAliases>
</xsl:template>

</xsl:stylesheet>

Expected Result

<?xml version="1.0" encoding="UTF-8"?>
<jsonArray>
<jsonObject>
<aaaa>
    <details>
        <name>abc</name>
    </details>
    <approved>true</approved>
 </aaaa>
  <payload>
     <address>
        <ffff>122</ffff>
        <gggg>true</gggg>
     </address>
     <?xml-multiple NICNumbers?>
     <NICNumbers>
        <name>New</name>
        <gccid>1</gccid>
        <?xml-multiple NICNumberAliases?>
        <NICNumberAliases>
           <id>123</id>
        </NICNumberAliases>
        <?xml-multiple xxxx?>
        <xxxx>
           <userId>bbbbee</userId>
        </xxxx>
     </NICNumbers>
  </payload>
  </jsonObject>
  </jsonArray>

But I'm receiving below response.

<?xml version="1.0" encoding="UTF-8"?>
<jsonArray>
<jsonObject>
  <aaaa>
    <details>
        <name>abc</name>
    </details>
    <approved>true</approved>
  </aaaa>
  <payload>
     <address>
        <ffff>122</ffff>
        <gggg>true</gggg>
     </address>
     <?xml-multiple NICNumbers?>
     <NICNumbers>
        <name>New</name>
        <gccid>1</gccid>
        <?xml-multiple NICNumberAlias?>
        <?xml-multiple NICNumberAliases?>
        <NICNumberAliases>
           <id>123</id>
        </NICNumberAliases>
        <?xml-multiple managersNICNumber?>
        <?xml-multiple xxxx?>
        <xxxx>
           <userId>bbbbee</userId>
        </xxxx>
     </NICNumbers>
  </payload>
 </jsonObject>
 </jsonArray>

From the input message, I moved the content of "managersNICNumber" to xxxx. managersNICNumber had <?xml-multiple managersNICNumber?> in the input payload. Even I moved managersNICNumber to xxxx, in the received message from my XSLT contains <?xml-multiple managersNICNumber?> too.

Is there any wrong in my XSLT?

I could not follow the logic of your XSLT. AFAICT, the required result can be produced using:

XSLT 1.0

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

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

<!-- add wrapper  -->
<xsl:template match="/">
    <jsonArray>
        <xsl:apply-templates/>
    </jsonArray>
</xsl:template>

<xsl:template match="payload">
    <xsl:copy>
        <!-- move address to here -->
        <xsl:copy-of select="NICNumber/address" />
        <xsl:processing-instruction name="xml-multiple">NICNumbers</xsl:processing-instruction>
        <xsl:apply-templates select="NICNumber"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="NICNumber">
    <NICNumbers>
        <!-- remove address -->
        <xsl:apply-templates select="node()[not(self::address)]"/>
    </NICNumbers>
</xsl:template>

<!-- rename NICNumberAlias [1] -->
<xsl:template match="processing-instruction('xml-multiple')[.='NICNumberAlias']">
    <xsl:processing-instruction name="xml-multiple">NICNumberAliases</xsl:processing-instruction>
</xsl:template>

<!-- rename NICNumberAlias [2] -->
<xsl:template match="NICNumberAlias">
    <NICNumberAliases>
        <xsl:apply-templates/>
    </NICNumberAliases>
</xsl:template>

<!-- rename managersNICNumber [1] -->
<xsl:template match="processing-instruction('xml-multiple')[.='managersNICNumber']">
    <xsl:processing-instruction name="xml-multiple">xxxx</xsl:processing-instruction>
</xsl:template>

<!-- rename managersNICNumber [2] -->
<xsl:template match="managersNICNumber">
    <xxxx>
        <xsl:apply-templates/>
    </xxxx>
</xsl:template>

</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM