简体   繁体   中英

How to go from XML file with a stucture having a parent node and a child node to a structure with only one level

Using ssis 'XML task', is there a XSL that makes it possible to go from an xml file with a parent node and child node to a xml file with only one level: same number of occurrences in the child node in the input xml as in the number of occurrences in the output xml. Here an example

Input xml

<?xml version="1.0" encoding="utf-8"?>
<RL>
  <RL0601a>BEAUDRY</RL0601a>
  <r>
    <RL0104B>61</RL0104B>
    <RL0104C>2095</RL0104C>
  </r>
  <r>
    <RL0104B>64</RL0104B>
    <RL0104C>1090</RL0104C>
  </r>
</RL>

Output xml

<?xml version="1.0" encoding="utf-8"?>
<RL>
<r>
    <RL0601a>BEAUDRY</RL0601a>
    <RL0104B>61</RL0104B>
    <RL0104C>2095</RL0104C>
</r>
<r>
    <RL0601a>BEAUDRY</RL0601a> 
    <RL0104B>64</RL0104B>
    <RL0104C>1090</RL0104C>
</r>
</RL>

Input XML

<?xml version="1.0" encoding="utf-8"?>
<RL>
    <RL0601a>BEAUDRY</RL0601a>
    <r>
        <RL0104B>61</RL0104B>
        <RL0104C>2095</RL0104C>
    </r>
    <r>
        <RL0104B>64</RL0104B>
        <RL0104C>1090</RL0104C>
    </r>
</RL>

XSLT

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

    <xsl:template match="/RL">
        <root>
            <xsl:for-each select="r">
                <xsl:copy>
                    <xsl:copy-of select="../RL0601a"/>
                    <xsl:copy-of select="*"/>
                </xsl:copy>
            </xsl:for-each>
        </root>
    </xsl:template>
</xsl:stylesheet>

Output XML

<?xml version='1.0' encoding='utf-8' ?>
<root>
  <r>
    <RL0601a>BEAUDRY</RL0601a>
    <RL0104B>61</RL0104B>
    <RL0104C>2095</RL0104C>
  </r>
  <r>
    <RL0601a>BEAUDRY</RL0601a>
    <RL0104B>64</RL0104B>
    <RL0104C>1090</RL0104C>
  </r>
</root>

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