Solved

MSXML XSLT is ignoring elements without a namespace.

Posted on 2001-09-14
2
333 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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to dynamically set the form action using jQuery.

762 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

15 Experts available now in Live!

Get 1:1 Help Now