Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Translate element names 1

Status
Not open for further replies.

razalas

Programmer
Apr 23, 2002
237
0
0
US
From what I read on the internet, this ought to be a simple task to accomplish, but I can't seem to get XSLT to do what I need...

Am receiving an XML message and I need to create an identical output XML message except with some of the element names replaced. In the input message below, I would like to rename element "Message" to "ReturnMsg" and "string" to "DataString".

Any help would be appreciated. Would also be interested in recommendations for a good reference site for XML/XPATH/XSLT on the web.

Code:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="[URL unfurl="true"]http://schemas.xmlsoap.org/soap/envelope/"[/URL] xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]
  <soap:Body>
    <GetShippersListResponse xmlns="[URL unfurl="true"]http://adsionline.com/ADSIShippingWS/">[/URL]
      <GetShippersListResult>
        <Success>true</Success>
        <Message />
        <ResponseList>
          <string>RSR</string>
        </ResponseList>
      </GetShippersListResult>
    </GetShippersListResponse>
  </soap:Body>
</soap:Envelope>

Code:
<?xml version="1.0"?>
<xsl:transform version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] xmlns:soapenv="[URL unfurl="true"]http://schemas.xmlsoap.org/soap/envelope/">[/URL]

<xsl:strip-space elements="*"/>

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

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

<xsl:template match="//Message">
<xsl:element name="ReturnMsg">
<xsl:value-of select="Message" />
</xsl:element>
</xsl:template>
<xsl:template match="//string">
<xsl:element name="DataString">
<xsl:value-of select="string" />
</xsl:element>
</xsl:template>

</xsl:transform>

Code:
<?xml version="1.0" encoding="UTF-8"?>
<GetShippersListResponse xmlns="[URL unfurl="true"]http://adsionline.com/ADSIShippingWS/">[/URL]
  <GetShippersListResult>
    <Success>true</Success>
    <Message/>
    <ResponseList>
      <string>RSR</string>
    </ResponseList>
  </GetShippersListResult>
</GetShippersListResponse>

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Hi David,


You have a couple issues, both involving namespace.

First add the additional namespace involved in the xsl:transform (by the way, xsl:transform is sort of archaic; use xsl:stylesheet instead.)
XML:
<xsl:transform version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] 
[indent][indent]xmlns:soapenv="[URL unfurl="true"]http://schemas.xmlsoap.org/soap/envelope/"[/URL]
xmlns:msg="[URL unfurl="true"]http://adsionline.com/ADSIShippingWS/">[/URL][/indent][/indent]

Then, you need to copy match the elements in a namespace-aware fashion, and use xsl:apply-templates to keep the transform going through all the nodes:

Code:
<xsl:template match="msg:Message">
<xsl:element name="ReturnMsg" namespace="[URL unfurl="true"]http://adsionline.com/ADSIShippingWS/">[/URL]
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

<xsl:template match="msg:string">
<xsl:element name="DataString" namespace="[URL unfurl="true"]http://adsionline.com/ADSIShippingWS/">[/URL]
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

This will get you started in the right direction.

Tom Morrison
Hill Country Software
 
The result:
XML:
<?xml version="1.0"?>
<GetShippersListResponse xmlns="[URL unfurl="true"]http://adsionline.com/ADSIShippingWS/">[/URL]
	<GetShippersListResult>
		<Success>true</Success>
		<ReturnMsg/>
		<ResponseList>
			<DataString>RSR</DataString>
		</ResponseList>
	</GetShippersListResult>
</GetShippersListResponse>

Tom Morrison
Hill Country Software
 
Tom,

many thanks! Those changes solved my issue. Now to see if I can apply the same concept to larger examples.

Don't know how you find time to watch all these forums, but thank you, thank you, thank you.


Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
David,

I don't watch very many. Our interests just happen to intersect on a couple...

If you have difficulty, feel free to ask again on this thread, as I have email notification set. If your longer examples contain proprietary information, you might want to obfuscate them before posting: faq426-7703

Tom Morrison
Hill Country Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top