复制并添加具有多个子节点的父节点

时间:2014-10-15 08:30:57

标签: xml xslt xpath xslt-1.0

我在上一篇文章中犯了错误。

我有这样工作的XML数据(这只是一个例子,章节和页面的数量都是可变的)。

<books>
 <chapter></chapter>
 <page></page>
 <page></page>
 <page></page>
 <chapter></chapter>
 <page></page>
 <page></page>
 <chapter></chapter>
 <page></page>
 <page></page>
 <page></page>
 <page></page>
</books>

我正在尝试重新创建它看起来像这样

<books>
 <book>
  <chapter></chapter>
  <page></page>
  <page></page>
  <page></page>
 </book>
 <book>
  <chapter></chapter>
  <page></page>
  <page></page>
 </book>
 <book>
  <chapter></chapter>
  <page></page>
  <page></page>
  <page></page>
  <page></page>
 </book>
</books>

据我所知,除非有新篇章,否则没有办法将循环置于循环中。

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <books>
            <xsl:for-each select="books/chapter">
                <!-- for each chapter node, record the number of preceding sibling,
                     for the first chapter there is none, so that is why I added +1,
                     so when I count all the preceding sibling chapter of page, I will
                     get a match -->
                <xsl:variable name="chapter_count" select="count(preceding-sibling::chapter) + 1"/>
                <book>
                    <xsl:copy-of select="."/>
                    <!-- This code will ensure that the following sibling pages that
                         will be copied has the same number of preceding sibling
                         chapter (for pages, notice that I did not add 1 in the
                         predicate). So for the first chapter node, $chapter_count is 1
                         and the number of preceding sibling chapters at page node is 1,
                         thus the match -->
                    <xsl:copy-of select="following-sibling::page[count(preceding-sibling::chapter) = $chapter_count]"/>
                </book>
            </xsl:for-each>
        </books>
    </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

@JoelMLamsen有正确的想法,他的解决方案可以正常工作,但可以简化一点,不使用计数。我们将尝试直接表示

的基本逻辑
  

对于每一章,请处理以下章节,紧接在前的章节就是这一章。

我们可以这样做:

<xsl:template match="books">
    <books>
        <xsl:apply-templates select="chapter"/>
    </books>
</xsl:template>

<xsl:template match="chapter">
    <xsl:variable name="this" select="generate-id()"/>
    <book>
        <xsl:copy-of select="."/>
        <xsl:copy-of 
            select="following-sibling::page[generate-id(preceding-sibling::chapter[1]) = $this]"/>
    </book>
</xsl:template>

如果您需要帮助了解病情,可以用英语阅读:

following-sibling           of all the following
::page                      page elements
[                           take the ones where
  generate-id(              the unique id of 
    preceding-sibling       of all its preceding
    ::chapter               chapter elements
    [1]                     (the most recent one)
  )
  =                         is equal to
  $this                     the unique id of the chapter we are on
]

对于XSLT较新的人来说有几点注意事项:

  1. 我们记得this变量中当前章节的唯一ID。或者,我们可以在generate-id(current())条件中使用[]

  2. preceding-sibling轴以反向文档顺序返回结果,因此[1]元素是前一个元素。

  3. 不是使用for-each在根模板中循环章节,而是使用bookschapter的模板,有些人可能会说这是更惯用的XSLT。默认的根模板将负责调用books模板。

答案 2 :(得分:0)

我相信简单而有效(!) - 这样做的方法是使用

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="page-by-chapter" match="page" use="generate-id(preceding-sibling::chapter[1])" />

<xsl:template match="/">
    <books>
        <xsl:for-each select="books/chapter">
            <book>
                <xsl:copy-of select=". | key('page-by-chapter', generate-id())"/>
            </book>
        </xsl:for-each>
    </books>
</xsl:template>

</xsl:stylesheet>
相关问题