user3345354
user3345354

Reputation: 105

Change value of node according to other node in XML using XSLT

I want to change value of some node. Condition for change value is as below:

If node name is "Name1" and if keyword is n1 then change to n2 If node name is "Name1" and if keyword is g1 then change to g2

<maindata>
<data>
 <keyword>n1</keywod>
 <keyword>g1</keyword>
</data>

<name>
<String>Name1</String>
</name>
</maindata>

Also I just change the value of above nodes all other content of file are just copy, so I write code for this as below:

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

So how can I write template to change node value as mention above?

Upvotes: 3

Views: 163

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

I think you want to add two templates:

<xsl:template match="maindata[name/String = 'Name1']/data/keyword[. = 'n1']">
  <keyword>n2</keyword>
</xsl:template>

<xsl:template match="maindata[name/String = 'Name1']/data/keyword[. = 'g1']">
  <keyword>g2</keyword>
</xsl:template>

Upvotes: 1

Related Questions