【发布时间】:2018-09-05 20:30:02
【问题描述】:
我有一个我一直在玩的测试文件,我正在尝试包装 中。如果我将模板匹配更改为 root ,我可以让它工作,但我会失去其他一切。有什么想法吗?提前致谢。这是我目前拥有的 xml 和 xsl。
xml 输入
<?xml version="1.0" encoding="UTF-8"?>
<document>
<chapter>
<title toc_id="new">An new story</title>
<para>New stories are different</para>
<listitem class="ol">Here is one list item</listitem>
<listitem class="ol">Here is two list item</listitem>
<listitem class="ol">Here is three list item</listitem>
<section>
<title toc_id="h2-intro-1">Introduction H1</title>
<para>Upon a time ....</para>
<title toc_id="h2-intro-2">Introduction H2</title>
<para>... there was a young prince. http://www.stuff.com, here's a website</para>
</section>
</chapter>
</document>
对于 xslt:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"
doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="listitem">
<xsl:for-each-group select="." group-by="@class">
<ol>
<xsl:for-each select=".">
<li>
<p>
<xsl:value-of select="."/>
</p>
</li>
</xsl:for-each>
</ol>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
我正在寻找的输出是这样的:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<document>
<chapter>
<title toc_id="new">An new story</title>
<para>New stories are different</para>
<ol>
<li>
<p>Here is one list item</p>
</li>
<li>
<p>Here is two list item</p>
</li>
<li>
<p>Here is three list item</p>
</li>
</ol>
<section>
<title toc_id="h2-intro-1">Introduction H1</title>
<para>Upon a time ....</para>
<title toc_id="h2-intro-2">Introduction H2</title>
<para>... there was a young prince. http://www.stuff.com, here's a website</para>
</section>
</chapter>
</document>
【问题讨论】:
-
那么您需要将相邻的
listitem元素包装在一起还是只包装具有特定class属性或相同类的那些?是否也可以有混合内容,即元素节点之间的非空白文本节点?一般来说,这似乎是match="*[listitem]"上的一项任务,然后是for-each-group group-adjacent="boolean(self::listitem)"可能还有一个嵌套的for-each-group group-by="@class"。 -
不,我试图弄清楚如何包装相似的元素,但无法完全理解 for-each-group 的工作原理。