请您帮助我了解如何做这个xml: xml看起来像
<a name="hr_1" id="hr">
<text>11</text>
</a>
<a name="hr_2" id="hr">
<text>12</text>
</a>
<a name="hre_1" id ="hre">
<text>11</text>
</a>
<a name="hre_2" id ="hre">
<text>12</text>
</a>预期输出:转换后的输出预期如下所示
<b name ="hr">
<value>11</value>
<value>12</value>
</b>
<b name ="hre">
<value>11</value>
<value>12</value>
</b>发布于 2019-06-11 17:34:42
来自评论:
非常感谢你..。我如何在XSLT1.0中做到这一点。此外,我还添加了一个标记id,因此我需要基于XSLT1.0中的id.Please帮助进行分组。
在XSLT1.0中,使用门窗群。我要做的是创建一个匹配所有text元素的键,并使用父元素的id属性.
XML
<doc>
<a name="hr_1" id="hr">
<text>11</text>
</a>
<a name="hr_2" id="hr">
<text>12</text>
</a>
<a name="hre_1" id ="hre">
<text>11b</text>
</a>
<a name="hre_2" id ="hre">
<text>12b</text>
</a>
</doc>XSLT1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kText" match="text" use="../@id"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each select="*/text[count(.|key('kText',../@id)[1])=1]">
<b name="{../@id}">
<xsl:apply-templates select="key('kText',../@id)"/>
</b>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>输出
<doc>
<b name="hr">
<text>11</text>
<text>12</text>
</b>
<b name="hre">
<text>11b</text>
<text>12b</text>
</b>
</doc>发布于 2019-06-11 12:04:19
这样看来是一个简单的分组任务,可以在XSLT 2或3中使用xsl:for-each-group解决。
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="a" group-by="substring-before(@name, '_')">
<b name="{current-grouping-key()}">
<xsl:copy-of select="current-group()/*"/>
</b>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>假设root是要分组的a元素的公共容器元素,根据需要对其进行调整。
https://stackoverflow.com/questions/56541437
复制相似问题