动态生成kml时出现名称空间问题

时间:2019-05-07 13:49:12

标签: xslt kml xspec

我正在使用测试驱动的开发过程来创建一些xslt以动态创建kml数据。 使用xslt时,我使用xspec来管理测试。 而且我遇到了默认的名称空间问题。

xsl样式表具有大量的名称空间声明

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    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"
    exclude-result-prefixes="xs xd kml gx atom"
    version="1.0">

xpec测试文件具有更简单的声明

<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec" 

xslt代码的一部分将大量kml样式元素复制到输出流中,例如

<StyleMap id="msn_ylw-pushpin">
    <Pair>
        <key>normal</key>
        <styleUrl>#sn_ylw-pushpin</styleUrl>
    </Pair>
    <Pair>
        <key>highlight</key>
        <styleUrl>#sh_ylw-pushpin</styleUrl>
    </Pair>
</StyleMap>

这是在元素中定义的。

在xspec测试文件中,我指定了期望的kml,然后将这段代码复制并粘贴到适当的元素中。

但是测试失败,因为生成的内容包含空的 xmlns =“” 命名空间声明,例如

XSPEC file

<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec" stylesheet="KMLLibraryTest.xsl">
    <x:scenario label="just a test">
        <x:call template="make-ghost-namespace"/>
        <x:expect label="Test output">
            <kml xmlns="http://www.opengis.net/kml/2.2" 
                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">
                <Document>
                    <name>Las Salinetas Port Limits</name>
                    <StyleMap id="msn_ylw-pushpin">
                        <Pair>
                            <key>normal</key>
                            <styleUrl>#sn_ylw-pushpin</styleUrl>
                        </Pair>
                        <Pair>
                            <key>highlight</key>
                            <styleUrl>#sh_ylw-pushpin</styleUrl>
                        </Pair>
                    </StyleMap>
                    <Placemark id="{generate-id()}">
                        <Snippet>&#160;</Snippet>
                        <name>Test</name>
                        <styleUrl>#msn_ylw-pushpin</styleUrl>
                        <LineString>
                            <tessellate>1</tessellate>
                            <coordinates>-25.3633333,46.9916667,0 -25.3566667,46.9383333,0 </coordinates>                                
                        </LineString>
                    </Placemark>
                </Document>
            </kml>
        </x:expect>
    </x:scenario>
</x:description>


XSL file
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    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"
    exclude-result-prefixes="xs xd kml gx atom"
    version="1.0">

    <xsl:variable name="test-style">
        <StyleMap id="msn_ylw-pushpin">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_ylw-pushpin</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_ylw-pushpin</styleUrl>
            </Pair>
        </StyleMap>
    </xsl:variable>

    <xd:doc scope="component">
        <xd:desc>
            <xd:p>Testing the creation of ghost empty namespace declarations</xd:p>
        </xd:desc>
    </xd:doc>
    <xsl:template name="make-ghost-namespace">
        <kml xmlns="http://www.opengis.net/kml/2.2" 
            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">
            <Document>
                <name>Las Salinetas Port Limits</name>
                <xsl:copy-of select="$test-style"/>
                <Placemark id="{generate-id()}">
                    <Snippet>&#160;</Snippet>
                    <name>Test</name>
                    <styleUrl>#msn_ylw-pushpin</styleUrl>
                    <LineString>
                        <tessellate>1</tessellate>
                        <coordinates>-25.3633333,46.9916667,0 -25.3566667,46.9383333,0 </coordinates>                                
                    </LineString>
                </Placemark>
            </Document>
        </kml>
    </xsl:template>


</xsl:stylesheet>

单个测试失败,因为实际输出为


    <kml xmlns="http://www.opengis.net/kml/2.2">
       <Document>
          <name>Las Salinetas Port Limits</name>
          <StyleMap xmlns="" id="msn_ylw-pushpin">
             <Pair>
                <key>normal</key>
                <styleUrl>#sn_ylw-pushpin</styleUrl>
             </Pair>
             <Pair>
                <key>highlight</key>
                <styleUrl>#sh_ylw-pushpin</styleUrl>
             </Pair>
          </StyleMap>
          <Placemark id="d5">
             <Snippet>&nbsp;</Snippet>
             <name>Test</name>
             <styleUrl>#msn_ylw-pushpin</styleUrl>
             <LineString>
                <tessellate>1</tessellate>
                <coordinates>-25.3633333,46.9916667,0 -25.3566667,46.9383333,0 </coordinates>
             </LineString>
          </Placemark>
       </Document>
    </kml>

那我该怎么办才能避免在测试过程中创建那些默认的名称空间?

EXPANSION: 因此第一个答案确实有效,但是如果我将另一个模板添加到sylesheet中

<xsl:template name="make-network-link">
    <xsl:param name="name"/>
    <xsl:param name="uri"/>
    <xsl:param name="vis" select="0"/>
    <xsl:param name="open" select="0"/>
    <xsl:param name="refresh"/>
    <NetworkLink>
        <name><xsl:value-of select="$name"/></name>
        <visibility><xsl:value-of select="$vis"/></visibility>
        <open><xsl:value-of select="$open"/></open>
        <xsl:if test="$refresh">
            <refreshVisibility><xsl:value-of select="$refresh"/></refreshVisibility>
        </xsl:if>
        <Link>
            <href><xsl:value-of select="$uri"/></href>
        </Link>
    </NetworkLink>
</xsl:template>

并更改声明以包含默认名称空间,并按如下所示禁止#default

<xsl:stylesheet xmlns="http://www.opengis.net/kml/2.2"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    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"
    exclude-result-prefixes="#default xs xd kml gx atom"
    version="1.0">

然后将另一个测试添加到xspec文件中

<x:scenario label="Create test Network link">
    <x:call template="make-network-link">
        <x:param name="name" select="'Test network link etc'"/>
        <x:param name="uri" select="'http://kmlbalahblah.com/GE/TZFolder.kml'"/>
        <x:param name="vis" select="'1'"/>
    </x:call>
    <x:expect label="Default Hardcoded links">
        <NetworkLink>
            <name>Test network link etc</name>
            <visibility>1</visibility>
            <open>0</open>
            <Link>
                <href>http://kmlbalahblah.com/GE/TZFolder.kml</href>
            </Link>
        </NetworkLink>
    </x:expect>
</x:scenario>

产生的输出仍然带有默认名称空间,即

<NetworkLink xmlns="http://www.opengis.net/kml/2.2">
   <name>Test network link etc</name>
   <visibility>1</visibility>
   <open>0</open>
   <Link>
      <href>http://kmlbalahblah.com/GE/TZFolder.kml</href>
   </Link>
</NetworkLink>

我现在如何取消该命名空间?

0 个答案:

没有答案