Convert flat XML table data where elements are concatenated row and column names into nested XML, using XSLT 1.0

StackOverflow https://stackoverflow.com/questions/19814352

  •  04-07-2022
  •  | 
  •  

Question

I have XML output representing a table of data being output like this:

<results>
  <Row1.Name>Henry</Row1.Name>
  <Row1.Id>P162</Row1.Id>
  <Row1.Age>23</Row1.Age>
  <Row2.Name>John</Row2.Name>
  <Row2.Id>P137</Row2.Id>
  <Row2.Age>27</Row2.Age>
  <Row3.Name>Mary</Row3.Name>
  <Row3.Id>L493</Row3.Id>
  <Row3.Age>32</Row3.Age>
</results>

and I want to convert it to this:

<results>
  <Row>
    <Name>Henry</Name>
    <Id>P162<Id>
    <Age>23</Age>
  </Row>
  <Row>
    <Name>John</Name>
    <Id>P137<Id>
    <Age>27</Age>
  </Row>
  <Row>
    <Name>Mary</Name>
    <Id>L493<Id>
    <Age>32</Age>
  </Row>
</results>

The application I am working with forces me to use XSLT 1.0, and I'm sure this is really simple, but I'm having a mental block today so I figured I'd ask my virtual colleagues.

Anybody got any ideas?

NOTE: Amended the desired output to not show iterative row numbers, which is what I want.

Haven't got anything close to working yet. Still playing around with different things.

Thought I could write something like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <xsl:variable name="Row" select="distinct-values(*[contains(name(),'Row')])" />
           <xsl:for-each select="$Row">
               <xsl:variable name="rowName" select="name()" />
               <Row>
                   <xsl:for-each select="*[contains(name(),$Row)]">
                       <xsl:copy select="." />
                   </xsl:for-each>
               </Row>
           </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

to produce this:

<results>
    <Row>
        <Row1.Name>Henry</Row1.Name>
        <Row1.Id>P162</Row1.Id>
        <Row1.Age>23</Row1.Age>
    </Row>
    <Row>
        <Row2.Name>John</Row2.Name>
        <Row2.Id>P137</Row2.Id>
        <Row2.Age>27</Row2.Age>
    </Row>
    <Row>
        <Row3.Name>Mary</Row3.Name>
        <Row3.Id>L493</Row3.Id>
        <Row3.Age>32</Row3.Age>
   </Row>
</results>

and then go back and remove all the Row# prefixes with:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="/*[contains(name(),'.')]">
        <xsl:variable name="Name" select="substring-after(name(),'.')" />
        <xsl:element name="{$Name}">
            <xsl:value-of select="." />
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Neither transform works though and I don't think I can use distinct-values with XSLT 1.0

Was it helpful?

Solution

This is a grouping problem, and the standard approach to such problems in XSLT 1.0 is called Muenchian grouping. You define a key that groups your elements in the way you want and then use a trick with generate-id to process just one element per group. In this case you want to group elements by the part of their name before the dot:

<?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="substring-before(name(), '.')" />

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

  <xsl:template match="*">
    <Row>
      <!-- process _all_ the elements that belong to this row -->
      <xsl:for-each select="key('elementByRow', substring-before(name(), '.'))">
        <xsl:element name="{substring-after(name(), '.')}">
          <xsl:value-of select="." />
        </xsl:element>
      </xsl:for-each>
    </Row>
  </xsl:template>
</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top