XSLT将2个节点集转换为行和列

时间:2011-04-24 13:21:19

标签: xml xslt

我是XML& XSLT但是遇到了这个问题。 我有以下XML文件格式: -

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="testing.xsl"?>
<asx:ABAP xmlns:asx="http://www.sap.com/abapxml" version="1.0">
  <asx:VALUES>
    <SOURCE>
      <AXIS_INFO>
        <RRWS_SX_AXIS_INFO>
          <AXIS>000</AXIS>
          <NCHARS>1</NCHARS>
          <NCOORDS>3</NCOORDS>
          <CHARS>
            <RRWS_SX_AXIS_CHARS>
              <CHANM>49E9DSTKHGHIYTUZRJ1XKDLC6</CHANM>
              <HIENM />
              <VERSION />
              <DATETO>0000-00-00</DATETO>
              <CAPTION>Key Figures</CAPTION>
              <CHAPRSNT>1</CHAPRSNT>
              <CHATYP>0</CHATYP>
              <ATTRINM />
            </RRWS_SX_AXIS_CHARS>
          </CHARS>
        </RRWS_SX_AXIS_INFO>
        <RRWS_SX_AXIS_INFO>
          <AXIS>001</AXIS>
          <NCHARS>3</NCHARS>
          <NCOORDS>370</NCOORDS>
          <CHARS>
            <RRWS_SX_AXIS_CHARS>
              <CHANM>0IS_CSTATUS</CHANM>
              <HIENM />
              <VERSION />
              <DATETO>0000-00-00</DATETO>
              <CAPTION>Claim Status</CAPTION>
              <CHAPRSNT>1</CHAPRSNT>
              <CHATYP>1</CHATYP>
              <ATTRINM />
            </RRWS_SX_AXIS_CHARS>
            <RRWS_SX_AXIS_CHARS>
              <CHANM>INJDESCRP</CHANM>
              <HIENM />
              <VERSION />
              <DATETO>0000-00-00</DATETO>
              <CAPTION>Injury Descriptor</CAPTION>
              <CHAPRSNT>1</CHAPRSNT>
              <CHATYP>1</CHATYP>
              <ATTRINM />
            </RRWS_SX_AXIS_CHARS>
            <RRWS_SX_AXIS_CHARS>
              <CHANM>0IS_CLAIMNT</CHANM>
              <HIENM />
              <VERSION />
              <DATETO>0000-00-00</DATETO>
              <CAPTION>Claimant</CAPTION>
              <CHAPRSNT>1</CHAPRSNT>
              <CHATYP>1</CHATYP>
              <ATTRINM>
                <RRWS_S_ATTRINM>
                  <ATTRINM>ZIPCD1ST</ATTRINM>
                  <CAPTION>Post Code 1st part.</CAPTION>
                  <CHAPRSNT>2</CHAPRSNT>
                </RRWS_S_ATTRINM>
              </ATTRINM>
            </RRWS_SX_AXIS_CHARS>
          </CHARS>
        </RRWS_SX_AXIS_INFO>
      </AXIS_INFO>
      <CELL_DATA>
        <RRWS_S_CELL>
          <CELL_ORDINAL>000000</CELL_ORDINAL>
          <VALUE>1.00000000000000</VALUE>
          <FORMATTED_VALUE>1</FORMATTED_VALUE>
          <VALUE_TYPE />
          <CURRENCY />
          <UNIT />
          <MWKZ>F</MWKZ>
          <NUM_SCALE>0</NUM_SCALE>
          <NUM_PREC>0</NUM_PREC>
          <CELL_STATUS />
          <BACK_COLOR>00</BACK_COLOR>
        </RRWS_S_CELL>
        <RRWS_S_CELL>
          <CELL_ORDINAL>000001</CELL_ORDINAL>
          <VALUE>12131.80</VALUE>
          <FORMATTED_VALUE>£ 12,131.80</FORMATTED_VALUE>
          <VALUE_TYPE />
          <CURRENCY>GBP</CURRENCY>
          <UNIT />
          <MWKZ>W</MWKZ>
          <NUM_SCALE>0</NUM_SCALE>
          <NUM_PREC>0</NUM_PREC>
          <CELL_STATUS />
          <BACK_COLOR>00</BACK_COLOR>
        </RRWS_S_CELL>
        <RRWS_S_CELL>
          <CELL_ORDINAL>000002</CELL_ORDINAL>
          <VALUE>10131.80</VALUE>
          <FORMATTED_VALUE>£ 10,131.80</FORMATTED_VALUE>
          <VALUE_TYPE />
          <CURRENCY>GBP</CURRENCY>
          <UNIT />
          <MWKZ>W</MWKZ>
          <NUM_SCALE>0</NUM_SCALE>
          <NUM_PREC>0</NUM_PREC>
          <CELL_STATUS />
          <BACK_COLOR>00</BACK_COLOR>
        </RRWS_S_CELL>
        <RRWS_S_CELL>
          <CELL_ORDINAL>000003</CELL_ORDINAL>
          <VALUE>1.00000000000000</VALUE>
          <FORMATTED_VALUE>1</FORMATTED_VALUE>
          <VALUE_TYPE />
          <CURRENCY />
          <UNIT />
          <MWKZ>F</MWKZ>
          <NUM_SCALE>0</NUM_SCALE>
          <NUM_PREC>0</NUM_PREC>
          <CELL_STATUS />
          <BACK_COLOR>00</BACK_COLOR>
        </RRWS_S_CELL>
        <RRWS_S_CELL>
          <CELL_ORDINAL>000004</CELL_ORDINAL>
          <VALUE>28116.90</VALUE>
          <FORMATTED_VALUE>£ 28,116.90</FORMATTED_VALUE>
          <VALUE_TYPE />
          <CURRENCY>GBP</CURRENCY>
          <UNIT />
          <MWKZ>W</MWKZ>
          <NUM_SCALE>0</NUM_SCALE>
          <NUM_PREC>0</NUM_PREC>
          <CELL_STATUS />
          <BACK_COLOR>00</BACK_COLOR>
        </RRWS_S_CELL>
        <RRWS_S_CELL>
          <CELL_ORDINAL>000005</CELL_ORDINAL>
          <VALUE>7616.90</VALUE>
          <FORMATTED_VALUE>£ 7,616.90</FORMATTED_VALUE>
          <VALUE_TYPE />
          <CURRENCY>GBP</CURRENCY>
          <UNIT />
          <MWKZ>W</MWKZ>
          <NUM_SCALE>0</NUM_SCALE>
          <NUM_PREC>0</NUM_PREC>
          <CELL_STATUS />
          <BACK_COLOR>00</BACK_COLOR>
        </RRWS_S_CELL>
       </CELL_DATA>
       <AXIS_DATA>
        <RRWS_SX_AXIS_DATA>
          <AXIS>000</AXIS>
          <SET>
            <RRWS_SX_TUPLE>
              <TUPLE_ORDINAL>000000</TUPLE_ORDINAL>
              <CHANM>49E9DSTKHGHIYTUZRJ1XKDLC6</CHANM>
              <CAPTION>Count of Unique Claim Numbers</CAPTION>
              <CHAVL>49E9DT190F38HGEFXD49UFK1Y</CHAVL>
              <CHAVL_EXT />
              <NIOBJNM />
              <TLEVEL>00</TLEVEL>
              <DRILLSTATE />
              <OPT />
              <SIGN />
              <ATTRIBUTES />
            </RRWS_SX_TUPLE>
            <RRWS_SX_TUPLE>
              <TUPLE_ORDINAL>000001</TUPLE_ORDINAL>
              <CHANM>49E9DSTKHGHIYTUZRJ1XKDLC6</CHANM>
              <CAPTION>Current reserve</CAPTION>
              <CHAVL>49E9DT8XJDOY02XW376M4HIRQ</CHAVL>
              <CHAVL_EXT />
              <NIOBJNM />
              <TLEVEL>00</TLEVEL>
              <DRILLSTATE />
              <OPT />
              <SIGN />
              <ATTRIBUTES />
            </RRWS_SX_TUPLE>
            <RRWS_SX_TUPLE>
              <TUPLE_ORDINAL>000002</TUPLE_ORDINAL>
              <CHANM>49E9DSTKHGHIYTUZRJ1XKDLC6</CHANM>
              <CAPTION>Payment Amount Posted in CM</CAPTION>
              <CHAVL>49E9DTGM2CANIPHC918YEJHHI</CHAVL>
              <CHAVL_EXT />
              <NIOBJNM />
              <TLEVEL>00</TLEVEL>
              <DRILLSTATE />
              <OPT />
              <SIGN />
              <ATTRIBUTES />
            </RRWS_SX_TUPLE>
          </SET>
        </RRWS_SX_AXIS_DATA>
        <RRWS_SX_AXIS_DATA>
          <AXIS>001</AXIS>
          <SET>
            <RRWS_SX_TUPLE>
              <TUPLE_ORDINAL>000000</TUPLE_ORDINAL>
              <CHANM>0IS_CSTATUS</CHANM>
              <CAPTION>Reopened</CAPTION>
              <CHAVL>1</CHAVL>
              <CHAVL_EXT />
              <NIOBJNM />
              <TLEVEL>00</TLEVEL>
              <DRILLSTATE />
              <OPT />
              <SIGN />
              <ATTRIBUTES />
            </RRWS_SX_TUPLE>
            <RRWS_SX_TUPLE>
              <TUPLE_ORDINAL>000000</TUPLE_ORDINAL>
              <CHANM>INJDESCRP</CHANM>
              <CAPTION>Disease</CAPTION>
              <CHAVL>ZDI</CHAVL>
              <CHAVL_EXT />
              <NIOBJNM />
              <TLEVEL>00</TLEVEL>
              <DRILLSTATE />
              <OPT />
              <SIGN />
              <ATTRIBUTES />
            </RRWS_SX_TUPLE>
            <RRWS_SX_TUPLE>
              <TUPLE_ORDINAL>000000</TUPLE_ORDINAL>
              <CHANM>0IS_CLAIMNT</CHANM>
              <CAPTION>Whithead</CAPTION>
              <CHAVL>0103619133</CHAVL>
              <CHAVL_EXT />
              <NIOBJNM />
              <TLEVEL>00</TLEVEL>
              <DRILLSTATE />
              <OPT />
              <SIGN />
              <ATTRIBUTES>
                <RRWS_S_ATTRIBUTES>
                  <ATTRINM>ZIPCD1ST</ATTRINM>
                  <CAPTION />
                  <ATTRIVL>L24</ATTRIVL>
                </RRWS_S_ATTRIBUTES>
              </ATTRIBUTES>
            </RRWS_SX_TUPLE>
            <RRWS_SX_TUPLE>
              <TUPLE_ORDINAL>000001</TUPLE_ORDINAL>
              <CHANM>0IS_CSTATUS</CHANM>
              <CAPTION>Reopened</CAPTION>
              <CHAVL>1</CHAVL>
              <CHAVL_EXT />
              <NIOBJNM />
              <TLEVEL>00</TLEVEL>
              <DRILLSTATE />
              <OPT />
              <SIGN />
              <ATTRIBUTES />
            </RRWS_SX_TUPLE>
            <RRWS_SX_TUPLE>
              <TUPLE_ORDINAL>000001</TUPLE_ORDINAL>
              <CHANM>INJDESCRP</CHANM>
              <CAPTION>Disease</CAPTION>
              <CHAVL>ZDI</CHAVL>
              <CHAVL_EXT />
              <NIOBJNM />
              <TLEVEL>00</TLEVEL>
              <DRILLSTATE />
              <OPT />
              <SIGN />
              <ATTRIBUTES />
            </RRWS_SX_TUPLE>
            <RRWS_SX_TUPLE>
              <TUPLE_ORDINAL>000001</TUPLE_ORDINAL>
              <CHANM>0IS_CLAIMNT</CHANM>
              <CAPTION>Smellie</CAPTION>
              <CHAVL>0103630524</CHAVL>
              <CHAVL_EXT />
              <NIOBJNM />
              <TLEVEL>00</TLEVEL>
              <DRILLSTATE />
              <OPT />
              <SIGN />
              <ATTRIBUTES>
                <RRWS_S_ATTRIBUTES>
                  <ATTRINM>ZIPCD1ST</ATTRINM>
                  <CAPTION />
                  <ATTRIVL>M21</ATTRIVL>
                </RRWS_S_ATTRIBUTES>
              </ATTRIBUTES>
            </RRWS_SX_TUPLE>
          </SET>
        </RRWS_SX_AXIS_DATA>
        <RRWS_SX_AXIS_DATA />
      </AXIS_DATA>
    </SOURCE>
  </asx:VALUES>
</asx:ABAP>

我要做的是产生如下结果: -

L24 Reopened    Disease Whithead  1 £ 12,131.80 £ 10,131.80
M21 Reopened    Disease Smellie   1 £ 28,116.90 £ 7,616.90

我已经使用了这里的样式表http://forums.asp.net/p/1449946/3309441.aspx,但我只能用它来制作它: -

Reopened    Disease     Whithead    L24
Reopened    Disease     Smellie     M21

这是我到目前为止的样式表: -

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

  <xsl:param name="col-count" select="3"/>

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <html lang="en">
      <head>
        <title>Example</title>
      </head>
      <body>
        <table border="1">
          <tbody>
              <tr bgcolor="#9acd32">
               <th>Claim Status</th>
               <th>Injury Desc</th>
               <th>Claimant</th>
              </tr>
              <xsl:apply-templates select="//AXIS_DATA/RRWS_SX_AXIS_DATA[2]/SET/RRWS_SX_TUPLE[position() mod $col-count = 1]" mode="row"/>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="RRWS_SX_TUPLE" mode="row">
    <tr>
      <xsl:apply-templates select=". | following-sibling::RRWS_SX_TUPLE[position() &lt; $col-count]" mode="cell"/>
    </tr>
  </xsl:template>
  <xsl:template match="RRWS_SX_TUPLE" mode="cell">
    <td><xsl:value-of select="CAPTION"/></td>
    <td><xsl:value-of select="ATTRIBUTES/RRWS_S_ATTRIBUTES/ATTRIVL"/></td>
  </xsl:template>
</xsl:stylesheet>

XML文件非常复杂我只是在这里展示了一些我想要做的事情。

最终,一旦我完成这项工作,我们的想法是将XML文件转换为另一个结构简单的XML文件: -

<data>
 <variable name="cc">
  <row>
   <column>L24</column>
   <column>Reopened</column>
   <column>Disease</column>
   <column>Whithead</column>
   <column>1</column>
   <column>12,131.80</column>
   <column>10,131.80</column>
  </row>
  <row>
   <column>M21</column>
   <column>Reopened</column>
   <column>Disease</column>
   <column>Smellie</column>
   <column>1</column>
   <column>28,116.90</column>
   <column>7,616.90</column>
  </row>
 </varible>
</data>

有人能指出我正确的方向还是更好还是给我提供必要的代码?

1 个答案:

答案 0 :(得分:0)

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:key name="tuples" match="RRWS_SX_AXIS_DATA[AXIS='001']//RRWS_SX_TUPLE" use="TUPLE_ORDINAL"/>
    <xsl:key name="cells" match="RRWS_S_CELL" use="floor(count(preceding-sibling::*) div 3)+1"/>
    <xsl:template match="/">
        <data>
            <variable name="cc">
                <!-- for each unique TUPLE_ORDINAL under AXIS='001' -->
                <xsl:for-each select="//RRWS_SX_TUPLE[generate-id()=generate-id(key('tuples',TUPLE_ORDINAL))]">
                    <row>
                        <!-- fetch the ATTRIVL (assuming only one) -->
                        <xsl:apply-templates select="key('tuples',TUPLE_ORDINAL)//ATTRIVL"/>
                        <!-- fetch all CAPTIONs -->
                        <xsl:apply-templates select="key('tuples',TUPLE_ORDINAL)/CAPTION"/>
                        <!-- fetch the 3 corresponding values: based on position() -->
                        <xsl:apply-templates select="key('cells',position())/FORMATTED_VALUE" mode="skipUnit"/>
                    </row>
                </xsl:for-each>
            </variable>
        </data>
    </xsl:template>
    <!-- output value in column element -->
    <xsl:template match="*">
        <column>
            <xsl:value-of select="."/>
        </column>
    </xsl:template>
    <!-- output value in column element, skipping the prefixed unit and space, if any -->
    <xsl:template match="*" mode="skipUnit">
        <column>
            <xsl:choose>
                <xsl:when test="contains(.,' ')">
                    <xsl:value-of select="substring-after(.,' ')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </column>
    </xsl:template>
</xsl:stylesheet> 

当应用于发布的文档时,产生此输出(根据请求):

<data>
  <variable name="cc">
    <row>
      <column>L24</column>
      <column>Reopened</column>
      <column>Disease</column>
      <column>Whithead</column>
      <column>1</column>
      <column>12,131.80</column>
      <column>10,131.80</column>
    </row>
    <row>
      <column>M21</column>
      <column>Reopened</column>
      <column>Disease</column>
      <column>Smellie</column>
      <column>1</column>
      <column>28,116.90</column>
      <column>7,616.90</column>
    </row>
  </variable>
</data>