简体   繁体   中英

XSLT to flatten XML(has repeating elements)

I need to flatten an XML using XSLT. Below is the XML

<Parent>
   <Child type="X">
      <data value="1" />
      <data value="2" />
      <data value="3" />
   </Child>
   <Child type="Y">
      <data value="5" />
      <data value="6" />
      <data value="7" />
      <data value="8" />
   </Child>
</Parent>

Desired output

<Parent>
   <Child type="X" data1_value="1" data2_value="2" data3_value="3"/>
   <Child type="Y" data1_value="5" data2_value="6" data3_value="7" data4_value="8"/>
</Parent>

You haven't put much effort into defining the details of your scenario, but this should work for that input:

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

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

  <xsl:template match="/*/*/*">
    <xsl:variable name="name" select="local-name()" />
    <xsl:variable name="num">
      <xsl:number count="*[local-name() = $name]"/>
    </xsl:variable>

    <xsl:apply-templates select="@*">
      <xsl:with-param name="prefix" select="concat(local-name(), $num)" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="/*/*/*/@*">
    <xsl:param name="prefix" />

    <xsl:attribute name="{concat($prefix, '_', local-name())}">
      <xsl:value-of select="." />
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

When run on your sample input, the result is:

<Parent>
  <Child type="X" data1_value="1" data2_value="2" data3_value="3" />
  <Child type="Y" data1_value="5" data2_value="6" data3_value="7" data4_value="8" />
</Parent>

If there are grandchild elements with different names, like this:

<Parent>
   <Child type="X">
      <data value="1" />
      <data value="2" />
      <data value="3" />
   </Child>
   <Child type="Y">
      <data value="5" />
      <mydata value="6" />
      <data value="7" />
      <mydata value="8" />
   </Child>
</Parent>

Then each distinct name will be numbered separately (I assume that this is preferable to numbering everything all together):

<Parent>
  <Child type="X" data1_value="1" data2_value="2" data3_value="3" />
  <Child type="Y" data1_value="5" mydata1_value="6" data2_value="7" mydata2_value="8" />
</Parent>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" encoding="utf-8" indent="yes" />
   <xsl:template match="/">
      <Parent>
         <xsl:for-each select="/Parent/Child">
            <Child type="{@type}">
               <xsl:for-each select="data">
                  <xsl:attribute name="data{position()}_value">
                     <xsl:value-of select="@value" />
                  </xsl:attribute>
               </xsl:for-each>
            </Child>
         </xsl:for-each>
      </Parent>
   </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