XSLT 1.0-应用转换后的冗余标签

时间:2019-03-22 06:06:18

标签: xml xslt

我有一个示例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> 

我想创建一个xslt文件来处理Item是否具有“ Lon”节点,那么它必须创建Point对象,如果Item具有“ cx”节点,则必须创建“ Circle”对象。

为此,我创建了一个如下所示的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>

但是应用此xslt文件后,Circle对象中有<kml:Point/>之类的空标记,而Point对象中有<kml:Circle/>之类的空标记。我没有这些多余的标签。如果Item具有“ cx”节点,则转换后的Circle对象不应具有空标签。

这是我的示例输出;

<?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>

我已经尝试过xsl:if testxsl:when test函数,但是我无法再次对其进行管理。您能帮我解决我的问题吗?

1 个答案:

答案 0 :(得分:1)

<xsl:for-each select="ItemList/Item">的上下文中,您拥有<kml:Circle><kml:Point>元素,它们在for-each的每次迭代中创建。如果只想为具有<kml:Circle>个子元素的<Item>个元素生成<cx>,则需要相应地重组代码。

建议在默认情况下,<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>

在您现有的代码中,<kml:Circle><kml:Point>元素每次都会输出-只有每个元素的内容都受条件逻辑约束。因此,如果条件失败,该元素仍会输出-仅减去任何内容,生成不需要的空标签。

在上面的<xsl:choose>中,<kml:Circle><kml:Point>元素本身仅是有条件生成的,因此不会得到空元素。