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,

HiddenFace
  • 699
  • 1
  • 10
  • 24

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,153
  • 2
  • 14
  • 26