Connecting Tech Pros Worldwide Help | Site Map

Merge XML within a file

Newbie
 
Join Date: Dec 2008
Posts: 5
#1: Dec 10 '08
Hello, I have an xml-file like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <ROOT>
  3.   <ORDER_LINES>
  4.     <DATA>
  5.       <LINE_NO>1</LINE_NO>
  6.       <MISC_INFO/>
  7.     </DATA>
  8.     <DATA>
  9.       <LINE_NO>2</LINE_NO>
  10.       <MISC_INFO/>
  11.     </DATA>
  12.   </ORDER_LINES>
  13.  
  14.   <SORT>
  15.     <LINE_NO sort_no="2">1</LINE_NO>
  16.     <LINE_NO sort_no="1">2</LINE_NO>
  17.   </SORT>
  18. </ROOT>
  19.  
  20.  
In XSLT 1.0, how would I go about producing this output:

Expand|Select|Wrap|Line Numbers
  1.   <ORDER_LINES>
  2.     <DATA>
  3.       <LINE_NO sort_no="1">2</LINE_NO>
  4.       <MISC_INFO/>
  5.     </DATA>
  6.     <DATA>
  7.       <LINE_NO sort_no="2">1</LINE_NO>
  8.       <MISC_INFO/>
  9.     </DATA>
  10.   </ORDER_LINES>
  11.  
I hva tried differrent solutions for this over the past couple of days. But have failed in producing the desired output every time. The problem lies in merging the two nodes. (The tag xsl:sort helps me sort accordingly though).

Any help would be greatly appreciated.....
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Dec 10 '08

re: Merge XML within a file


I wouldn't try to merge the nodes, replace one with its counterpart.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="LINE_NO[not(ancestor-or-self::SORT)]">
  2.   <xsl:copy-of select="//LINE_NO[@sort_no = current()]"/>
  3. </xsl:template>
regards
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#3: Dec 10 '08

re: Merge XML within a file


Suggest sort on apply-templates at the data element. It's hard to tell without seeing the rest of your code.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="ORDER_LINES">
  2.   <xsl:copy>
  3.     <xsl:apply-templates select="DATA">
  4.       <xsl:sort select="//SORT/LINE_NO[.=current()/LINE_NO]/@sort_no"/>
  5.    </xsl:apply-templates>
  6.   </xsl:copy>
  7. </xsl:template>
  8. <xsl:template match="LINE_NO">
  9.    <xsl:copy-of select"//SORT/LINE_NO[.=current()]"/>
  10. </xsl:template>
  11. <xsl:template match="*"> <!-- default copy template -->
  12.   <xsl:copy>
  13.     <xsl:copy-of select="@*"/>
  14.     <xsl:apply-templates/>
  15.   </xsl:copy>
  16. </xsl:template>
  17.  
Newbie
 
Join Date: Dec 2008
Posts: 5
#4: Dec 11 '08

re: Merge XML within a file


Wow, thank you all for quick replies. Problem is solved! Adopted both suggestions into my solution and it finally works.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#5: Dec 11 '08

re: Merge XML within a file


I'm pleased to hear that we could help.
Reply

Tags
merge, sort, xml, xslt