Shifting row data in xsl 1.0

2019-02-19 17:56发布

问题:

I have multiple Rows data in my XML like this -

<ROW TYPE="SPECIFICATION" ID="1">
    <R1>English1</R1>
</ROW>
<ROW TYPE="SPECIFICATION" ID="1">
    <R1>Maths1</R1>
</ROW>
<ROW TYPE="TOTAL" ID="1">
    <R1>Code1</R1>
</ROW>
<ROW TYPE="SPECIFICATION" ID="2">
    <R1>English2</R1>
</ROW>
<ROW TYPE="SPECIFICATION" ID="2">
    <R1>Maths2</R1>
</ROW>
<ROW TYPE="TOTAL" ID="2">
    <R1>Code2</R1>
</ROW>

I want my Output to be displayed in Image like that this ROW data works in a for-each loop. And the requirement is that ROW TYPE="TOTAL" ID="1" should be displayed first then its ROW TYPE="SPECIFICATION" data. Moving further in loop again, ROW TYPE="TOTAL" ID="2" should be displayed first then its ROW TYPE="SPECIFICATION" data.

Basically it should come like this -

Code1
  English1
  Maths1
Code2
  English2
  Maths2

The XSLT code works for sequence as for-2each loop works. But I need to shift the TOTAL row first for that ID. Please suggest logic for this.

回答1:

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:key name="kRowById"
      match="ROW[not(@TYPE='TOTAL')]" use="@ID"/>

 <xsl:template match="ROW[@TYPE='TOTAL']">
  <xsl:copy-of select="."/>
  <xsl:copy-of select="key('kRowById', @ID)"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the provided XML (wrapped into a single top element to be made a well-formed XML document):

<t>
    <ROW TYPE="SPECIFICATION" ID="1">
        <R1>English1</R1>
    </ROW>
    <ROW TYPE="SPECIFICATION" ID="1">
        <R1>Maths1</R1>
    </ROW>
    <ROW TYPE="TOTAL" ID="1">
        <R1>Code1</R1>
    </ROW>
    <ROW TYPE="SPECIFICATION" ID="2">
        <R1>English2</R1>
    </ROW>
    <ROW TYPE="SPECIFICATION" ID="2">
        <R1>Maths2</R1>
    </ROW>
    <ROW TYPE="TOTAL" ID="2">
        <R1>Code2</R1>
    </ROW>
</t>

produces the wanted result (in which the wanted elements are re-ordered:

<ROW TYPE="TOTAL" ID="1">
   <R1>Code1</R1>
</ROW>
<ROW TYPE="SPECIFICATION" ID="1">
   <R1>English1</R1>
</ROW>
<ROW TYPE="SPECIFICATION" ID="1">
   <R1>Maths1</R1>
</ROW>
<ROW TYPE="TOTAL" ID="2">
   <R1>Code2</R1>
</ROW>
<ROW TYPE="SPECIFICATION" ID="2">
   <R1>English2</R1>
</ROW>
<ROW TYPE="SPECIFICATION" ID="2">
   <R1>Maths2</R1>
</ROW>