我正在尝试将“测试”元素添加到树的最后一个元素中,但它对每个元素都重复:
<root>
<Module>
<Router>
<Message>@OppositeMacAddress='0x00, 0x80, 0xC8, 0x3B, 0xC6, 0xA0'</Message>
@OppositeMacAddress='0x00, 0x80, 0xC8, 0x3B, 0xC6, 0xA0'
</Router>
@OppositeMacAddress='0x00, 0x80, 0xC8, 0x3B, 0xC6, 0xA0'
</Module>
</root>
这是我的 XML 源
<fired-rule context="Message[@Name='SPY_IN']"/>
<failed-assert
test="@OppositeIpAddress='192,168,1,2'"
location="/Module[1]/Router[1]/Message[1]">
<text>!Error! Erwarteter Wert:"192,168,1,2"</text>
</failed-assert>
它应该如下所示:
<root>
<Module>
<Router>
<Message>@OppositeMacAddress='0x00, 0x80, 0xC8, 0x3B, 0xC6, 0xA0'</Message>
</Router>
</Module>
</root>
这是我的 XSL 转换
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="//failed-assert/@location" />
</root>
</xsl:template>
<xsl:template match="@location">
<xsl:param name="path" select="." />
<xsl:variable
name="currentContext"
select="substring-before(substring-after($path,'/'), '[')"/>
<xsl:variable name="subContext" select="substring-after($path, ']')"/>
<xsl:element name="{$currentContext}">
<xsl:apply-templates select="current()[string($subContext)]">
<xsl:with-param name="path" select="$subContext"/>
</xsl:apply-templates>
<xsl:value-of select="../@test"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我怎么解决这个问题?