虽然有很多类似标题的问题,但我找不到具体问题的答案。
假设我有一个xml树:
<input>
<a>
<b>
<p1/>
</b>
</a>
<a>
<b>
<p2/>
</b>
</a>
</input>我想把这个当成
<input>
<a>
<b>
<p1/>
<p2/>
</b>
</a>
</input>这种转换背后的想法是将一棵树,其中一个节点可以有多个同名的子节点,转换成一个更“格式良好”的树,其中每个节点只能有一个同名的子节点。(c.f.文件系统)。
我试着使用xslt-2的分组特性,但无法使递归工作。
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<output>
<xsl:apply-templates select="*"/>
</output>
</xsl:template>
<!-- this merges the children -->
<xsl:template name="merge" match="*">
<xsl:for-each-group select="child::*" group-by="local-name()">
<xsl:variable name="x" select="current-grouping-key()"/>
<xsl:element name="{$x}">
<xsl:copy-of select="current-group()/@*"/>
<xsl:apply-templates select="current-group()"/>
<xsl:copy-of select="text()"/>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>我发现问题在于,我要为current-group()中的每个节点分别应用模板,但我不知道如何首先“加入”这个集合并整体应用该模板。
发布于 2015-07-01 09:08:56
我认为您可以使用分组来设置一个函数,请参阅http://xsltransform.net/bdxtqM/1,它确实
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="mf">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:function name="mf:merge" as="node()*">
<xsl:param name="elements" as="element()*"/>
<xsl:for-each-group select="$elements" group-by="node-name(.)">
<xsl:copy>
<xsl:copy-of select="current-group()/@*"/>
<xsl:sequence select="mf:merge(current-group()/*)"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="/*">
<xsl:copy>
<xsl:sequence select="mf:merge(*)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/31126808
复制相似问题