开发者

issue with a xmlns

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(HttpCont开发者_JAVA技巧ext.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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜