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

1
  • Search for "XSLT default namespace" to find 641 previous answers to this question. Commented Apr 26, 2018 at 22:31

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

@Derek, if it worked for you than accept this answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.