Topic
4 replies Latest Post - ‏2013-05-08T18:04:25Z by Arnab_Ghosh
Arnab_Ghosh
Arnab_Ghosh
70 Posts
ACCEPTED ANSWER

Pinned topic XML transformation

‏2013-04-30T15:11:34Z |

HI,

I have a variable say "data" in one of my stylesheet which is basically an XML like this

<root>

  <child_A>value_A<child_A>

  <child_B>value_B<child_B>

                       .

                       .

                       .

   <child_Z>value_Z<child_Z>

</root>

I want to

  1. remove the node <child_A>
  2. add node <child_A_1>
  3. edit content of node <child_B> from value_B to value_B_1

Can anyone give me the xsl code of how to do this?

 

Thanks,

Arnab

  • swlinn
    swlinn
    1330 Posts
    ACCEPTED ANSWER

    Re: XML transformation

    ‏2013-04-30T15:23:06Z  in response to Arnab_Ghosh

    I'm doing this on the fly, but this would be done with a simple identity transformation:

    <!-- For the root, copy the root element, add the child_A_1 element to root, and then process the other children of root ->

    <xsl:template match="/">

        <xsl:copy>

           <child_A_1>child_A_1_value</child_A_1>

           <xsl:apply-templates select="*" />

        </xsl:copy>

    </xsl:template>

    <!-- do nothing with child_A which removes it -->

    <xsl:template match="child_A" />

    <!-- child_B copy the element but modify the value -->

    <xsl:template match="child_B">

        <xsl:copy>

            <xsl:text>value_B_1</xsl:text>

        </xsl:copy>

    </xsl:template>

    <!-- All other nodes, just copy them and their children if any -->

    <xsl:template match="*">

        <xsl:copy>

           <xsl:apply-templates select="*" />

        </xsl:copy>

    </xsl:template>

     

    Regards,

    Steve

    • Arnab_Ghosh
      Arnab_Ghosh
      70 Posts
      ACCEPTED ANSWER

      Re: XML transformation

      ‏2013-04-30T15:36:05Z  in response to swlinn

      Hi Steve,

      I have not done this transformation before, so asking another silly question. How will i tell detapower that it should work on the variable "data" which has this XML as its value. If I do what you mentioned above, then it will work on the XML it received as an input to the "Transform" action but i want it to work on a variable defined within the XSL for the Transform action. My XSL has something like this:

      <xsl:template match="/"> 

        <xsl:variable name="data">

            <dp:url-open response="binaryNode" timeout="3" target="{$GcacheServiceEndPoint}">
              <xsl:copy-of select="$request-xml"/>
            </dp:url-open>
          </xsl:variable>

      ........................<<<<<<<< I want to write a code here which will change the value of the variable "data">>>>>>>>>

      </xsl:template>

       

      • swlinn
        swlinn
        1330 Posts
        ACCEPTED ANSWER

        Re: XML transformation

        ‏2013-04-30T16:06:46Z  in response to Arnab_Ghosh

        Ah, I missed that ... in your match / template, add

        <xsl:variable name="updatedData">

            <xsl:apply-templates select="$data" mode="update" />

        </xsl:variable>

        Note that the mode of "update" can be anything, but on the templates I put in my previous post, add mode="update" to the match.  The key is the mode on the apply templates should match the mode on the template matches you want to run for your transformation, which will keep your two match / templates from conflicting.

        Regards,

        Steve