xml - 使用 xslt 的组相邻节点

标签 xml xslt xslt-2.0 xsl-grouping

关于 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>

提前致谢。

最佳答案

尝试失败的原因有以下三个:

  1. 您选择了错误的节点集;

  2. 您正在使用联合运算符 |而不是逻辑运算符 or ;

  3. 没有名为 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/

相关文章:

xslt - XSLT 中的动态变量

xslt - XSL 变量用法

xml - 在xsl中的节点和子节点中将文本转换为小写

java - JPA XML 映射文件可以很好地解析,但在作为 JUnit 测试运行时无法解析

xslt - 如何通过模板xslt从子节点获取祖 parent 节点的属性?

PHP:获取xml的属性值

java - XSLT:获取已声明实体的 URI

xslt - 撒克逊错误 "XPTY0019: Required item type of first operand of '/' is node(); supplied value has item type xs:string"

java - 任何人都有一个带有可扩展 ListView 的 xml 解析器

python - 如何使用 Python 将 XML 中的所有信息放入字典中