使用具有不同数据类型的xsl将xml转换为不同的html表

时间:2017-05-01 08:51:42

标签: xml xslt html-table

需要将不同的项目转换为不同的表格。每次item-type更改时,都应该启动一个新表(上面有一些注释和不同的表标题)。需要保留排序顺序(seqnr)。

提出了一个解决方案,如果类型发生变化,它会将所有内容放在一个表中并发出新的表头。但我真正想做的是关闭前面的表格,发出一些bla-bla,并开始一个新的。我的所有尝试都导致了xsl-errors,因为需要/想要发出不匹配的标签(即:检测到类型更改时,发出</table>, some text, <table><th><tr>...</th>)。

据我所知,这是“初学者用程序语言思考”的结果,但仍然找不到诀窍。

(此示例适用于Firefox 51.0.1)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="#stylesheet"?>
<doc>
<xsl:stylesheet id="stylesheet" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="xsl:stylesheet"/>
<xsl:variable name="PersonHeader">
  <tr>
    <th>Name</th>
    <th>seqnr</th>
    <th>weight</th>
  </tr>
</xsl:variable>
<xsl:variable name="FruitHeader">
  <tr>
    <th>Fruit</th>
    <th>seqnr</th>
    <th>color</th>
    <th>size</th>
  </tr>
</xsl:variable>
<xsl:template match="Data">
  <html>
    <body>
      <table border="1">
        <xsl:for-each select="Item">
          <xsl:sort select="seqnr" data-type="number"/>
          <xsl:choose>
            <xsl:when test="type = 'Person'">
              <xsl:if test="not(preceding-sibling::Item[1]/type = type)">
                <xsl:copy-of select="$PersonHeader"/>
              </xsl:if>
              <tr>
                <td><xsl:value-of select="name"/></td>
                <td><xsl:value-of select="seqnr"/></td>
                <td><xsl:value-of select="weight"/></td>
              </tr>
            </xsl:when>
            <xsl:when test="type = 'Fruit'">
              <xsl:if test="not(preceding-sibling::Item[1]/type = type)">
                <xsl:copy-of select="$FruitHeader"/>
              </xsl:if>
              <tr>
                <td><xsl:value-of select="name"/></td>
                <td><xsl:value-of select="seqnr"/></td>
                <td><xsl:value-of select="color"/></td>
                <td><xsl:value-of select="size"/></td>
              </tr>
            </xsl:when>
          </xsl:choose>
        </xsl:for-each>
      </table>
    </body>
  </html>
</xsl:template>
</xsl:stylesheet>
<Data>
    <Item>
      <seqnr>1000</seqnr>
      <type>Fruit</type>
      <name>Banana</name>
      <color>yellow</color>
      <size>200</size>
    </Item>
    <Item>
      <seqnr>101</seqnr>
      <type>Person</type>
      <name>Hugo</name>
      <weight>70</weight>
    </Item>
    <Item>
      <seqnr>102</seqnr>
      <type>Person</type>
      <name>Bert</name>
      <weight>81</weight>
    </Item>
    <Item>
      <seqnr>103</seqnr>
      <type>Fruit</type>
      <name>Orange</name>
      <color>orange</color>
      <size>150</size>
    </Item>
    <Item>
      <seqnr>104</seqnr>
      <type>Fruit</type>
      <name>Apple</name>
      <color>red</color>
      <size>100</size>
    </Item>
    <Item>
      <seqnr>105</seqnr>
      <type>Person</type>
      <name>Charly</name>
      <weight>76</weight>
    </Item>
  </Data> 
</doc>

0 个答案:

没有答案