views:

25

answers:

2
+1  Q: 

issue with a xmlns

Hello,

I have an xml file and an xslt file.

The xml file has a xmlns="exa:com.test" attribute.

If i remove this attribute in the xml the xpath sentences in my xslt works. But if i leave it, doesnt work.

Im using the following code to mix xml and xslt:

XslCompiledTransform transformer = new XslCompiledTransform();

transformer.Load(HttpContext.Current.Server.MapPath("xslt\\searchresults.xslt"));  

transformer.Transform(xmlreader, null, utf8stringwriter);

What im doing wrong? How could i mix xml and xslt if the xml has the xmlns attribute on top?

Thanks in advance.

Best Regards.

Jose

+2  A: 

The xmlns attribute without prefix name replaces the default namespace, so that your query matches nodes with a different (empty) namespace. You need to use a prefixed namespace in the XSLT XPath queries (or a XmlNamespaceManager for isolated XPath queries) and your queries will work again as expected.

In the XSLT:

<xsl:stylesheet ... xmlns:exa="exa:com.test">

Then, assuming that you used to match for instance xyz, you now change your query like this:

<xsl:template match="exa:xyz"> ...

In general you might want to read some docs on XML namespaces.

Lucero
Thanks a lot.Greetings.Jose
Josemalive
+1  A: 

It should be noted though, though the namespace string is to just be seen as an exact literal string that is used as an identifier with no other meaning. The string must conform to an IANA uri, this is to guarantee uniqueness of namespaces. Some parsers refuse to parse namespaces that do not conform.

Also note that because they are strings http://www.w3.org/1999/xhtml and for instance http://w3.org/1999/xhtml/ are for the purposes of namespaces completely different namespaces.

You might want to read up on how namespaces in XML work. exa:com.test is as far as I know not a valid namespace string.

Lajla