[英]Copying and adding a parent node with multiple children
我在上一篇文章中犯了一个错误。
我有像这样工作的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>
据我所知,除非有新的章节,否则无法将循环放入循环中。
尝试这样的事情:
<?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>
@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新手的一些注意事项:
我们记得在this
变量中当前章节的唯一ID。 另外,我们可以在[]
条件内使用generate-id(current())
。
preceding-sibling
轴以相反的文档顺序返回结果,因此[1]
元素是紧接前一个。
与其使用for-each
遍历根模板中的章节,不如使用books
和chapter
模板,有些人可能会说这是惯用的XSLT。 默认的根模板将负责调用books
模板。
我相信简单而有效的方法是使用密钥 :
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.