Issue with xmlns

I have xml file and xslt file.

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

If I remove this attribute in the xml, the xpath clauses in my xslt work. But if I leave it, it doesn't 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 am I doing wrong? How can I mix xml and xslt if xml has xmlns attribute on top?

+2


a source to share


2 answers


The xmlns

unnamed prefix attribute replaces the default namespace so your query matches nodes with a different (empty) namespace. You must use the prefixed namespace in XSLT XPath queries (or XmlNamespaceManager

for standalone XPath queries) and your queries will work as expected again.

In XSLT:

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

      



Then, assuming you used to compare, for example xyz

, you now change your query like this:

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

      

In general, you can read some documents in XML namespaces.

+3


a source


It should be noted though, at least a namespace string should simply be treated as an exact literal string that is used as an identifier without any other meaning. The string must conform to the IANA uri to ensure that the namespaces are unique. Some parsers refuse to parse namespaces that don't match.

Also note that since they are strings http://www.w3.org/1999/xhtml

and for example are http://w3.org/1999/xhtml/

completely different namespaces for namespace purposes.



You might want to read how namespaces work in XML. exa:com.test

not a valid namespace string as far as i know.

+1


a source







All Articles