[英]XSLT 1.0- Redundant tags after applying transformation
I have a sample XML file like below; 我有一个示例XML文件,如下所示;
<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. 我想创建一个xslt文件来处理如果Item具有“ Lon”节点,那么它必须创建一个Point对象,如果Item具有“ cx”节点,则它必须创建一个“ Circle”对象。
For this purpose , I created a xslt file like below; 为此,我创建了一个xslt文件,如下所示;
<?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. 但是在应用此xslt文件后,Circle对象中有
<kml:Point/>
类的空标签,而Point对象中有<kml:Circle/>
类的空标签。 I do not these redundant tags. 我没有这些多余的标签。 If Item has "cx" node, transformed Circle object should not have empty tag.
如果Item具有“ cx”节点,则转换后的Circle对象不应具有空标签。
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. 我已经尝试了
xsl:if test
和xsl:when test
功能,但是我无法再次管理它。 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 the for-each
. 在
<xsl:for-each select="ItemList/Item">
的上下文中,您具有在for-each
每次迭代中创建的<kml:Circle>
和<kml:Point>
元素。 If you only want to generate <kml:Circle>
for <Item>
elements that have <cx>
children, you need to restructure your code accordingly. 如果只想为具有
<cx>
子元素的<Item>
元素生成<kml:Circle>
,则需要相应地重组代码。
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: 建议在默认情况下,不要在
<kml:Placemark>
元素中创建<kml:Circle>
和<kml:Point>
元素-添加条件逻辑,例如:
<kml:Placemark>
<xsl:choose>
<!-- Our current context is an <Item> element. Does this have a <cx> child element? -->
<xsl:when test="cx">
<kml:Circle>
<...snip.../>
</kml:Circle>
</xsl:when>
<!-- Or does this have a <Lon> child element? -->
<xsl:when test="Lon">
<kml:Point>
<...snip.../>
</kml:Point>
</xsl:when>
<!-- Sanity check: generally good practice to include an `otherwise`
to define what to do in case of unexpected input. -->
<xsl:otherwise>
<...snip.../>
</xsl:otherwise>
</xsl:choose>
</kml:Placemark>
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. 在您现有的代码中,
<kml:Circle>
和<kml:Point>
元素每次都会输出-只有每个元素的内容都受条件逻辑约束。 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. 在上面的
<xsl:choose>
, <kml:Circle>
和<kml:Point>
元素本身仅是有条件生成的,因此不会得到空元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.