我有源文件:
<?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>
目标文件:
<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>
我认识到这种变换的三个主要规则......但我无法想象变换的方式:
城市中具有“目的地ID”的所有元素将成为“CITY”类中的元素子元素。甚至是元素之间映射的连接。
@name成为文本元素:只有“CITY”和“CITYLIST”再获得一层“<org><text><fill>@name</fill></text></org>
”。其余元素可以直接放置:'<text>@name</text>
'
如果元素中有另一个子元素,它将成为新元素。例如:对于“博物馆”,如果它具有元素“artmuseum”,则类名称变为“ARTMUSEUM”。而不是“博物馆”
问题是如何检查元素是否包含任何元素来调用相应的模板。因为这些子元素将成为新类,而不是其父元素。
答案 0 :(得分:0)
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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>
输出:
<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>