0

I am using a test-driven-development process to create some xslt to create kml data dynamically. With xslt I use xspec to manage the tests. And I am experiencing a default namespace problem.

The xsl stylesheet has a large number of namespace declarations

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

The xpec test file has a simpler declaration

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

Part of the xslt code copies swathes of kml style elements into the output stream, e.g.

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

This is defined in an element.

In the xspec test file I am specifying what kml to expect and I just copy and paste this code into the appropriate element.

But the test fails as what is generated contains an empty xmlns="" namespace declaration, e.g.

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>

The single test fails as the actual output is


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

So what do I need to do in order avoid those default namespaces being created during testing?

EXPANSION: So the first answer does work, but if I add another template to the 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>

and change the declaration to include the default namespace as well as suppress #default as follows

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

and add another test to the xspec file

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

The output produced still comes through with the default namespace, i.e.

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

How do I suppress THAT namespace now?

Feargal Hogan
  • 169
  • 1
  • 8
  • 1
    That's because your `StyleMap` element is under the empty namespace instead of the KML namespace. Add a _"global"_ default namespace declaration `xmlns="http://www.opengis.net/kml/2.2" ` to your stylesheet or add one for each literal result element like `` as your are doing right now with your `kml` element – Alejandro May 07 '19 at 14:25
  • Your question has shifted. That doesn't make it more clear... – Alejandro May 14 '19 at 12:54

0 Answers0