0

Consider the below example. I have to change the value of an attribute inside setData

<system>
    <Groups>
        <Group id="01" check="true">
            <name>Value</name>
            <age>test</age>
            <setData>
                <data type="module">module</data>
                <data enabled="true">true</data>
            </setData>
        </Group>
        <Group id="02" check="true">
            <name>Value</name>
            <age>test</age>
            <setData>
                <data type="module">module</data>
                <data enabled="true">true</data>
            </setData>
        </Group>
    </Groups>
</system>

I have to update 'enabled' attribute value to "false" in Group with id="01"

So the expected output is

<Group id="01" check="true">
    <name>Value</name>
    <age>test</age>
    <setData>
        <data type="module">module</data>
        <data enabled="false">false</data>
    </setData>
</Group>

Below given is the XL style sheet I have used. Can some one help on this?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />    
<xsl:param name="groupId" />
<xsl:param name="isEnabledStatus" />

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

<xsl:template match="setData" >
    <xsl:call-template name="identity" />
  <xsl:if test="@data = enabled">
    <plan id="{$groupId}">
     <xsl:attribute name="enabled"><xsl:value-of select="$isEnabledStatus"/></xsl:attribute> 
  </xsl:if>
</xsl:template>
</xsl:stylesheet>
2
  • So you write 'I have to update 'enabled' attribute value to "false" in Group with id="01"' but then you show us an expected output of <data enabled="true">true</data>. Commented Aug 3, 2018 at 9:10
  • Sorry, that was a mistake. Changed same on the expected output. Commented Aug 3, 2018 at 9:35

1 Answer 1

2

For your verbal description I would simply use

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

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

  <xsl:template match="Group[@id = '01']/setData/data[@enabled = 'true']">
      <data enabled="false">false</data>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bdxtqi

If you also need the parameters shown you will need to explain what you need them for and which values you want to set them to.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. This worked. "isEnabledStatus" will hold the true/false to be set. How to get the value from "isEnabledStatus" and place instead of hard coded values.
Do you need to toggle the value, i.e. if @enabled = 'true' you need to set it to false and if @enabled = 'false' you need to set it to true? Can it also occur that you want to run the stylesheet without setting the parameter and basically ignore it?
No I just wanted to place the parameter value there. What ever value is contained in parameter, it should get placed in those positions.
Do you need the parameter both to compare and to set the value? Setting is easy with a global <xsl:param name="isEnabledStatus"/> and then instead of <data enabled="false">false</data> you use <data enabled="{$isEnabledStatus}"><xsl:value-of select="$isEnabledStatus"/></data>. The only problem with XSLT 1 is using a parameter or variable reference in a match pattern (i.e. where I hard coded the comparison @enabled = 'true').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.