Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to ignore namespaces during an XSL transformation?

Tags:

xml

xslt

Namespace checking is disabled on the server that handles XSL transformations (because the guy who wrote the XSL didn't understand namespaces). I have to make changes to the XSL but I can't test it because there aren't any namespaces defined ie.

Instead of

 <xsl:template match="ns:element[position()=1]">...

it has

 <xsl:template match="element[position()=1]">...

so it doesn't match any of the elements in the XML because all they're all qualified with namespaces.

I can't test on the server because I don't have access to it. It's no use fix the XSL because then namespace checking will have to be enabled, and that will break all the other transformations.

So what I need to do is find a way of ignoring namespaces during an XSL transformation. I have access to MSXML, XMLSpy (can't find an option in here) and if I really need to I can code something up in C# or a similar language.

As a last resort I can code up a few regexes but I really don't want to go down that route, especially when dealing with XML...

In response to a comment about more details:

It's a Windows 2003 Virtual Server, running an instance a Methode Servlet (www.eidosmedia.com). I don't know what technique this servlet uses to perform XSL transformations. They're ignoring namespaces because the person who originally wrote the XSL didn't understand them, and didn't include them in the XSL. So now all the XSL files (hundreds) don't have namespaces.

It could be an interesting challenge to fix all these files in one go, but that's not what I need right now (and the departmental manager would never agree to it anyway because of the amount of testing involved). All I want to know is if there's a tool (or technique) available that will allow me to take these XSL files as is and use them to transform a corresponding XML document without taking into account namespaces. It seems to me that a tool must exist, because the guy who wrote the original XSL must have used something similar to test the transformations himself.

like image 479
ilitirit Avatar asked Mar 10 '09 13:03

ilitirit


2 Answers

I'm posting this as an answer because it's too long to fit as a comment.

No XSLT processor that I've ever even heard of allows you to just globally ignore namespaces in the input XML. If you have XSL transforms that are written in ignorance of namespaces, and XML documents that use them, you either have to use the broken servlet to do the transformation, or pre-process the XML documents to remove the namespaces.

The transform Tomalak posted will do that - and unlike using regular expressions, it will do it without screwing up everything else in the XML. And it's not very much work to do that, either. Chaining transforms is pretty easy.

like image 31
Robert Rossney Avatar answered Sep 25 '22 20:09

Robert Rossney


You could do a transform to remove all namespaces from your input prior to your "real" transformation. But... I'm not sure if you should do it. It feels ugly.

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="*">
    <xsl:element name="{local-name()}" >
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>  

  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="." />
    </xsl:attribute>
  </xsl:template>  

</xsl:stylesheet>

Original answer:

Would that be an option?

<xsl:template match="*[local-name()='element' and position()=1]">...

(as comments showed - it wouldn't)

like image 158
Tomalak Avatar answered Sep 22 '22 20:09

Tomalak