![](/img/trans.png)
[英]In XSLT 1.0, how can I access attributes of a particular XML element when it is one of multiple elements with the same name?
[英]Multiple elements with same name in XML and want to change value of one specific element only based on one condition using xslt
输入XML
<?xml version="1.0"?>
<Response>
<TroubleResponse>
<Check>
<DStatus>
<GID>123456789</GID>
<FLAG/>
</DStatus>
<DStatus>
<GID>222233333</GID>
<FLAG/>
</DStatus>
<DStatus>
<GID>5555777788</GID>
<FLAG/>
</DStatus>
</Check>
<RAM>
<Details>
<RAMID>5555777788</RAMID>
</Details>
</RAM>
</TroubleResponse>
</Response>
我的问题是:
我想将元素标志值更新为TRUE,其中元素RAMID值与元素GID值匹配。
如何使用XSLT实现呢?
将身份模板与此模板结合使用
<xsl:template match="FLAG[../GID = ../../../RAM/Details/RAMID]">
<xsl:copy>TRUE</xsl:copy>
</xsl:template>
XSLT-1.0的身份模板为
<!-- Identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
两者的结合达到了预期的结果。
如果可以使用XSLT-2.0或更高版本,则可以将身份模板替换为
<xsl:mode on-no-match="shallow-copy"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.