简体   繁体   中英

Convert flat XML data into nested XML using XSLT1.0

I have a grouping issue where i need to group all the keys that has the same number under a node. For eg, my XML looks like :

<results>
   <status>completed</status>
   <info>success</info>
   <prod1>abc</prod1>
   <pub1>test</pub1>
   <sub1>123</sub1>
   <subtype1>pt</subtype1>
   <prod2>def</prod2>
   <pub2>test22</pub2>
   <sub2>456</sub2>
   <subtype2>pt</subtype2>
   <prod3>ghi</prod3>
   <pub3>test33</pub3>
   <sub3>789</sub3>
   <subtype3>pt</subtype3>
</results>

I need to convert the above into:

<results>
   <status>completed</status>
   <info>success</info>
   <products>
      <product>
         <prod>abc</prod>
         <pub>test</pub>
         <sub>123</sub>
         <subtype>pt</subtype>
       </product>
       <product>
         <prod>def</prod>
         <pub>test22</pub>
         <sub>456</sub>
         <subtype>pt</subtype>
       </product>
       <product>
         <prod>ghi</prod>
         <pub>test33</pub>
         <sub>789</sub>
         <subtype>pt</subtype>
       </product>
    </products>
</results>

Any help in resolving the above is highly appreciated. I am currently stuck with this issue and not able to proceed.

The below xslt pulls each element and puts into a node and im not able to group all elements that ends with a particular number into a single node.

<?xml version="1.0" encoding="UTF-8"?>
   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:key name="elementByRow" match="/*/*"
           use="(name(.)[1])" />

   <xsl:template match="/messages">
    <messages>
      <!-- pick out the first RowN.* element for each N -->
      <xsl:apply-templates select="*[generate-id() =
         generate-id(key('elementByRow', name(.))[1])]" />
    </messages>
   </xsl:template>

   <xsl:template match="*">
    <row>
      <!-- process _all_ the elements that belong to this row -->
      <xsl:for-each select="key('elementByRow', name(.))[1]">
        <xsl:element name="{name(.)[1]}">
          <xsl:value-of select="." />
        </xsl:element>
      </xsl:for-each>
     </row>
    </xsl:template>
   </xsl:stylesheet>

If the input structure is constant, I would take advantage of that, instead of trying to group by the trailing number.

For example, you could do:

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="*"/>

<xsl:template match="/results">
    <xsl:copy>
        <xsl:copy-of select="status | info"/>
        <products>
            <xsl:for-each select="*[starts-with(name(), 'prod')]">
                <product>
                    <prod>
                        <xsl:value-of select="." />
                    </prod>
                    <pub>
                        <xsl:value-of select="following-sibling::*[1]" />
                    </pub>
                    <sub>
                        <xsl:value-of select="following-sibling::*[2]" />
                    </sub>
                    <subtype>
                        <xsl:value-of select="following-sibling::*[3]" />
                    </subtype>
                </product>
            </xsl:for-each>
        </products>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Here's a more elaborate approach that groups the elements by their number; note, however, that this assumes an element's name will not contain any digits other than the group number at the end.

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="*"/>

<xsl:key name="field-by-group" match="*" use="translate(name(), translate(name(), '0123456789', ''), '')" />

<xsl:template match="/results">
    <xsl:copy>
        <xsl:copy-of select="status | info"/>
        <products>
            <xsl:for-each select="*[starts-with(name(), 'prod')]">
                <product>
                    <xsl:for-each select="key('field-by-group', substring-after(name(), 'prod'))">
                        <xsl:element name="{translate(name(), '0123456789', '')}">
                            <xsl:value-of select="."/>
                        </xsl:element>
                    </xsl:for-each>
                </product>
            </xsl:for-each>
        </products>
    </xsl:copy>
</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