INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Translate element names

Translate element names

(OP)
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 --> Input

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

CODE --> XSLT

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

<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 --> Output

<?xml version="1.0" encoding="UTF-8"?>
<GetShippersListResponse xmlns="http://adsionline.com/ADSIShippingWS/">
  <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

RE: Translate element names

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.)

CODE --> XML

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:msg="http://adsionline.com/ADSIShippingWS/">

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

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

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

This will get you started in the right direction.

Tom Morrison
Hill Country Software

RE: Translate element names

The result:

CODE --> XML

<?xml version="1.0"?>
<GetShippersListResponse xmlns="http://adsionline.com/ADSIShippingWS/">
	<GetShippersListResult>
		<Success>true</Success>
		<ReturnMsg/>
		<ResponseList>
			<DataString>RSR</DataString>
		</ResponseList>
	</GetShippersListResult>
</GetShippersListResponse> 

Tom Morrison
Hill Country Software

RE: Translate element names

(OP)
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

RE: Translate element names

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: How can I obscure my XML before posting?

Tom Morrison
Hill Country Software

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close