0

I'm having trouble with the default namespace in the xml file I'm trying to reference. Does anyone know whey this default ns is causing me so much grief. I'm at my wits end!

InputXML

<?xml version="1.0" encoding="utf-8"?>
<contactBatchResponse version="1.0.3"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="/somelocation.xsd" 
              xmlns="http://www.somecompany.com">
    <FileStatus>
       <someStatus>get status</someStatus>
    </FileStatus>
</contactBatchResponse>

My incorrect xslt :(

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xsi:schemaLocation="/somelocation.xsd" 
                 xmlns="http://www.somecompany.com"
                 exclude-result-prefixes="#default xsi xsl ">

<xsl:output indent="yes" method="xml"/>   
    <xsl:template match="/">
        <Foo>
            <xsl:value-of select="//someStatus"/>
        </Foo>
    </xsl:template>
</xsl:stylesheet>

When I run this I get nothing returned for Foo however once I remove the default namespace everything is ok. What am I missing here????

Thanks

Derek
  • 27
  • 4

2 Answers2

3

You are missing the use of xpath-default-namespace

<xsl:stylesheet version="2.0"
             xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="/somelocation.xsd" 
             xmlns="http://www.somecompany.com"
             xpath-default-namespace="http://www.somecompany.com" 
             exclude-result-prefixes="#default xsi xsl ">

Note your use of xmlns="http://www.somecompany.com" is only really applying to the <Foo> tags, so they are in this default namespace. It does not cover your xpath expression <xsl:value-of select="//someStatus"/> which would select someStatus in no namespace otherwise.

Tim C
  • 70,053
  • 14
  • 74
  • 93
3

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="/somelocation.xsd" 
    xmlns:a="http://www.somecompany.com"
    exclude-result-prefixes="xsi xsl a">

    <xsl:output indent="yes" method="xml"/>   
    <xsl:template match="/">
        <Foo>
            <xsl:value-of select="//a:someStatus"/>
        </Foo>
    </xsl:template>
</xsl:stylesheet>

You have missing namespace

Rupesh_Kr
  • 3,395
  • 2
  • 17
  • 32
  • 1
    @Derek, if it worked for you than [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) this answer – Rupesh_Kr Apr 26 '18 at 16:53