1

I want to change an input xml file to a given format.

My input xml is:

 <Syncaaa xmlns="http://www.w3.org/TR/html4/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/TR/html4/ Syncaaa.xsd" releaseID="9.2" versionID="2.0.1">
        <eee>
            <Sender>
                <BOD>qwqqs</BOD>
                <ID>1222</ID>
                <Code>Success</Code>
            </Sender>
        </eee>
        <ddd>
            <bbb>
                <EntityId>100</EntityId>
            </bbb>
            <aaa type="xoxo">
                <Item>
                    <Status>true</Status>
                    <zzzz>O</zzzz>
                </Item>
                <Item>
                    <Status>false</Status>
                    <zzzz>1</zzzz>
                </Item>
            </aaa>
        </ddd>
    </Syncaaa>

From above xml, I want to extract below xml part.

<aaa type="xoxo">
    <Item>
        <Status>true</Status>
        <zzzz>O</zzzz>
    </Item>
    <Item>
        <Status>false</Status>
        <zzzz>1</zzzz>
    </Item>
</aaa>

And, tag names of the extracted xml should be changed as below.

  • <ddd> -> <Updatedaaa>
  • <Item> -> <UpdateItem>
  • <Status> -> <UpdatedStatus>
  • <zzzz> -> <Updatedzzzz>

Excepted output is like below.

<Updatedaaa>
            <UpdateItem>
                <UpdatedStatus>true</UpdatedStatus>
                <Updatedzzzz>0</Updatedzzzz>
            </UpdateItem>
            <UpdateItem>
                <UpdatedStatus>false</UpdatedStatus>
                <Updatedzzzz>1</Updatedzzzz>
            </UpdateItem>
        </Updatedaaa>

I tried to do that task using below xsl file.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

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

<xsl:template match="ddd"> 
  <UpdatedItem> 
    <xsl:apply-templates select="@*"/>
    <UpdatedStatus>
      <xsl:apply-templates select="Status"/>
    </UpdatedStatus>
    <Updatedzzzz>
      <xsl:apply-templates select="zzzz"/>
    </Updatedzzzz>
  </UpdatedItem> 
</xsl:template>


  </xsl:stylesheet>

But it didn't work with this xsl file. Please help me to identify the issue.

Thanks,

prime
  • 769
  • 1
  • 13
  • 27
  • yes you can do this,are you using any developer tool? – Vikram Saini Aug 04 '17 at 07:37
  • No, I just try on online tools. And, I tried to do this using some another SO questions. like https://stackoverflow.com/questions/22871731/change-xml-element-name-using-xslt – prime Aug 04 '17 at 07:39
  • Your question does not contain any XSLT code. Please make an attempt and describe where you are stuck. – Tomalak Aug 04 '17 at 07:50
  • Updated the question with XSLT code. – prime Aug 04 '17 at 08:10
  • Start here, then work on your other issues: https://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 – michael.hor257k Aug 04 '17 at 09:01

1 Answers1

2

There is a namespace associated in the input XML xmlns="http://www.w3.org/TR/html4/" which is missing in the XSL. Need to add the following in the XSL to use the namespace.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:html="http://www.w3.org/TR/html4/"
    exclude-result-prefixes="html">

The XML data can then be accessed by adding the namespace prefix to the XML nodes. There can be multiple solutions to get the desired output, like the one below.

XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:html="http://www.w3.org/TR/html4/"
    exclude-result-prefixes="html">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="/">
        <Updatedaaa>
            <xsl:for-each select="//*/html:Item">
                <UpdatedItem>
                    <UpdatedStatus>
                        <xsl:value-of select="html:Status" />
                    </UpdatedStatus>
                    <Updatedzzzz>
                        <xsl:value-of select="html:zzzz" />
                    </Updatedzzzz>
                </UpdatedItem>
            </xsl:for-each>
        </Updatedaaa>
    </xsl:template>
</xsl:stylesheet>

This XSLT when applied to the input XML shared produces the desired output

<?xml version="1.0" encoding="UTF-8"?>
<Updatedaaa>
    <UpdatedItem>
        <UpdatedStatus>true</UpdatedStatus>
        <Updatedzzzz>O</Updatedzzzz>
    </UpdatedItem>
    <UpdatedItem>
        <UpdatedStatus>false</UpdatedStatus>
        <Updatedzzzz>1</Updatedzzzz>
    </UpdatedItem>
</Updatedaaa>
Aniket V
  • 3,183
  • 2
  • 15
  • 27