Re: [xsl] XPath Expression in XSL Template Will Not Return Documen t Element with Attributesþ

Subject: Re: [xsl] XPath Expression in XSL Template Will Not Return Documen t Element with Attributesþ
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 09 Feb 2009 18:31:19 -0500
Joshua,

Welcome to XSL-List.

At 05:03 PM 2/9/2009, you wrote:
Listmembers, I have a problem creating an appropriate XPath expression in a XSL file, and have been unable to find a solution. Here the Scenario: I have an xml file output from asp.net xmlserializer class. It outputs the Document Element "Catalog" with three attributes pertaining to XML namespaces.

the catalog tag looks like this (markup removed):

Catalog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:xsd="http://www.w3.org/2001/XMLSchema";

First off, a point of terminology. In namespace-aware XML, attributes that start "xmlns" aren't attributes in the normal sense, and don't appear as such in the data model. They are namespace declarations, which affect the way elements are named (specifically, by allowing us to differentiate families of elements and attributes with prefixes on their names).


Second, while you say you have three attributes, you've only pasted in two. The third might be the source of the problem.

In particular, if you have a (pseudo-)attribute named "xmlns", this is a namespace declaration for the "null" or unprefixed namespace -- which has the effect of placing unprefixed elements, such as "Catalog", into this namespace.

The tricky thing is that when this is the case, XSLT has no way of addressing the element, since you ordinarily do not want to bind unprefixed names in XSLT to a namespace. (If you do, it's going to affect elements in your *result* XML, without helping to address the elements in your *source* XML, since XSLT addresses the source data via XPath, which is to say, only as attribute values in the XSLT-as-XML.)

The solution is therefore to declare a namespace with a prefix in your stylesheet, and use that to address the elements.

So if your source data has:

Catalog xmlns="http://my.namespace.com";

your stylesheet would have a declaration xmlns:n="http://my.namespace.com"; (probably at the very top, on the xsl:stylesheet element; use any prefix you like, 'n' is my choice), and thence you would say

... xsl:apply-templates select="n:Catalog" ...

and your stylesheet would then work with your (namespaced) input.

I hope that helps.

Cheers,
Wendell

I have created an XSL file to restructure the information for use in a ASP.NET GridView Control.

  xsl:template match="/"
    xsl:apply-templates select="Catalog"
  xsl:template>

It works, but only if I remove the attributes from the Element. I need to know how to format the xsl:template match="Catalog" xpath expression in the XSL file to match that tag, even if it has attributes.


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread