简体   繁体   中英

How to remove the parent element and move up the child element in XML using XSLT

Hi im new to XSLT stuff and I have a payload like below. This payload has parent elements and a parent has child elements as below.

 <jsonObject>
  
    <FN_PN>
        <?xml-multiple  item?>
        <item>
           <PARTNER>11111</PARTNER>
            <RLTYP>222222</RLTYP>
            <DESC_FN>eeeeee</DESC_FN>
        </item>
    </FN_PN>
    <TI_DADOS>
        <?xml-multiple  item?>
        <item>
           <name>test</name>
            <age>20</age>
        </item>
        <item>
            <name>test</name>
            <age>20</age>
        </item>
        <item>
           <name>test</name>
            <age>20</age>
        </item>
    </TI_DADOS>

        </jsonObject>

Im trying to remove the item array and copy the items inside the array and set it to the parent element. Below is the xslt that i tried. it removes the item array and it copied the items inside item array. But now im trying to remove the parent Below is my XSLT that removes the item

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="@*|node()">
<xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>   
  </xsl:template>
<xsl:template match="//processing-instruction()" />
<xsl:template match="node()/item">
   <xsl:if
      test="local-name(.) != local-name(preceding-sibling::*[1])">
      <xsl:processing-instruction name="xml-multiple">
        <xsl:value-of select="local-name(.)" />
      </xsl:processing-instruction>
    </xsl:if>
<xsl:element name="{local-name(.)}">
       <xsl:apply-templates />
</xsl:element>
    </xsl:template>
</xsl:stylesheet>

Here is the out put i get by running the above XSLT

<jsonObject>
  
    <FN_PN>
        <?xml-multiple  FN_PN?>
        <FN_PN>
            <PARTNER>11111</PARTNER>
            <RLTYP>222222</RLTYP>
            <DESC_FN>eeeeee</DESC_FN>
        </FN_PN>
    </FN_PN>
    <TI_DADOS>
        <?xml-multiple  TI_DADOS?>
        <TI_DADOS>
            <name>test</name>
            <age>20</age>
        </TI_DADOS>
        <TI_DADOS>
              <name>test</name>
            <age>20</age>
        </TI_DADOS>
        <TI_DADOS>
           <name>test</name>
            <age>20</age>
        </TI_DADOS>
    </TI_DADOS>

    </jsonObject>

Below is the xml format im trying to get

 <jsonObject>
  
   
        <?xml-multiple  FN_PN?>
        <FN_PN>
            <PARTNER>11111</PARTNER>
            <RLTYP>222222</RLTYP>
            <DESC_FN>eeeeee</DESC_FN>
        </FN_PN>
    
    
        <?xml-multiple  TI_DADOS?>
        <TI_DADOS>
            <name>test</name>
            <age>20</age>
        </TI_DADOS>
        <TI_DADOS>
              <name>test</name>
            <age>20</age>
        </TI_DADOS>
        <TI_DADOS>
           <name>test</name>
            <age>20</age>
        </TI_DADOS>
  

    </jsonObject>

Im trying to remove <FN_PN></FN_PN> and <TI_DADOS></TI_DADOS> parent nodes while keeping the child elements

AFAICT the result you show can be obtained 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>

<!-- skip item's parent -->
<xsl:template match="*[item]">
    <xsl:apply-templates/>
</xsl:template>

<!-- rename item to its parent's name -->
<xsl:template match="item">
    <xsl:element name="{name(..)}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<!-- add name to PI -->
<xsl:template match="processing-instruction()">
    <xsl:processing-instruction name="{name()}">
        <xsl:value-of select="name(..)" />
    </xsl:processing-instruction>
</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