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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT - replace element's value with another element's value 2

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
I'm using a transform stylesheet to convert xml doc A into xml doc B. This is what's part of doc A:
Code:
<From>
  <PartnerInformation>
  <PartnerName>Company A</PartnerName> 
  <PartnerIdentifier Agency="ID">1</PartnerIdentifier> 
  </PartnerInformation>
</From>
<To>
  <PartnerInformation>
  <PartnerName>Company B</PartnerName> 
  <PartnerIdentifier Agency="ID">2</PartnerIdentifier> 
  </PartnerInformation>
</To>

In doc B, I need to flip From information with To information:
Code:
<From>
  <PartnerInformation>
  <PartnerName>Company B</PartnerName> 
  <PartnerIdentifier Agency="ID">2</PartnerIdentifier> 
  </PartnerInformation>
</From>
<To>
  <PartnerInformation>
  <PartnerName>Company A</PartnerName> 
  <PartnerIdentifier Agency="ID">1</PartnerIdentifier> 
  </PartnerInformation>
</To>

My experience with xsl is very limited. What I'm doing right now is this:
Code:
<xsl:when test="contains(local-name(),'From')">
  <PartnerInformation>
    <PartnerName>
      <xsl:value-of  select="/Header/To/PartnerInformation/PartnerName"/>
    </PartnerName>
    <PartnerIdentifier Agency="ID">
      <xsl:value-of select="/Header/To/PartnerInformation/PartnerIdentifier"/>
    </PartnerIdentifier>
  </PartnerInformation>
</xsl:when>
and I'm doing a similar thing for 'To'.
I'm sure there's a simplier way (without specifying all those tags: PartnerName, PartnerIdentifier). Can anyone tell me how to write this code in a more efficient way?
Thank you!
 
Try a couple templates that match From and To elements:
Code:
<xsl:template match="From">
<To><xsl:copy-of select="*"/></To>
</xsl:template>
[small]typed, not tested[/small]

Do analogous thing for match="To".

Tom Morrison
 
I forgot to mention that I'm using a template already, but it matches everything:
Code:
<xsl:template match="@* | node()">
I'm not sure how I can fit
Code:
<xsl:template match="From">
into that. The reason I'm using match="@* | node()" is the resulting doc B is almost identical to doc A (there are only little changes to be made), so I didn't want to write code for every single element.
 
You might still want to try my suggestion. You might be surprised to see what gets matched when. (If you are using one of the tools that allows you to set breakpoints, and step through the translation, do so.)

the resulting doc B is almost identical to doc A

Consider what xsl:copy-of does.

While you are at it, you should study what built-in rules do. Also, if you feel a need to force things a bit, you can always use template prioritization (though I find it very, very rare to need this).

Tom Morrison
 
The way to think is to devise a template to match only From-To pair in specific order. Like this.
[tt]
<xsl:template match="From[following-sibling::*[1][local-name()='To']]|To[preceding-sibling::*[1][local-name()='From']]">
<xsl:if test="local-name()='From'">
<xsl:copy>
<xsl:copy-of select="following-sibling::*[1]/*|following-sibling::*[1]/@*" />
</xsl:copy>
</xsl:if>
<xsl:if test="local-name()='To'">
<xsl:copy>
<xsl:copy-of select="preceding-sibling::*[1]/*|preceding-sibling::*[1]/@*" />
</xsl:copy>
</xsl:if>
</xsl:template>
[/tt]
This template of match the pair together has the advantage of preserving the document order of From with respect to To.
 
k5tm,
Thanks a lot for pointing me to the documentation - I've seen this document before but I didn't read all of it (that's a lot to go through and I wouldn't probably know what they are talking about until I actually have an issue, like right now).

tsuji,
Thank you so much for the code - it worked great!

I always get best responses on tek-tips.com. This is a wonderful forum!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top