tags:

views:

21

answers:

1

I have the source document:

<?xml version="1.0" encoding="UTF-8" ?> 
<tourcompany id="CMP001" name= "pacific cmp">
<country id="FRA" name="France">
<citylist id="list001" name="CityofFRA">
<city id="CT001" name="Paris">
  <destination>PD1</destination> 
  <destination>PD2</destination> 
  <destination>PD3</destination> 
  <destination>PD4</destination> 
  <destination>PD5</destination> 
  </city>
<city id="CT002" name="Versailles">
  <destination>VD1</destination> 
  <destination>VD2</destination> 
  <destination>VD3</destination> 
  <destination>VD4</destination> 
  <destination>VD5</destination> 
    </city>
  </citylist>
  <zoo id="PD1" name="BurgerZoo" /> 
  <park id="PD2" name="partABC" /> 
  <church id="PD3" name="AtoZchurch" /> 
  <museum id="PD4" name="VANGT">
  <artmuseum/>
  </museum> 
 <museum id="PD5" name="WATER FALL"/>


  <direction_path id="PH123" from="PD1" to="PD3"/>

  <direction_path id="PH124" from="PD3" to="PD2"/>

  <direction_path id="PH125" from="PD2" to="PD4"/>
  <direction_path id="PH126" from="PD4" to="PD5"/>

<zoo id="VD1" name="GDF">
  <bigzoo /> 
</zoo>
<part id="VD2" name="KALA">
  <nationalpart/> 
 </part>
  <part id="VD3" name="Disneyalnd">
  <waterpart/> 
  </part>
<church id="VD4" name="SANT">
<museum id="VD5" name="alibaba">
  <historymuseum/>
  </museum>

  <direction_path id="PH001" from="VD1" to="VD2"/>

  <direction_path id="PH002" from="VD2" to="VD3"/>

  <direction_path id="PH003" from="VD3" to="VD5"/>

  <direction_path id="PH004" from="VD5" to="VD4"/>

 </country>  
 </tourcompany>

And the target document:

<org id='COUNTRY'  class = "COUNTRY">
   <org id="list001" class ="CITYLIST">
   <org><text><fill>CityofFRA</fill></text></org>
  <org id="CT001" class ="CITY" countryID="FRA">
      <org><text><fill>Paris</fill></text></org>
      <org id="PD1" class="ZOO" CityID="CT001">
           <text>BurgerZoo</text>
      </org>
             <org id="PD2" class="PART" CityID="CT001">
    <text>partABC</text>
      </org>
      <org id="PD3" class="CHURCH" CityID="CT001">
    <text>AtoZchurch</text>
      </org>
      <org id="PD4" class="ARTMUSEUM" CityID="CT001">
    <text>VANGT</text>
        </org>
        <org id="DIRECTION_PATH_CT001" class="DIRECTION_PATH">
    <org id="PH123" class="CONNECTION" source="PD1" target="PD3"/>  
     <org id="PH124" class="CONNECTION" source="PD3" target="PD2"/>  
     <org id="PH125" class="CONNECTION" source="PD2" target="PD4"/>  
     <org id="PH126" class="CONNECTION" source="PD4" target="PD5"/>  
   </org>    
   </org>  
   <org id="CT002" class ="CITY" countryID="FRA">
   <org><text><fill>Versailles</fill></text></org>
    <org id="VD1" class="ZOO" CityID="CT002">
       <text>GDF</text>
    </org>
    <org id="VD2" class="NATIONALPART" CityID="CT002">
       <text>KALA</text>
    </org>
    <org id="VD3" class="WATERPART" CityID="CT002">
        <text>Disneyalnd</text>
    </org>
    <org id="VD4" class="CHURCH" CityID="CT002">
       <text>SANT</text>
    </org>
    <org id="VD5" class="HISTORYMUSEUM" CityID="CT002">
     <text>alibaba</text>
    </org>
    <org id="DIRECTION_PATH_CT001" class="DIRECTION_PATH">
      <org id="PH001" class="CONNECTION" source="VD1" target="VD2"/>  
      <org id="PH002" class="CONNECTION" source="VD2" target="VD3"/>  
      <org id="PH003" class="CONNECTION" source="VD3" target="VD5"/>  
        <org id="PH004" class="CONNECTION" source="VD5" target="VD4"/>  
    </org>    
  </org>  
  </org> 
 </org>

I recognize three main rules for this transforms... but I cannot imagine the way to transform:

  1. All element that has 'destination id' inside city will become the element childs inside that "CITY" class. Even the connection that map between element.

  2. The @name become text elements: only "CITY" and "CITYLIST" get 1 one more layer '<org><text><fill>@name</fill></text></org>'. For the remaining elements can be put directly: '<text>@name</text>'

  3. If there are the other child inside the element, it will become the new element. For example: for the "museum" if it has element "artmuseum" then class name become "ARTMUSEUM" . Instead of "MUSEUM"

The problem is how to check whether the element contains any element or not to invoke the corresponding template. Because these child elements will become new class, not its parent.

A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:key name="ById" match="*" use="@id"/>
    <xsl:param name="pLowerCase" select="'qwertyuiopasdfghjklzxcvbnm'"/>
    <xsl:param name="pUpperCase" select="'QWERTYUIOPASDFGHJKLZXCVBNM'"/>
    <xsl:template match="country|citylist|city">
        <org id="{@id}"  class="{translate(name(),$pLowerCase,$pUpperCase)}">
            <xsl:apply-templates select="@name|citylist|city|destination"/>
        </org>
    </xsl:template>
    <xsl:template match="country/@name"/>
    <xsl:template match="citylist/@name|city/@name">
        <org>
            <text>
                <fill>
                    <xsl:value-of select="."/>
                </fill>
            </text>
        </org>
    </xsl:template>
    <xsl:template match="destination" name="destination">
        <org id="{.}"
                 class="{translate(name(key('ById',.)
                                          /descendant-or-self::*[last()]),
                                   $pLowerCase,$pUpperCase)}"
                 CityID="{../@id}">
            <text>
                <xsl:value-of select="key('ById',.)/@name"/>
            </text>
        </org>
    </xsl:template>
    <xsl:template match="destination[last()]">
        <xsl:call-template name="destination"/>
        <org id="DIRECTION_PATH_{../@id}" class="DIRECTION_PATH">
            <xsl:apply-templates 
             select="../../../direction_path[@from = current()/../destination]"/>
        </org>
    </xsl:template>
    <xsl:template match="direction_path">
        <org id="{@id}" class="CONNECTION" source="{@from}" target="{@to}"/>
    </xsl:template>
</xsl:stylesheet>

Output:

<org id="FRA" class="COUNTRY">
    <org id="list001" class="CITYLIST">
        <org>
            <text>
                <fill>CityofFRA</fill>
            </text>
        </org>
        <org id="CT001" class="CITY">
            <org>
                <text>
                    <fill>Paris</fill>
                </text>
            </org>
            <org id="PD1" class="ZOO" CityID="CT001">
                <text>BurgerZoo</text>
            </org>
            <org id="PD2" class="PARK" CityID="CT001">
                <text>partABC</text>
            </org>
            <org id="PD3" class="CHURCH" CityID="CT001">
                <text>AtoZchurch</text>
            </org>
            <org id="PD4" class="ARTMUSEUM" CityID="CT001">
                <text>VANGT</text>
            </org>
            <org id="PD5" class="MUSEUM" CityID="CT001">
                <text>WATER FALL</text>
            </org>
            <org id="DIRECTION_PATH_CT001" class="DIRECTION_PATH">
                <org id="PH123" class="CONNECTION" source="PD1" target="PD3" />
                <org id="PH124" class="CONNECTION" source="PD3" target="PD2" />
                <org id="PH125" class="CONNECTION" source="PD2" target="PD4" />
                <org id="PH126" class="CONNECTION" source="PD4" target="PD5" />
            </org>
        </org>
        <org id="CT002" class="CITY">
            <org>
                <text>
                    <fill>Versailles</fill>
                </text>
            </org>
            <org id="VD1" class="BIGZOO" CityID="CT002">
                <text>GDF</text>
            </org>
            <org id="VD2" class="NATIONALPART" CityID="CT002">
                <text>KALA</text>
            </org>
            <org id="VD3" class="WATERPART" CityID="CT002">
                <text>Disneyalnd</text>
            </org>
            <org id="VD4" class="CHURCH" CityID="CT002">
                <text>SANT</text>
            </org>
            <org id="VD5" class="HISTORYMUSEUM" CityID="CT002">
                <text>alibaba</text>
            </org>
            <org id="DIRECTION_PATH_CT002" class="DIRECTION_PATH">
                <org id="PH001" class="CONNECTION" source="VD1" target="VD2" />
                <org id="PH002" class="CONNECTION" source="VD2" target="VD3" />
                <org id="PH003" class="CONNECTION" source="VD3" target="VD5" />
                <org id="PH004" class="CONNECTION" source="VD5" target="VD4" />
            </org>
        </org>
    </org>
</org>
Alejandro