Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matching xmlns atribute using XSLT template

Status
Not open for further replies.

crazyboybert

Programmer
Jun 27, 2001
798
GB
Hi All

I'm writing an XSLT to transform some XHTML content from an RSS feed to the markup I want to use in a website. I'm encoutnering a problem matching elements with an xmlns attribute. Heres an example..

XML
Code:
<item>
	<title>title</title>
	<link>link</link>
	<category>category</category>
	<pubDate>Wed, 01 Sep 2004 12:20:00 GMT</pubDate>
	<description>description</description>
	<body xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
		<h4>some markup</h4>
                <p>some more markup</p>
	</body>
	<guid isPermaLink="false">someguid</guid>
</item>

XSL
Code:
<xsl:template match="/item"><xsl:apply-templates /></xsl:template>

<xsl:template match="title" />

<xsl:template match="link" />

<xsl:template match="category" />

<xsl:template match="pubDate" />

<xsl:template match="description" />

<xsl:template match="body">some markup</xsl:template>

<xsl:template match="guid" />

When transforming the XML file using the stylesheet shown I would only expect the output to be...

some markup

what actually outputs is...

some markupsome more markup

This is because the body element does not get captured by the template match due to the xmlns attribute. I did some playing around in XMLSPY and found the following matches/failures for XPATH expressions

xpath: body

This matches the following

<body>
<body attribute="value">

but does not match

<body xmlns="value">


xpath: body[attribute]

This only matches

<body attribute="value">


xpath: body[xmlns]

This doesn't seem to match any body elements.


It seems like you can't write an xpath expression to match an element with an xmlns attribute. Whats goign on here, am i missing something fundamental? I've googled for answers and come up empty handed!

Thanks in advance

Rob





Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
I found a solution. You can match for items on the namespace axes using the prefix so the following xpath

rob:item

would match the element

<item xmlns:rob="robsnamespace" />

as the body tag in the rss feed has an xmlna attribute but no prefix it couldnt be matched in my xsl. However by adding a namespace delcaration with the same identifer and a prefix to the stylesheet i could matched the unprefixed items within the same namespace from the XML ok.

also i think i could have matched with

item[namespace-uri='robsnamespace']

and that wouidl have worked too.



Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top