关于 group-adjacent 函数的问题已经有人问过了,我知道。但出于某种原因,我无法让它工作。
我在工作中接手了一个使用 xslt 的项目,而我的老板不理解学习 xslt 的学习曲线有多陡,以及它与过程编程有多么不同。
尽管如此,我还是下定决心要尽可能地学习它,但我仍然必须在学习的同时使用它。如果您能帮助解决以下问题,我们将不胜感激。
我有一个格式如下的 xml 文档:
<document xml:lang="EN">
<CRPg>text</CRPg>
<CRPg>text</CRPg>
<CRPg>text</CRPg>
<Section/> <!--section marker-->
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<Section/> <!--section marker-->
<Txt>body text</Txt>
<Txt>body text</Txt>
<Txt>body text</Txt>
<Txt>body text</Txt>
</document>
目前它基本上没有层次结构,包含标记以赋予它结构并包含在整个文件中。我正在尝试对所有 TOC 元素进行分组并将它们嵌套在一个包含元素中:
<document xml:lang="EN">
<CRPg>text</CRPg>
<CRPg>text</CRPg>
<CRPg>text</CRPg>
<Section/> <!--section marker-->
<Wrapper> <!-- create new element to wrap around TOC and TOCAuth -->
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
</Wrapper>
<Section/> <!--section marker-->
<Txt>body text</Txt>
<Txt>body text</Txt>
<Txt>body text</Txt>
<Txt>body text</Txt>
</document>
事实证明这比我预期的要困难。我尝试了以下代码的许多不同版本,并粘贴了看起来最不错误的那个。我意识到它仍然很糟糕:
<xsl:template match="*">
<xsl:for-each-group select="TOCChap | TOCAuth" group-adjacent="self::TOCChap | self::TOCAuth">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<wrapper>
<xsl:apply-templates select="current-group()"/>
</wrapper>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
提前致谢。
最佳答案
尝试失败的原因有以下三个:
您选择了错误的节点集;
您正在使用联合运算符
|
而不是逻辑运算符or
;没有名为
TOCAuth
的元素在您的文档中。
一旦您通过将说明更改为以下方式解决了这三个问题:
<xsl:for-each-group select="*" group-adjacent="self::TOCChap or self::TOCAu">
您将准确地看到您正在寻找的结果 - 请在此处查看完整的演示:http://xsltransform.net/jyH9rME
--
顺便说一句,您的情况几乎与 XSLT 2.0 规范中给出的使用 group-adjacent
的示例完全相同。 - 请参阅此处的最后一个示例:http://www.w3.org/TR/xslt20/#grouping-examples
附言
我不禁想知道你为什么不利用 <Section/>
将所有您的组放在包装器中的标记 - 例如,应用:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/document">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:for-each-group select="*" group-starting-with="Section">
<wrapper>
<xsl:apply-templates select="current-group()" />
</wrapper>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="Section"/>
</xsl:stylesheet>
您的输入将导致:
:<?xml version="1.0" encoding="utf-8"?>
<document xml:lang="EN">
<wrapper>
<CRPg>text</CRPg>
<CRPg>text</CRPg>
<CRPg>text</CRPg>
</wrapper>
<wrapper>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
<TOCChap>chapter title</TOCChap>
<TOCAu>chapter author</TOCAu>
</wrapper>
<wrapper>
<Txt>body text</Txt>
<Txt>body text</Txt>
<Txt>body text</Txt>
<Txt>body text</Txt>
</wrapper>
</document>
关于xml - 使用 xslt 的组相邻节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29724755/