xslt - XSLT 重复生成的表

标签 xslt html-table

我在处理从 XSLT 生成的表时遇到困难。

我的 XML 是一个食谱。任何粗体行都将被格式化为表格。我的问题是该表在输出时重复。我尝试了各种分组方案,但没有解决任何问题。有没有人了解我所缺少的东西?谢谢。

XML:

<p>Some text goes here</p>
<p>Some text goes here</p>
<p>Some text goes here</p>
<p>Some text goes here</p>
<p>Ingredients</p>
<p><b>1 cup of flour</b></p>
<p><b>2 eggs</b></p>
<p><b>1/4 stick of butter</b></p>
<p><b>1/4 cup of sugar</b></p>
<p>Some text goes here</p>
<p>Some text goes here</p>
<p>Some text goes here</p>
<p>Some text goes here</p>

代码:

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

<xsl:strip-space elements="*" />

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="*/p/b" >
    <table>
      <xsl:apply-templates  select="//b" mode="test"/>
    </table>
</xsl:template>

<xsl:template match="//b" mode="test">
 <tr>
<td>
  <xsl:value-of select="substring-before(., ' ')" />
</td>
<td>
  <xsl:value-of select="substring-after(., ' ')" />
</td> 
</tr>
</xsl:template>

</xsl:stylesheet>

输出:

<p>Some text goes here</p>
<p>Some text goes here</p>
<p>Some text goes here</p>
<p>Some text goes here</p>

<p>Ingredients</p>
<p><table><tr><td>1</td><td>cup of flour</td></tr>
<tr><td>2</td><td>eggs</td></tr>
<tr><td>1/4</td><td>stick of butter</td></tr>
<tr><td>1/4</td><td>cup of sugar</td></tr>
</table></p>

<p><table><tr><td>1</td><td>cup of flour</td></tr>
<tr><td>2</td><td>eggs</td></tr>
<tr><td>1/4</td><td>stick of butter</td></tr>
<tr><td>1/4</td><td>cup of sugar</td></tr>
</table></p>

<p><table><tr><td>1</td><td>cup of flour</td></tr>
<tr><td>2</td><td>eggs</td></tr>
<tr><td>1/4</td><td>stick of butter</td></tr>
<tr><td>1/4</td><td>cup of sugar</td></tr>
</table></p>

<p><table><tr><td>1</td><td>cup of flour</td></tr>
<tr><td>2</td><td>eggs</td></tr>
<tr><td>1/4</td><td>stick of butter</td></tr>
<tr><td>1/4</td><td>cup of sugar</td></tr>
</table></p>

<p>Some text goes here</p>
<p>Some text goes here</p>
<p>Some text goes here</p>
<p>Some text goes here</p>

最佳答案

这个简单的转换(没有 xsl:if,没有 count(),没有 preceding-sibling:: 轴)是只需对提供的代码稍作修改:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="p[b][1]/b" >
        <table>
          <xsl:apply-templates  select="//b" mode="test"/>
        </table>
    </xsl:template>

    <xsl:template match="//b" mode="test">
     <tr>
    <td>
      <xsl:value-of select="substring-before(., ' ')" />
    </td>
    <td>
      <xsl:value-of select="substring-after(., ' ')" />
    </td>
    </tr>
    </xsl:template>

    <xsl:template match="p[b][position() > 1]"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<t>
    <p>Some text goes here</p>
    <p>Some text goes here</p>
    <p>Some text goes here</p>
    <p>Some text goes here</p>
    <p>Ingredients</p>
    <p><b>1 cup of flour</b></p>
    <p><b>2 eggs</b></p>
    <p><b>1/4 stick of butter</b></p>
    <p><b>1/4 cup of sugar</b></p>
    <p>Some text goes here</p>
    <p>Some text goes here</p>
    <p>Some text goes here</p>
    <p>Some text goes here</p>
</t>

产生想要的结果:

<t>
   <p>Some text goes here</p>
   <p>Some text goes here</p>
   <p>Some text goes here</p>
   <p>Some text goes here</p>
   <p>Ingredients</p>
   <p>
      <table>
         <tr>
            <td>1</td>
            <td>cup of flour</td>
         </tr>
         <tr>
            <td>2</td>
            <td>eggs</td>
         </tr>
         <tr>
            <td>1/4</td>
            <td>stick of butter</td>
         </tr>
         <tr>
            <td>1/4</td>
            <td>cup of sugar</td>
         </tr>
      </table>
   </p>
   <p>Some text goes here</p>
   <p>Some text goes here</p>
   <p>Some text goes here</p>
   <p>Some text goes here</p>
</t>

关于xslt - XSLT 重复生成的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9011257/

相关文章:

html - 我怎样才能说服 IE 遵守我的明确指示,使表格列 X 像素宽?

CSS 无法在表格上工作

xslt - 如何在 .NET 4.0 中使用 XPath 2.0 方法?

xslt 将 utf-8 字符转换为不同的编码

html - 如何知道何时在嵌套元素上放置结束标记

jQuery - 如何检查输入是否存在?

javascript - 带有删除行选项的动态表

javascript - 将类应用于前 5 行和后 5 行

xslt - 如何对XSLT模板的输出进行第二次转换

xml - 使用 XQUERY 从 XML 中消除单个元素类型