I have a sample XML file like below;
<ItemList>
<Item>
<Name>1</Name>
<Lon>66.406180329538</Lon>
<Lat>35.7185924672465</Lat>
</Item>
<Item>
<Name>2</Name>
<cx>1</cx>
<cy>2</cy>
<rx>3</rx>
<ry>4</ry>
</Item>
</ItemList>
I want to create a xslt file that handles if Item has "Lon" node then it must create a Point object, if item has a "cx" node it must create a "Circle" object.
For this purpose , I created a xslt file like below;
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="var1_initial" select="."/>
<ItemList xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
<xsl:attribute name="xsi:noNamespaceSchemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance"></xsl:attribute>
<xsl:for-each select="ItemList/Item">
<xsl:variable name="var2_cur" select="."/>
<Item>
<CommonMetadata>
<xsl:for-each select="Name">
<xsl:variable name="var3_cur" select="."/>
<Description>
<xsl:value-of select="."/>
</Description>
</xsl:for-each>
<Geography>
<KMLRepresentation>
<kml:kml>
<kml:Folder>
<kml:Placemark>
<kml:Circle>
<xsl:for-each select="cx">
<xsl:variable name="var4_cur" select="."/>
<xsl:attribute name="cx">
<xsl:value-of select="number(.)"/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="cy">
<xsl:variable name="var5_cur" select="."/>
<xsl:attribute name="cy">
<xsl:value-of select="number(.)"/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="rx">
<xsl:variable name="var6_cur" select="."/>
<xsl:attribute name="r">
<xsl:value-of select="number(.)"/>
</xsl:attribute>
</xsl:for-each>
</kml:Circle>
<kml:Point>
<xsl:for-each select="Lon">
<xsl:variable name="var7_cur" select="."/>
<xsl:for-each select="$var2_cur/Lat">
<xsl:variable name="var8_cur" select="."/>
<kml:coordinates>
<xsl:value-of select="concat($var7_cur, ',', .)"/>
</kml:coordinates>
</xsl:for-each>
</xsl:for-each>
</kml:Point>
</kml:Placemark>
</kml:Folder>
</kml:kml>
</KMLRepresentation>
</Geography>
</CommonMetadata>
</Item>
</xsl:for-each>
</ItemList>
</xsl:template>
</xsl:stylesheet>
But after applying this xslt file there are empty tags like <kml:Point/>
in the Circle object and <kml:Circle/>
tag in the Point object. I do not these redundant tags. If Item has "cx" node, transformed Circle object should not have empty tag.
This is my sample output ;
<?xml version="1.0" encoding="UTF-8"?>
<ItemList>
<Item>
<CommonMetadata>
<Description>1</Description>
<Geography>
<KMLRepresentation>
<kml:kml>
<kml:Folder>
<kml:Placemark>
<kml:Circle/>
<kml:Point>
<kml:coordinates>66.406180329538,35.7185924672465</kml:coordinates>
</kml:Point>
</kml:Placemark>
</kml:Folder>
</kml:kml>
</KMLRepresentation>
</Geography>
</CommonMetadata>
</Item>
<Item>
<CommonMetadata>
<Description>2</Description>
<Geography>
<KMLRepresentation>
<kml:kml>
<kml:Folder>
<kml:Placemark>
<kml:Circle cx="1" cy="2" r="3"/>
<kml:Point/>
</kml:Placemark>
</kml:Folder>
</kml:kml>
</KMLRepresentation>
</Geography>
</CommonMetadata>
</Item>
</ItemList>
I have tried xsl:if test
and xsl:when test
functions but I could not manage it again. Could you please help about my problem ?
Within the context of
<xsl:for-each select="ItemList/Item">
, you have<kml:Circle>
and<kml:Point>
elements that are created in every iteration of thefor-each
. If you only want to generate<kml:Circle>
for<Item>
elements that have<cx>
children, you need to restructure your code accordingly.As a suggestion, within the
<kml:Placemark>
element, do not just create<kml:Circle>
and<kml:Point>
elements by default -- add conditional logic, such as:In your existing code, the
<kml:Circle>
and<kml:Point>
elements are output every time -- only the content of each is subject to conditional logic. So if the condition fails, the element is still output -- only minus any content, generating the empty tags you don't want.In the above
<xsl:choose>
, the<kml:Circle>
and<kml:Point>
elements themselves are only generated conditionally, so you won't get the empty elements.