Merge XML within a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blacksab
    New Member
    • Dec 2008
    • 5

    Merge XML within a file

    Hello, I have an xml-file like this:
    Code:
    <ROOT>
      <ORDER_LINES>
        <DATA>
          <LINE_NO>1</LINE_NO>
          <MISC_INFO/>
        </DATA>
        <DATA>
          <LINE_NO>2</LINE_NO>
          <MISC_INFO/>
        </DATA>
      </ORDER_LINES>
    
      <SORT>
        <LINE_NO sort_no="2">1</LINE_NO>
        <LINE_NO sort_no="1">2</LINE_NO>
      </SORT>
    </ROOT>
    In XSLT 1.0, how would I go about producing this output:

    Code:
      <ORDER_LINES>
        <DATA>
          <LINE_NO sort_no="1">2</LINE_NO>
          <MISC_INFO/>
        </DATA>
        <DATA>
          <LINE_NO sort_no="2">1</LINE_NO>
          <MISC_INFO/>
        </DATA>
      </ORDER_LINES>
    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
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I wouldn't try to merge the nodes, replace one with its counterpart.
    Code:
    <xsl:template match="LINE_NO[not(ancestor-or-self::SORT)]">
      <xsl:copy-of select="//LINE_NO[@sort_no = current()]"/>
    </xsl:template>
    regards

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Suggest sort on apply-templates at the data element. It's hard to tell without seeing the rest of your code.
      [code=xml]
      <xsl:template match="ORDER_LI NES">
      <xsl:copy>
      <xsl:apply-templates select="DATA">
      <xsl:sort select="//SORT/LINE_NO[.=current()/LINE_NO]/@sort_no"/>
      </xsl:apply-templates>
      </xsl:copy>
      </xsl:template>
      <xsl:template match="LINE_NO" >
      <xsl:copy-of select"//SORT/LINE_NO[.=current()]"/>
      </xsl:template>
      <xsl:template match="*"> <!-- default copy template -->
      <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
      </xsl:copy>
      </xsl:template>
      [/code]

      Comment

      • blacksab
        New Member
        • Dec 2008
        • 5

        #4
        Problem solved

        Wow, thank you all for quick replies. Problem is solved! Adopted both suggestions into my solution and it finally works.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          I'm pleased to hear that we could help.

          Comment

          Working...