简体   繁体   中英

Multiple elements with same name in XML and want to change value of one specific element only based on one condition using xslt

Input 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>

My question is:
I want to update the element FLAG value as TRUE where element RAMID value matches with element GID value.

How can I achieve this using XSLT?

Use the identity template in combination with this template

<xsl:template match="FLAG[../GID = ../../../RAM/Details/RAMID]">
    <xsl:copy>TRUE</xsl:copy>
</xsl:template>

The identity template for XSLT-1.0 is

<!-- Identity template -->
<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*" />
  </xsl:copy>
</xsl:template> 

The combination of both achieves the desired result.
If you can XSLT-2.0 or above, you can replace the identity template with

<xsl:mode on-no-match="shallow-copy"/>    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM