Solved

MSXML XSLT is ignoring elements without a namespace.

Posted on 2001-09-14
2
347 Views
Last Modified: 2013-11-19
I'm uaing MSXML2 to parse some XSL and I'm having real problems with namespaces.

My XML document has some elements with a namespace and some without.

The problem is that the elements without a namespace are being ignored by the stylesheet.

<xsl:value-of select="element-name"/> gives nothing, even though the name is definately valid.

If I uses this

<xsl:value-of select="*[name()='element-name']"/> it works.

Anything with a namespace also works fine.

Any ideas whats going on?

This is some sample xml:

<?xml version="1.0" encoding="utf-8"?>

<rdf:RDF
xmlns="http://"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"


<channel rdf:about="http://www.blahblah.com">

<title>Title Title</title>
<description>
Test Text Text Test Text TextTest Text TextTest Text TextTest Text Text
Test Text Text Test Text TextTest Text TextTest Text TextTest Text Text
Test Text Text Test Text TextTest Text TextTest Text TextTest Text Text
Test Text Text Test Text TextTest Text TextTest Text TextTest Text Text
Test Text Text Test Text TextTest Text TextTest Text TextTest Text Text
</description>
<link>http://www.blahblah.com</link>
<dc:publisher>The Publisher</dc:publisher>
<dc:rights>? Copyright Stuff</dc:rights>
<dc:date>2001-09-10T10:31+00:00</dc:date>
<dc:creator>The creator</dc:creator>

<image rdf:resource="http://www.blahblah.com/images/piccy.gif" />

</channel>


</rdf:RDF>


and this is my xsl:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
version="1.0">


<xsl:output method="html" version="4.0"/>

<xsl:template match="rdf:RDF">
<html>
<head>
<title>Name Space Problem</title>
</head>
<body>
<xsl:apply-templates select="*"/>
</body>
</html>
</xsl:template>

<xsl:template match="*[name()='channel']">

<h1><xsl:value-of select="title"/></h1>
<p><xsl:value-of select="description"/></p>
<font size="small" color="gray">
<center><xsl:value-of select="dc:creator"/></center>
<center><xsl:value-of select="dc:publisher"/></center>
<center><xsl:value-of select="dc:rights"/></center>
<center><xsl:value-of select="dc:date"/></center>

</font>

</xsl:template>

<xsl:template match="*">
<font size="large" color="red">Thats no good</font>
</xsl:template>


</xsl:stylesheet>


This is driving me round the bend, so any help would be appreciated.


0
Comment
Question by:GedB
2 Comments
 
LVL 4

Accepted Solution

by:
chabaud earned 200 total points
ID: 6483351
You really have a namespace problem.

It comes from the default namespace declared in your XML file. It's not the same as the XSL file one.

With no modification of the Xml file, the solution could be to append a foo namespace in XSL:

<xsl:stylesheet
     xmlns:foo="http://"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     exclude-result-prefixes="rdf dc foo"
     version="1.0">

The exclude-result-prefixes attribute remove these namespace from resulting Html.

Then templates should use the foo namespace:

<xsl:template match="foo:channel">

<h1><xsl:value-of select="foo:title"/></h1>
<p><xsl:value-of select="foo:description"/></p>

0
 
LVL 1

Author Comment

by:GedB
ID: 6486069
I knew the namespace was to blame, but the Microsoft documentation is rather weak in this area.  

The exclude-result-prefixes is a great tip, too.

Thannks.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Parsing the XML data to SQL Server 4 59
XML Data Missing in PHP SimpleXML 8 45
Facebook React and SyntheticEvents 6 50
Download a website to hdd 2 51
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now