I am using XSLT to generate my HTML.
I am having below xml and I want to write condition if there is only one city node inside a country node I want to write some condition, please see the below xml.
There are two xmls.
1) destinations.xml
<?xml version="1.0"?>
<list type="Destinations">
<resources location="include/xml/locations.xml">
<publication>232</publication>
</resources>
<destination id="594904" title="Maldives" url="/destinations_offers/destinations/asiapacific/maldives/maldives.aspx" thumbnail="/99/english/images/square_tcm481-594879.jpg" FeaturedDestination="true"> 开发者_如何学C
<city id="192513" />
</destination>
<destination id="594089" title="New Delhi" url="/destinations_offers/destinations/asiapacific/india/newdelhi.aspx" thumbnail="/99/english/images/sydney_tcm481-594346.jpg" FeaturedDestination="true" NewestDestination="true">
<city id="192460" />
</destination>
</list>
For eample In the above xml there is city id = 192513
for maldives and it is alone node in locations.xml this will be checked in below locations.xml and if that id is alone in that country node then I need to call specific condition.
<?xml version="1.0"?>
<list type="Locations">
<region id="192393" code="ASIA" name="Asia & the Pacific" shortname="Asia & the Pacific">
<country id="192395" code="AU" name="Australia" shortname="Australia">
<city id="192397" code="BNE" name="Brisbane" shortname="Brisbane">
<airport id="192399" code="BNE" name="Brisbane International Airport" shortname="Brisbane"></airport>
</city>
<city id="192409" code="SYD" name="Sydney" shortname="Sydney">
<airport id="192411" code="SYD" name="Kingsford Smith Airport" shortname="Sydney"></airport>
</city>
</country>
<country id="192511" code="MV" name="Maldives" shortname="Maldives">
<city id="192513" code="MLE" name="Male" shortname="Male">
<airport id="192515" code="MLE" name="Male International Airport" shortname="Male"></airport>
</city>
</country>
</region>
</list>
Please suggest!
Thanks.
Use:
count($vLocations/*/*/country[city[@id = $vDestCity/@id]]/city) = 1
In this expression $vLocations
is the XML document with top element <list type="Locations">
and $vDestCity is the <city>
element we are interested in from the XML document with top element <list type="Destinations">
To see this in action:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output method="text"/>
<my:locations>
<list type="Locations">
<region id="192393" code="ASIA"
name="Asia & the Pacific"
shortname="Asia & the Pacific">
<country id="192395" code="AU" name="Australia"
shortname="Australia">
<city id="192397" code="BNE" name="Brisbane"
shortname="Brisbane">
<airport id="192399" code="BNE"
name="Brisbane International Airport"
shortname="Brisbane">
</airport>
</city>
<city id="192409" code="SYD" name="Sydney"
shortname="Sydney">
<airport id="192411" code="SYD"
name="Kingsford Smith Airport"
shortname="Sydney">
</airport>
</city>
</country>
<country id="192511" code="MV" name="Maldives"
shortname="Maldives">
<city id="192513" code="MLE" name="Male"
shortname="Male">
<airport id="192515" code="MLE"
name="Male International Airport"
shortname="Male">
</airport>
</city>
</country>
</region>
</list>
</my:locations>
<xsl:variable name="vLocations"
select="document('')/*/my:locations"/>
<xsl:variable name="vDestCity1"
select="/*/destination/city[@id=192513]"/>
<xsl:variable name="vDestCity2"
select="/*/destination/city[@id=192397]"/>
<xsl:template match="/">
<xsl:value-of select=
"count($vLocations/*/*/country
[city[@id = $vDestCity1/@id]]/city
) = 1
"/>
:
<xsl:text/>
<xsl:value-of select=
"count($vLocations/*/*/country
[city[@id = $vDestCity2/@id]]/city
) = 1
"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided destinations.xml:
<list type="Destinations">
<resources location="include/xml/locations.xml">
<publication>232</publication>
</resources>
<destination id="594904" title="Maldives" url="/destinations_offers/destinations/asiapacific/maldives/maldives.aspx" thumbnail="/99/english/images/square_tcm481-594879.jpg" FeaturedDestination="true">
<city id="192513" />
</destination>
<destination id="594089" title="New Delhi" url="/destinations_offers/destinations/asiapacific/india/newdelhi.aspx" thumbnail="/99/english/images/sydney_tcm481-594346.jpg" FeaturedDestination="true" NewestDestination="true">
<city id="192460" />
</destination>
</list>
The wanted, correct result is produced:
true
:
false
for example Australia: count(//country[@id='192395']/city)
精彩评论