xsl-list
[Top] [All Lists]

temporary merge of multiple xml files for processing

2004-12-04 04:05:16
Hello Jay,

your answer seems not to be a solution. Let's modify the scenario so that 
the problem becomes clear:

first.xml:
<myelem>
   <subelem name="karl" age="10" />
   <subelem name="paul" age="12" />
   <otherfile name="second.xml" />
</myelem>

second.xml:
<parts>
  <subelem name="peter" age="13" />
  <subelem name="tom"   age="12" />
</parts>

Now I want to find out e.g. how many persons have the same age
as the currently inspected subelem.
According to your suggestion I should write something like:

<xsl:template match="/">
  <xsl:apply-templates select="/myelem/subelem"/>
  <xsl:apply-templates select="document('second.xml')/parts/subelem"/>
</xsl:template>

<xsl:template match="subelem">
  age=<xsl:value-of select="./@age" /> 
  number=<xsl:value-of select="count(//subelem[(_at_)age = ./@age])" />
</xsl:template>

would yield:
age=10 number=1    
age=12 number=1
age=13 number=1    
age=12 number=1

but what I want is:
age=10 number=1                 
age=12 number=2               
age=13 number=1                 
age=12 number=2

The problem is that your solution looks only into ONE (=the current) xml file 
but not
into all files simultaneously !
Again, how can I (temporarily) integrate all 
referred xml files into the first, "main" xml file?     

Matt

How can I insert an external xml file into a given first xml file              
  
and start then processing the target xml as base for xpath expressions?        
  
Seems difficult question but isn't. Assume the following sample:               
  
                                                                              
   
first.xml:                                                                     
  
<myelem>                                                                       
  
  <subelem aaa="1" />                                                         
 
  <subelem aaa="2" />                                                         
 
  <otherfile name="second.xml" />                                             
 
</myelem>                                                                      
  
                                                                              
   
second.xml:                                                                    
  
<parts>                                                                        
  
 <subelem aaa="3" />                                                          
 
 <subelem aaa="4" />                                                          
 
</parts>                                                                       
  
                                                                              
   
mysample.xslt:                                                                 
  
....                                                                           
  
<xsl:replaceotherfileby select="document(//otherfile/@name)" />                

<!-- temporary tree in memory should be now:                                   
  
<myelem>                                                                       
  
  <subelem aaa="1" />                                                         
 
  <subelem aaa="2" />                                                         
 
  <parts>                                                                     
   
    <subelem aaa="3" />                                                       
 
    <subelem aaa="4" />                                                       
 
  </parts>                                                                    
   
</myelem>                                                                      
  
-->                                                                            
  
<xsl:for-each ... select="//subelem">                                          

  <xsl:value-of select="./@aaa" />                                            
 
</xsl:for-each>                                                                
  
                                                                              
   
output:                                                                        
  
1                                                                              
  
2                                                                              
  
3                                                                              
  
4                                                                              
  
                                                                              
   
How do I code the function here called "replaceotherfileby"?                   
  
                                                                              
   
Second related question: Suppose in first.xml and second.xml                   
  
different prefixes are used for the same namespace.                            
  
Is your solution a pure textual replacement or are                             
  
the prefixes adjusted according to the refering common                         
  
namespace as well?                                                             
  
                                                                              
   
Thank you                                                                      
  
Matt                                                                           
  
---                                                                            
  
Date: Fri, 3 Dec 2004 12:52:05 -0600                                           
  
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com                           
                   
From: JBryant(_at_)s-s-t(_dot_)com                                             
             
Subject: Re: [xsl] including external XML file and process whole new tree      
  
                                                                              
   
I think you may be making things overly complex (or perhaps I                  
  
misunderstand your goals). You can use apply-templates to insert result        
  
elements from another document into the current result tree. So, the           
  
following would work:                                                          
  
                                                                              
   
<xsl:apply-templates select="document(//otherfile/@name)//subelem"/>           
  
                                                                              
   
Then toss the for-each and have a template for match="subelem".                
  
                                                                              
   
You could replace //subelem with the full path from the root of the            
  
inserted document to subelem. In your example, that would be                   
  
/parts/subelem. I assume that your example is a fragment of a much bigger      
  
document, in which you might not know the full path, so // will do for         
  
that case.                                                                     
  
                                                                              
   
The following stylesheet works on Saxon                                        
  
                                                                              
   
<?xml version="1.0" encoding="UTF-8"?>                                         
  
<xsl:stylesheet version="2.0"                                                  
  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>                              
  
                                                                              
   
  <xsl:template match="/">                                                    
   
    <xsl:apply-templates select="/myelem/subelem"/>                           
   
    <xsl:apply-templates select="document('test2.xml')/parts/subelem"/>       
   
  </xsl:template>                                                             
   
                                                                              
   
  <xsl:template match="subelem">                                              
   
    <xsl:value-of select="@aaa"/>                                             
   
  </xsl:template>                                                             
   
                                                                              
   
</xsl:stylesheet>                                                              
  
                                                                              
   
A couple caveats: In this case, the elements appear in sorted order            
  
because of the order of the elements in the file and the order of the          
  
apply-template statements. If you can't control the order of things in the     
  
input documents, you'll need to add sorting to the stylesheet. Also, with      
  
Saxon (8.1.1), I got all the output on one line. That's not a problem if       
  
you are wrapping your output in tags (HTML or XML), but it's a problem if      
  
you are relying on line breaks. As you can see, I also hardcoded the           
  
second file's name, just to save myself a bit of time.                         
  
                                                                              
   
HTH                                                                            
  
                                                                              
   
Jay Bryant                                                                     
  
Bryant Communication Services                                                  
  
-- 
___________________________________________________________
Sign-up for Ads Free at Mail.com
http://promo.mail.com/adsfreejump.htm


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>