Shifting row data in xsl 1.0

五迷三道 提交于 2019-12-19 10:22:13

问题


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>


来源:https://stackoverflow.com/questions/15539332/shifting-row-data-in-xsl-1-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!