Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: work with different trees

From: MukulGandhi@-----------.---------.---
To: NULL
Date: 4/9/2005 8:33:00 PM
Please try this XSL -

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">
 
<xsl:output method="xml" indent="yes" /> 
 
<xsl:template match="/root">
  <part12>
    <res>
       <!-- merging part1/a[1]/b and part2/w[1]/ss -->
       <xsl:choose>
         <xsl:when test="count(part1/a[1]/b) > count(part2/w[1]/ss)">         
            <xsl:for-each select="part2/w[1]/ss">
              <xsl:variable name="pos" select="position()" />
              <r><xsl:value-of select="." /><xsl:text> 
</xsl:text><xsl:value-of select="../../../part1/a[1]/b[$pos]" /></r>
            </xsl:for-each>
            <xsl:for-each select="part1/a[1]/b[position() > 
count(part2/w[1]/ss)]">
              <r><xsl:value-of select="." /></r>
            </xsl:for-each>
         </xsl:when>
         <xsl:when test="count(part1/a[1]/b) < count(part2/w[1]/ss)">         
	   <xsl:for-each select="part1/a[1]/b">
             <xsl:variable name="pos" select="position()" />
             <r><xsl:value-of select="../../../part2/w[1]/ss[$pos]" 
/><xsl:text> </xsl:text><xsl:value-of select="." /></r>
           </xsl:for-each>
           <xsl:for-each select="part2/w[1]/ss[position() > 
count(../../../part1/a[1]/b)]">
             <r><xsl:value-of select="." /></r>
           </xsl:for-each>       
         </xsl:when>
         <xsl:otherwise>         
	   <xsl:for-each select="part1/a[1]/b">
             <xsl:variable name="pos" select="position()" />
             <r><xsl:value-of select="../../../part2/w[1]/ss[$pos]" 
/><xsl:text> </xsl:text><xsl:value-of select="." /></r>
           </xsl:for-each>       
         </xsl:otherwise>
       </xsl:choose>
       
       <!-- merging part1/a[2]/b and part2/w[2]/ss -->
       <xsl:choose>
         <xsl:when test="count(part1/a[2]/b) > count(part2/w[2]/ss)">
           <xsl:for-each select="part2/w[2]/ss">
             <xsl:variable name="pos" select="position()" />
             <r><xsl:value-of select="." /><xsl:text> 
</xsl:text><xsl:value-of select="../../../part1/a[2]/b[$pos]" /></r>
           </xsl:for-each>
           <xsl:for-each select="part1/a[2]/b[position() > 
count(part2/w[2]/ss)]">
             <r><xsl:value-of select="." /></r>
           </xsl:for-each>     
         </xsl:when>
         <xsl:when test="count(part1/a[2]/b) < count(part2/w[2]/ss)">
       	   <xsl:for-each select="part1/a[2]/b">
             <xsl:variable name="pos" select="position()" />
             <r><xsl:value-of select="../../../part2/w[2]/ss[$pos]" 
/><xsl:text> </xsl:text><xsl:value-of select="." /></r>
           </xsl:for-each>
           <xsl:for-each select="part2/w[2]/ss[position() > 
count(../../../part1/a[2]/b)]">
             <r><xsl:value-of select="." /></r>
           </xsl:for-each>            
         </xsl:when>
         <xsl:otherwise>
           <xsl:for-each select="part1/a[2]/b">
             <xsl:variable name="pos" select="position()" />
             <r><xsl:value-of select="../../../part2/w[2]/ss[$pos]" 
/><xsl:text> </xsl:text><xsl:value-of select="." /></r>
           </xsl:for-each>           
         </xsl:otherwise>
       </xsl:choose>
    </res>
  </part12>
</xsl:template>
   
</xsl:stylesheet>

When the above XSL is applied to XML -

<root>
  <part1>
    <a>
      <b>AA</b><b>BB</b>
    </a>
    <a>
      <b>EE</b><b>FF</b><b>GG</b>
    </a>
  </part1>
  <part2>
    <w>
      <ss>a</ss><ss>b</ss><ss>c</ss><ss>d</ss>
    </w>
    <w>
      <ss>e</ss><ss>f</ss><ss>g</ss>
    </w>
  </part2>
</root>

The output produced is -

<?xml version="1.0" encoding="UTF-8"?>
<part12>
   <res>
      <r>a AA</r>
      <r>b BB</r>
      <r>c</r>
      <r>d</r>
      <r>e EE</r>
      <r>f FF</r>
      <r>g GG</r>
   </res>
</part12>

Regards,
Mukul


"MB" wrote:

> Hello. Please help me with solving such problem:
> I have input xml like this:
> 
> <root>
>   <part1>
>     <a>
>       <b> AA <b> <b> BB <b>
>     </a>
>     <a>
>       <b> EE <b> <b> FF <b> <b> GG <b>
>     </a>
>   </part1>
> 
>   <part2>
>     <w>
>       <ss> a </ss> <ss> b </ss> <ss> c </ss> <ss> d </ss>
>     </w>
>     <w>
>       <ss> e </ss> <ss> f </ss> <ss> g </ss>
>     </w>
>   </part2>
> 
> </root>
> ---------------------------------------------------------
> I need such output:
> <part12>
>   <res>
>     <r>a AA </r>
>     <r>b BB </r>
>     ...
>     <r>h HH </r>
>   <res>
> <part12>
> 
> Problem is that when I need to concatenate values from different trees
> I don't know how get the value of corresponded element from another
> tree.
> 
> 


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent