【发布时间】:2015-07-16 14:38:50
【问题描述】:
我有如下的xml,
<doc>
<p type="Foot">
<link ref="http://www.facebook.com">
<c type="Hyperlink">www.facebook.com</c>
</link>
</p>
<p type="End">
<link ref="http://www.google.com">
<c type="Hyperlink">www.google.com.com</c>
</link>
</p>
</doc>
我需要做的是向<p> 节点添加动态id 属性,该节点具有"Foot" 和"End" 属性。所以我写了以下xsl,
<xsl:template match="p[@type='Foot' or @type='End']" priority="1">
<xsl:copy>
<xsl:attribute name="id">
<xsl:value-of select="'foot-'"/>
<xsl:number count="p[@type='Foot' or @type='End']" level="any"/>
</xsl:attribute>
<xsl:next-match/>
</xsl:copy>
</xsl:template>
它给了我以下结果
<doc>
<p id="foot-1"><p type="Foot">
<link ref="http://www.facebook.com">
<c type="Hyperlink">www.facebook.com</c>
</link>
</p></p>
<p id="foot-2"><p type="End">
<link ref="http://www.google.com">
<c type="Hyperlink">www.google.com.com</c>
</link>
</p></p>
</doc>
和上面的结果xml一样,它添加了重复
节点并添加新属性。但我需要的是这个,
<doc>
<p id="foot-1 type="Foot">
<link ref="http://www.facebook.com">
<c type="Hyperlink">www.facebook.com</c>
</link>
</p></p>
<p id="foot-2 type="End">
<link ref="http://www.google.com">
<c type="Hyperlink">www.google.com.com</c>
</link>
</p></p>
</doc>
如何通过更改 mu xsl 获得此输出?
【问题讨论】:
-
我没试过,但我认为你需要 copy-of 来复制你的属性,然后 apply-templates 而不是 next-match 以避免重新处理同一个节点。
-
@Giles,我为 这些转换不起作用。这些仅在我使用
时才有效。