xsl-list
[Top] [All Lists]

Grouping and renaming elements

2002-12-01 17:29:44
Hi

I have a problem trying to isolate groups of information in some xml. I would like to group the information below to separate the address from the users details, i.e. their address and then DOB and telephone number.

I have found a solution using "mode" in apply-templates but this solution isn't very elegant. Does anyone have any ideas or opinions if this is a valid way to group information or is there a better solution out there?

Thanks in advance,

Ronan


==>Xml to convert

<translated>
        <PersonaUnaCalle> Sunny Street </PersonaUnaCalle>
        <PersonaUnaCiudad> Dublin </PersonaUnaCiudad>
        <PersonaUnaDOB> 19660301 </PersonaUnaDOB>
        <PersonaUnaTel> 01035312174234 </PersonaUnaTel>
        <PersonaDosCalle> Windy Street </PersonaDosCalle>
        <PersonaDosCiudad> Cork </PersonaDosCiudad>
        <PersonaDosDOB> 19800502 </PersonaDosDOB>
        <PersonaDosTel> 0103532287213 </PersonaDosTel>
</translated>


==>Xml converted

<request>
        <PersonOne>
                <address>
                        <street> Sunny Street </street>
                        <city> Dublin </city>
                </address>
                <dob> 19660301 </dob>
                <telephone> 01035312174234 </telephone>
        </PersonOne>
        <PersonTwo>
                <address>
                        <street> Windy Street </street>
                        <city> Cork </city>
                </address>
                <dob> 19800502 </dob>
                <telephone> 0103532287213 </telephone>
        </PersonTwo>
</request>


==>XSL using mode to isolate repeating groups

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output method="xml"/>
 <xsl:template match="/translated">
   <request>
     <PersonOne>
       <address>

<xsl:apply-templates select="*[contains(local-name(), 'Ciudad') and starts-with(local-name(), 'PersonaUna')]"/>

<xsl:apply-templates select="*[contains(local-name(), 'Calle') and starts-with(local-name(), 'PersonaUna')]"/>

        <!--more lines in the address to match fields -->
       </address>

<xsl:apply-templates mode="other" select="*[starts-with(local-name(), 'PersonaUna')]"/>
     </PersonOne>

     <PersonTwo>
       <address>

<xsl:apply-templates select="*[contains(local-name(), 'Ciudad') and starts-with(local-name(), 'PersonaDos')]"/>

<xsl:apply-templates select="*[contains(local-name(), 'Calle') and starts-with(local-name(), 'PersonaDos')]"/>

         <!--more lines in the address to match fields -->

       </address>

<xsl:apply-templates mode="other" select="*[starts-with(local-name(), 'PersonaDos')]"/>

     </PersonTwo>
   </request>

 </xsl:template>


 <!-- address templates -->
 <xsl:template match="*[contains(local-name(), 'Calle')]">
   <Street>
     <xsl:value-of select="."/>
   </Street>
 </xsl:template>

 <xsl:template match="*[contains(local-name(), 'Ciudad')]">
   <city>
     <xsl:value-of select="."/>
   </city>
 </xsl:template>

 <!-- more template to match the lines in the address -->


 <!-- all other fields -->
<xsl:template mode="other" match="*[starts-with(local-name(), 'PersonaUna')] | *[starts-with(local-name(), 'PersonaDos')]" >

   <xsl:if test="contains( local-name(), 'DOB')">
     <dob>
       <xsl:value-of select="."/>
     </dob>
   </xsl:if>

   <xsl:if test="contains( local-name(),'Tel')">
     <telephone>
       <xsl:value-of select="."/>
     </telephone>
   </xsl:if>

<!-- more if's to match other fields to match the lines that aren't address types -->
 </xsl:template>

</xsl:stylesheet>


_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*. http://join.msn.com/?page=features/featuredemail


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>
  • Grouping and renaming elements, Ronan Phelan <=