SolvedPrivate

XSLT Question

Posted on 2014-07-18
7
41 Views
Last Modified: 2016-02-26
Hi,

We are sending the below SOAP response and there is a requirement where the <Message> needs to look like                                                <Message xmlns="">

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<soapenv:Body>
		<StockAdjustmentMsg xmlns="http://www.abc-def.com/ns/schema">
			<Message>
				<Header>
					<ApiVersion>1.0.1</ApiVersion>
					<CreationTime>2014-07-18T11:30:17</CreationTime>
					<SenderId>ABCD</SenderId>
					<SenderAuth>18a51609-6ae2-4ea7-b5bd-bc8806fd</SenderAuth>
					<ReceiverId>DEF</ReceiverId>
					<MessageType>STOCK_ADJUSTMENT</MessageType>
				</Header>
				<Body>
					<MessageId>20140718113017_</MessageId>
					<StockAdjustment>
						<ProductNr>60000000006902</ProductNr>
						<Quantity>6</Quantity>
						<StockTypeTo>NEW</StockTypeTo>
						<Reason>510</Reason>
					</StockAdjustment>
				</Body>
			</Message>
		</StockAdjustmentMsg>
	</soapenv:Body>
</soapenv:Envelope>

Open in new window


Note that the XSLT needs to be general as StockAdjustmentMsg can also be different like OrderMsg etc. Also Body can repeat.

Thanks
Pradeep
0
Comment
Question by:Pradeep0308
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40205979
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*[local-name() = 'Message']">
        <Message>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </Message>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:Pradeep0308
ID: 40206259
Geert,

The output of the XSLT is below:

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<soapenv:Body>
		<StockAdjustmentMsg xmlns="http://www.abc-def.com/ns/schema">
			<Message xmlns="">
				<Header xmlns="http://www.abc-def.com/ns/schema">
					<ApiVersion>1.0.1</ApiVersion>
					<CreationTime>2014-07-18T11:30:17</CreationTime>
					<SenderId>ABCD</SenderId>
					<SenderAuth>18a51609-6ae2-4ea7-b5bd-bc8806fd</SenderAuth>
					<ReceiverId>DEF</ReceiverId>
					<MessageType>STOCK_ADJUSTMENT</MessageType>
				</Header>
				<Body xmlns="http://www.abc-def.com/ns/schema">
					<MessageId>20140718113017_</MessageId>
					<StockAdjustment>
						<ProductNr>60000000006902</ProductNr>
						<Quantity>6</Quantity>
						<StockTypeTo>NEW</StockTypeTo>
						<Reason>510</Reason>
					</StockAdjustment>
				</Body>
			</Message>
		</StockAdjustmentMsg>
	</soapenv:Body>
</soapenv:Envelope>

Open in new window

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Body>
            <StockAdjustmentMsg xmlns="http://www.abc-def.com/ns/schema">
                  <Message xmlns="">
                        <Header xmlns="http://www.abc-def.com/ns/schema">
                              <ApiVersion>1.0.1</ApiVersion>
                              <CreationTime>2014-07-18T11:30:17</CreationTime>
                              <SenderId>ABCD</SenderId>
                              <SenderAuth>18a51609-6ae2-4ea7-b5bd-bc8806fd</SenderAuth>
                              <ReceiverId>DEF</ReceiverId>
                              <MessageType>STOCK_ADJUSTMENT</MessageType>
                        </Header>
                        <Body xmlns="http://www.abc-def.com/ns/schema">
                              <MessageId>20140718113017_</MessageId>
                              <StockAdjustment>
                                    <ProductNr>60000000006902</ProductNr>
                                    <Quantity>6</Quantity>
                                    <StockTypeTo>NEW</StockTypeTo>
                                    <Reason>510</Reason>
                              </StockAdjustment>
                        </Body>
                  </Message>
            </StockAdjustmentMsg>
      </soapenv:Body>
</soapenv:Envelope>

It is adding namespace to Header and Body also. I only need Message to look like <Message xmlns="">. Below is how I need the output to look like:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<soapenv:Body>
		<StockAdjustmentMsg xmlns="http://www.abc-def.com/ns/schema">
			<Message xmlns="">
				<Header>
					<ApiVersion>1.0.1</ApiVersion>
					<CreationTime>2014-07-18T11:30:17</CreationTime>
					<SenderId>ABCD</SenderId>
					<SenderAuth>18a51609-6ae2-4ea7-b5bd-bc8806fd</SenderAuth>
					<ReceiverId>DEF</ReceiverId>
					<MessageType>STOCK_ADJUSTMENT</MessageType>
				</Header>
				<Body>
					<MessageId>20140718113017_</MessageId>
					<StockAdjustment>
						<ProductNr>60000000006902</ProductNr>
						<Quantity>6</Quantity>
						<StockTypeTo>NEW</StockTypeTo>
						<Reason>510</Reason>
					</StockAdjustment>
				</Body>
			</Message>
		</StockAdjustmentMsg>
	</soapenv:Body>
</soapenv:Envelope>

Open in new window

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Body>
            <StockAdjustmentMsg xmlns="http://www.abc-def.com/ns/schema">
                  <Message xmlns="">
                        <Header>
                              <ApiVersion>1.0.1</ApiVersion>
                              <CreationTime>2014-07-18T11:30:17</CreationTime>
                              <SenderId>ABCD</SenderId>
                              <SenderAuth>18a51609-6ae2-4ea7-b5bd-bc8806fd</SenderAuth>
                              <ReceiverId>DEF</ReceiverId>
                              <MessageType>STOCK_ADJUSTMENT</MessageType>
                        </Header>
                        <Body>
                              <MessageId>20140718113017_</MessageId>
                              <StockAdjustment>
                                    <ProductNr>60000000006902</ProductNr>
                                    <Quantity>6</Quantity>
                                    <StockTypeTo>NEW</StockTypeTo>
                                    <Reason>510</Reason>
                              </StockAdjustment>
                        </Body>
                  </Message>
            </StockAdjustmentMsg>
      </soapenv:Body>
</soapenv:Envelope>

Thanks
Pradeep
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40206721
Actually you asked
<Message xmlns="">
which is mechanical to say that Message is NOT in the namespace of the higher up element it would inherit the default namespace from
(you are basically switching back to the null namespace for this element)

Now you ask to take out the entire Mesaage including children, to the null namespace
(note that you are making it different elements

the xmlns="" will not appear if the parent of Message is in the NULL namespace

Will post improved code in a minute
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 40206722
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="*[local-name() = 'Message']">
        <Message>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()" mode="null-ns"/>
        </Message>
    </xsl:template>

    <xsl:template match="*" mode="null-ns">
        <xsl:element name="{local-name()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()" mode="null-ns"/>
        </xsl:element>
    </xsl:template>
    
    <xsl:template match="node()[not(self::*)]" mode="null-ns">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:Pradeep0308
ID: 40208533
Geert,

Many thanks. Could you explain me the last two template match what they are doing?

Regards
Pradeep
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40210232
The first one gets triggered on the Message element and creates a new one but only using the local name (essentialy removing the namespace) and continues processing in a mode. The second one picks up all elements in that mode and removes the namespace for all of them
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JSON  parse help 8 64
XML extra information 8 47
.NET XML to Dictionary - XML has repeating node names 8 29
How do i delete the last node in an xml in T-SQL 7 28
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question