xml - xsl 将以下同级移动到当前标记
问题描述
我需要移动当前元素内的当前元素之后的以下兄弟姐妹。我不明白为什么我正在做的事情不起作用,你能帮我理解为什么吗?
我有这个 XML 输入:
<book>
<prelim>
<introd></introd>
<introd></introd>
<bibl></bibl>
<bibl></bibl>
</prelim>
我需要有这个输出:
<book>
<prelim>
<introd></introd>
<introd>
<bibl></bibl>
<bibl></bibl>
</introd>
</prelim>
它尝试了这个:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="*"><xsl:element name="{local-name()}"><xsl:apply-templates/></xsl:element>
</xsl:template>
<xsl:template match="introd">
<introd>
<xsl:apply-templates/>
<xsl:variable name="i"><xsl:value-of select="following-sibling::bibl/position()"/></xsl:variable>
<xsl:if test="following-sibling::*[$i]=following-sibling::bibl[$i]">
<xsl:apply-templates select="following-sibling::bibl[following-sibling::*[$i]=following-sibling::bibl[$1]]"></xsl:apply-templates>
</xsl:if>
</introd>
</xsl:template>
</xsl:stylesheet>
谢谢!玛丽亚
解决方案
我建议匹配prelim
然后使用xsl:for-each-group select="*" group-starting-with="introd"
:
<xsl:template match="prelim">
<xsl:copy>
<xsl:for-each-group select="*" group-starting-with="introd">
<xsl:copy>
<xsl:apply-templates select="node(), tail(current-group())"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
其余的可以通过身份转换处理,可以xsl:mode
在 XSLT 3 ( https://xsltfiddle.liberty-development.net/naZYrpT ) 中声明或在 XSLT 2 中拼写为模板。在 XSLT 2tail
中不可用,但您可以使用subsequence(current-group(), 2)
反而。
推荐阅读
- r - 使用 R 编程语言的书名列表的平均长度
- android - 创建签名的 apk 时构建失败
- sql - 如何从数组(postgresql)中的每个对象获取键?
- vb.net - 您好如何使用从文本框中获得的值更新标签的值?
- typescript - 类型 'a | 上不存在属性 'propertyTrue' b'
- android-sqlite - 查询一对多关系Android Room
- tesseract - 如何编辑“hocr”文件的文本并保存更正的文本
- css - 在伪元素的情况下,堆叠上下文究竟是如何工作的?
- python - Python selenium 在测试之间共享第二个浏览器
- database-design - 时序数据库的模式设计