我在处理从 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/