我正在尝试根据后代元素的位置拆分元素树。 (特别是,我正在尝试解析 Adobe 的 IDML。)我希望能够转换如下所示的树:
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bar">
<Content>foo</Content>
<Br />
<Content>bar</Content>
</CharacterStyleRange>
<CharacterStyleRange style="bop">
<Content>baz</Content>
<Br />
<Hyperlink>
<Content>boo</Content>
<Br />
<Content>meep</Content>
</Hyperlink>
</ParagraphStyleRange>
分成 split 树:
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bar">
<Content>foo</Content>
</CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bar">
<Content>bar</Content>
</CharacterStyleRange>
<CharacterStyleRange style="bop">
<Content>baz</Content>
</CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bop">
<Hyperlink>
<Content>boo</Content>
</Hyperlink>
</CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bop">
<Hyperlink>
<Content>meep</Content>
</Hyperlink>
</CharacterStyleRange>
</ParagraphStyleRange>
然后我可以使用普通的 XSL 对其进行解析。 (编辑:我最初在其原始位置显示 <Br/>
标签,但它们是否存在并不重要,因为它们包含的信息现在由分割元素表示。我认为这可能更容易解决这个问题不用担心将它们留在里面。)
我尝试使用xsl:for-each-group
正如 XSLT 2.0 规范中所建议的(例如 <xsl:for-each-group select="CharacterStyleRange/*" group-ending-with="Br">
),但我无法弄清楚如何在树的每个级别应用它( <Br />
标签可以出现在任何级别,例如在 <Hyperlink>
元素内<CharacterStyleRange>
元素,它还限制我只能拥有适用于所选深度的模板。
编辑:我的示例代码仅显示需要拆分树的一个位置,但可以有任意数量的拆分点(但始终是相同的元素。)
编辑2:我添加了一些更详细的示例,以显示一些复杂性。
最佳答案
此 XSLT 1.0(当然还有 XSLT 2.0)转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="text()"/>
<xsl:template match="/">
<xsl:call-template name="Split">
<xsl:with-param name="pSplitters"
select="//Br"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="Split">
<xsl:param name="pSplitters"/>
<xsl:if test="$pSplitters">
<xsl:for-each select="$pSplitters[1]">
<xsl:call-template name="buildTree">
<xsl:with-param name="pLeafs" select=
"preceding-sibling::node()[not(descendant::Br)]"/>
</xsl:call-template>
<xsl:if test=
"not(following-sibling::node()//Br)">
<xsl:call-template name="buildTree">
<xsl:with-param name="pLeafs" select=
"following-sibling::node()"/>
</xsl:call-template>
</xsl:if>
<xsl:call-template name="Split">
<xsl:with-param name="pSplitters" select=
"$pSplitters[position() > 1]"/>
</xsl:call-template>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template name="buildTree">
<xsl:param name="pAncestors" select="ancestor::*"/>
<xsl:param name="pLeafs"/>
<xsl:choose>
<xsl:when test="not($pAncestors)">
<xsl:copy-of select="$pLeafs"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vtopAncestor" select="$pAncestors[1]"/>
<xsl:element name="{name($vtopAncestor)}"
namespace="{namespace-uri($vtopAncestor)}">
<xsl:copy-of select=
"$vtopAncestor/namespace::* | $vtopAncestor/@*"/>
<xsl:call-template name="buildTree">
<xsl:with-param name="pAncestors"
select="$pAncestors[position()>1]"/>
<xsl:with-param name="pLeafs" select="$pLeafs"/>
</xsl:call-template>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bar">
<Content>foo</Content>
<Br />
<Content>bar</Content>
</CharacterStyleRange>
<CharacterStyleRange style="bop">
<Content>baz</Content>
<Br />
<Hyperlink>
<Content>boo</Content>
<Br />
<Content>meep</Content>
</Hyperlink>
</CharacterStyleRange>
</ParagraphStyleRange>
产生想要的正确结果:
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bar">
<Content>foo</Content>
</CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bar">
<Content>bar</Content>
</CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bop">
<Content>baz</Content>
</CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bop">
<Hyperlink>
<Content>boo</Content>
</Hyperlink>
</CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange style="foo">
<CharacterStyleRange style="bop">
<Hyperlink>
<Content>meep</Content>
</Hyperlink>
</CharacterStyleRange>
</ParagraphStyleRange>
关于XSLT 在后代节点处分割树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5129188/