0

我正在使用测试驱动的开发过程来创建一些 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>

那么我需要做什么才能避免在测试期间创建这些默认命名空间?

扩展: 所以第一个答案确实有效,但是如果我在样式表中添加另一个模板

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

我现在如何抑制那个命名空间?

4

0 回答 0