-1

我有未知行数的 xml 表,如下所示:

  <?xml version="1.0" encoding="utf-8"?>
 <?xml-stylesheet type="text/xsl" href="Realone.xsl"?>
 <norm builddate="xxxxxxxxxxxxxx" doknr="xxxxxxxxxxxxxxxxxxx">
  <textdata>
  <text format="XML">
   <Content>
   <BR />
   <table frame="all" pgwide="1" tabstyle="tab2" tocentry="%yes;">
          <tgroup align="left" char="" charoff="50" cols="3" colsep="1" rowsep="1">
            <colspec colname="col1" colnum="1" />
            <colspec colname="col2" colnum="2" />
            <colspec colname="col3" colnum="3" />
            <thead valign="bottom">
              <row>
                <entry VJ="1" align="center" colname="col1" colsep="1" rowsep="1" valign="top">Color</entry>
                <entry VJ="1" align="center" colname="col2" colsep="1" rowsep="1" valign="top">Number1</entry>
                <entry VJ="1" align="center" colname="col3" valign="top">Number2</entry>
              </row>
            </thead>
            <tbody valign="top">
              <row>
                <entry VJ="1" align="left" colname="col1" colsep="1" rowsep="1" valign="top">blue</entry>
                <entry VJ="1" align="left" colname="col2" colsep="1" rowsep="1" valign="top">11-11-11</entry>
                <entry VJ="1" align="left" colname="col3" colsep="1" rowsep="1" valign="top">44</entry>
              </row>
              <row>
                <entry VJ="1" align="left" colname="col1" colsep="1" rowsep="1" valign="top">Red</entry>
                <entry VJ="1" align="left" colname="col2" colsep="1" rowsep="1" valign="top">22-22-22</entry>
                <entry VJ="1" align="left" colname="col3" colsep="1" rowsep="1" valign="top">33</entry>
              </row>
 </tbody>
 </tgroup>
</Content>
</text>

xsl 文件:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="html" encoding="ISO-8859-1" />
   <xsl:template match="/">
   <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>A</title>
   <link rel="stylesheet" href="../../format.css" type="text/css" />
   </head>
   <body>      
   <xsl:apply-templates select="normal"/>

   </body>
   </html>
   </xsl:template>


   <xsl:template match="norm">
   <xsl:apply-templates select="textdata" />
   </xsl:template>

   <xsl:template match="textdata">
   <xsl:if test="text">
   <xsl:apply-templates select="text"/>
   </xsl:if>
   </xsl:template>

   <xsl:template match="text">
   <xsl:if test="TOC">
   <xsl:apply-templates select="TOC"/>
   </xsl:if>
   <xsl:if test="Content">
   <xsl:apply-templates select="Content"/>
   </xsl:if> 
   </xsl:template>

    <xsl:template match="Content">
   <xsl:for-each select="P">
   <P> 
   <xsl:for-each select="text()">
   <xsl:value-of select="."/>
   </xsl:for-each>
   <xsl:choose> 
   <xsl:when test="DL"> 
   <xsl:apply-templates select="DL" /> 
   </xsl:when> 
   </xsl:choose>
   </P> 
   </xsl:for-each>
   </xsl:template> 

我希望输出看起来像:

Color   Number 1    Number2
Blue    11-11-11    44
Red     22-22-22    33

请注意: 1. 无法更改 xml 文件(我的输入)。2.不知道多少行。

如何使用 XSLT 进行管理?提前致谢。

4

1 回答 1

1

假设行和行的顺序由 指定colspec。你可以这样做:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="table">
        <table>
            <xsl:apply-templates select="tgroup/*/row" />
        </table>

    </xsl:template>

    <xsl:template match="row" >
        <xsl:variable name="row" select="." />
        <tr>
            <xsl:for-each select ="../../colspec">
                <td>
                    <xsl:value-of select="$row/entry[@colname=current()/@colname]"/>
                </td>
            </xsl:for-each>
        </tr>
    </xsl:template>

</xsl:stylesheet>

即使行丢失或顺序不同,这也应该有效。也可以添加xsl:sort到 colspec for-each

于 2013-06-19T14:05:15.563 回答