XLST 1.0 拆分节点 [英] XLST 1.0 Split node
本文介绍了XLST 1.0 拆分节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在
处拆分这个节点:
I want to split this node at <foo>
:
<p>
<span>This is before foo.</span>
<foo>inside foo</foo>
<span>This is after foo.</span>
</p>
我想要的结果是
<p>
<span>This is before foo.</span>
</p>
<foo>inside foo</foo>
<p>
<span>This is after foo.</span>
</p>
但是我得到了
<p>
<span>This is before foo.</span>
<p>
<span>This is before foo.</span>
</p>
<foo>inside foo</foo>
<p>
<span>This is after foo.</span>
</p>
<span>This is after foo.</span>
</p>
这是我正在尝试使用的样式表:
And here's the style sheet I'm trying with:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="p/foo">
<xsl:element name="p">
<xsl:apply-templates select="preceding-sibling::node()"/>
</xsl:element>
<foo>
<xsl:apply-templates/>
</foo>
<xsl:element name="p">
<xsl:apply-templates select="following-sibling::node()"/>
</xsl:element>
</xsl:template>
</xsl:transform>
我怎样才能摆脱那些额外的输出?
How can I get rid of that extra output?
举一个更现实的例子.请参阅 http://xsltransform.net/pNP88xQ/2
Gave a more realistic example. See http://xsltransform.net/pNP88xQ/2
推荐答案
-- 针对您编辑的问题进行了编辑 --
这在 XSLT 1.0 中不是一件容易完成的任务(在 XSLT 2.0 中使用 xsl:for-each-group
更容易完成).
This is not an easy task to accomplish in XSLT 1.0 (it's much easier to do in XSLT 2.0 using xsl:for-each-group
).
一种可能的方法是一种称为兄弟递归的方法:
One possible approach is a method known as sibling recursion:
XSLT 1.0
<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" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<!-- start sibling recursion -->
<xsl:apply-templates select="node()[1]"/>
</xsl:template>
<!-- first node in a group -->
<xsl:template match="p/node()[not(self::foo)]">
<p>
<xsl:call-template name="identity"/>
<!-- collect the following node in this group -->
<xsl:apply-templates select="following-sibling::node()[1][not(self::foo)]" mode="collect"/>
</p>
<!-- continue recursion with the following divider -->
<xsl:apply-templates select="following-sibling::foo[1]"/>
</xsl:template>
<!-- other nodes in a group -->
<xsl:template match="node()" mode="collect">
<xsl:call-template name="identity"/>
<!-- collect the following node in this group -->
<xsl:apply-templates select="following-sibling::node()[1][not(self::foo)]" mode="collect"/>
</xsl:template>
<xsl:template match="foo">
<xsl:call-template name="identity"/>
<!-- restart sibling recursion -->
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
</xsl:stylesheet>
这篇关于XLST 1.0 拆分节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文