XSL模板 - 需要帮助从行获取类名

时间:2015-01-03 05:37:53

标签: xml xslt

我有一个测试html表:

<table>
  <tr>
    <th style="width:10px;" />
    <th style="width:80px;">Name</th>
    <th style="width:350px;">Number</th>
    <th style="width:80px;">Date<br />2014-12-31</th>
  </tr>
  <tr class="parent" data-level="0">
    <td />
    <td class="Center">123-12345</td>
    <td>     Range</td>
    <td class="Number">
      <br />
    </td>
  </tr>
  <tr class="child" >
    <td>
      <div class="childNo"  />
    </td>
    <td class="Center">D</td>
    <td>     Test Description</td>
    <td class="Number">123456<br /></td>
  </tr>

</table>

我想要做的就是检查<TR>类是否是&#34; parent&#34;,如果是,则连接&#34; expando&#34;上课。所以它看起来像这样:

<table>
  <tr>
    <th style="width:10px;" />
    <th style="width:80px;">Name</th>
    <th style="width:350px;">Number</th>
    <th style="width:80px;">Date<br />2014-12-31</th>
  </tr>
  <tr class="parent expando" data-level="0">
   ....

我似乎无法使用xsl执行此操作。我不确定是否需要使用模板匹配。

任何帮助都将不胜感激。

提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以复制整个表格,只将类添加到tr class="parent"这样的所有<?xml version="1.0" encoding="UTF-8" ?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="tr[@class='parent']"> <xsl:copy> <xsl:attribute name="class" select="'parent expando'"/> <xsl:attribute name="data-level" select="@data-level"/> <xsl:apply-templates select="td"/> </xsl:copy> </xsl:template> </xsl:transform>

<table>
  <tr>
    <th style="width:10px;"></th>
    <th style="width:80px;">Name</th>
    <th style="width:350px;">Number</th>
    <th style="width:80px;">Date<br>2014-12-31
    </th>
  </tr>
  <tr class="parent expando" data-level="0">
    <td></td>
    <td class="Center">123-12345</td>
    <td>     Range</td>
    <td class="Number"><br></td>
  </tr>
  <tr class="child">
    <td>
      <div class="childNo"></div>
    </td>
    <td class="Center">D</td>
    <td>     Test Description</td>
    <td class="Number">123456<br></td>
  </tr>
</table>

输出:

<xsl:template match="tr[@class='parent']">

匹配模式tr的模板 使用

匹配具有类parent的所有<xsl:copy> <xsl:attribute name="class" select="'parent expando'"/> <xsl:attribute name="data-level" select="@data-level"/> <xsl:apply-templates select="td"/> </xsl:copy>
tr

复制整个class并将属性tr的值更改为两个类。

更新:作为附加要求,这也适用于parent,可以在课程<xsl:template match="tr[contains(@class,'parent')]"> <xsl:copy> <xsl:attribute name="class" select="concat(@class, ' expando')"/> <xsl:attribute name="data-level" select="@data-level"/> <xsl:apply-templates select="td"/> </xsl:copy> </xsl:template> 中添加其他课程。
以下调整处理:

tr

现在所有parent将匹配包含类expando的内容,并且作为class属性的新值,类<xsl:attribute name="class" select="concat(@class, ' expando')"/> 将添加到当前类值:

<xsl:attribute>

更新:注意到注释,这不是<xsl:attribute name="class"> <xsl:value-of select="concat(@class, ' expando')"/> </xsl:attribute> <xsl:attribute name="data-level"> <xsl:value-of select="@data-level"/> </xsl:attribute> 的有效XSLT 1.0语法 - 它应该是

Attribute select is not allowed on this element

这是使用在线XSLT处理器测试的,虽然XSLT之前已声明为1.0,但它并未抛出任何错误。正如评论中所提到的,使用XSLT 2.0处理器(Saxon 9.5.1.6)是错误的,在线版本中,它不会将所有错误和警告传递给用户。切换到XSLT 1.0处理器(Saxon 6.5)导致正确的错误class

虽然这是公认的答案,但在我看来,Lingamurthy CS给出的答案是更好/更清洁的方法,因为它只会改变必要的xsl:copy属性。我应该知道更好地保存一些不必要的{{1}}。

答案 1 :(得分:1)

您可以使用以下样式表,其中包含两个模板。首先,使用标识模板复制源文档中的所有节点。其次,仅更新其父级为@class的{​​{1}},并且如果&#34; parent&#34;则更新值,因此,无需担心tr元素或其他属性:< / p>

tr
相关问题