You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Persson Richard <ri...@tietoenator.com> on 2003/06/30 10:03:09 UTC
Problem with default namespace declaration and XSLT
I am trying to do a XSL transformation of a XML file using the class
javax.xml.transform.Transformer and an external XSL file.
The XSL file performs a very simple copy of the entire input to the output.
XSL-File:
----------
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output encoding="ISO-8859-1"/>
<xsl:template match="Message">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
-----------
Problem:
--------------
If the input XML looks like (note default namespace declaration has no
namespace prefix 'xmlns=...'):
<?xml version="1.0" encoding="ISO-8859-1"?>
<Message xmlns="http://www.dot.com/xmlstds/request">
<Type V="H"/>
<MsgVersion>v1.0 2002-11-08</MsgVersion>
<MIGversion>v1.0 2002-11-08</MIGversion>
...
I get a value-of result from the XSLT.
But, if the input XML has got a namespace declaraion with namespace prefix,
'xmlns:foo="http://www.dot.com/xmlstds/request' I get what I want, a copy-of
result of the input file.
Is the javax.xml.transform.Transformer class failing or am I on the wrong
track?
/Richard Persson
---------------
Richard Persson
TietoEnator AS
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: Problem with default namespace declaration and XSLT
Posted by Joseph Kesselman <ke...@us.ibm.com>.
You probably intended this for the Xalan (XSLT processor) user list rather
than the Xerces (XML parser) list.
Short answer: This is an XSLT FAQ. To match a namespaced element -- whether
the source document uses the default namespace or not -- you must use a
properly-declared namespace prefix in your XPath. There is a proposal to
add the default shorthand to XPath 2.0, but until that comes along you're
stuck. Rewrite your template as
<xsl:template match="foo:Message" xmlns:foo="
http://www.dot.com/xmlstds/request">
<xsl:copy-of select="."/>
</xsl:template>
(Of course you can declare the prefix at a higher level, eg on the
stylesheet element, if you prefer; since you'll usually want to use it in
multiple templates that's generally the preferred solution.)
______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org