在xslt中从多个中创建一行

时间:2015-09-11 06:13:38

标签: xml xslt

我想在xslt表中从多个创建一行。 像这样:

 |Table name|Category| Id     |
 |----------+--------+--------|
 | Object 1 | Rooms  | 407843 |

但是现在我的xslt模板输入了:

 |Table name|Category| Id     |
 |----------+--------+--------|
 | Object 1 |        |        |
 |          | Rooms  |        |
 |          |        | 407843 |

xml文档如下所示:

<Object>
    <Property>
      <Property property="Table name">Object 1</Property>
      <Property property="Category">Rooms</Property>
      <Property property="Id">407843</Property>
    </Property>
</Object>

问题是我无法修改xml文件,所以它的结构与上面相同。

和xslt这样的文件:

<tr>        
      <td>
       <xsl:for-each select=".">
          <xsl:choose>                
            <xsl:when test="contains(@property, 'Table name')">
              <xsl:value-of select="."/>
            </xsl:when>
          </xsl:choose>
       </xsl:for-each>
      </td>

      <td>
       <xsl:for-each select=".">
          <xsl:choose>                
            <xsl:when test="contains(@property, 'Category')">
               <xsl:value-of select="."/>
            </xsl:when>
          </xsl:choose>
       </xsl:for-each>
      </td>

      <td>
       <xsl:for-each select=".">
          <xsl:choose>                
            <xsl:when test="contains(@property, 'Id')">
               <xsl:value-of select="."/>
            </xsl:when>
          </xsl:choose>
       </xsl:for-each>
      </td>
</tr>

1 个答案:

答案 0 :(得分:1)

  

有多个对象,它们具有相同的属性,但是   有时订单不一样。

让我们输入以下示例:

<强> XML

<Objects>
    <Object>
        <Property>
            <Property property="Table name">Table 1</Property>
            <Property property="Category">Cat 1</Property>
            <Property property="Id">001</Property>
        </Property>
    </Object>
    <Object>
        <Property>
            <Property property="Category">Cat 2</Property>
            <Property property="Id">002</Property>
            <Property property="Table name">Table 2</Property>
        </Property>
    </Object>
</Objects>

XSLT 1.0

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

<xsl:template match="/Objects">
    <table border="1">
        <tr>
            <th>Table</th>
            <th>Category</th>
            <th>Id</th>
        </tr>
        <xsl:for-each select="Object/Property">
            <tr>
                <td><xsl:value-of select="Property[@property='Table name']"/></td>
                <td><xsl:value-of select="Property[@property='Category']"/></td>
                <td><xsl:value-of select="Property[@property='Id']"/></td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>

结果(已呈现)

enter image description here