Shifting row data in xsl 1.0

前端 未结 1 1673
终归单人心
终归单人心 2021-01-15 11:28

I have multiple Rows data in my XML like this -


    English1



        
相关标签:
1条回答
  • 2021-01-15 12:02

    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>
    
    0 讨论(0)
提交回复
热议问题