Project

Profile

Help

XHTML, Saxon and namespaces

Added by Anonymous over 18 years ago

Legacy ID: #3649271 Legacy Poster: dumbdare (dumbdare)

Now I appreciate what I'm about to ask goes against W3C recommendations etc, but is there any way to kill completely the namespace cleanup in Saxon 8? The reason I ask is that I'm generating jsp files via XSLT 2.0 that supports the xhtml output method. Now these files contain fragments of markup rather than a nicely formatted and compliant template. In order to get the parser to format the output as xhtml I have to associate the output with the xhtml namespace, else it appears to default to parsing html (whether I do this on the input XML or through declaring the output namespace in the stylesheet doesn't matter). The problem is that Saxon will then add the xhtml namespace to the output markup (and so it should) as I haven't explicitely declared it anywhere. Ultimately all my templates are combined to create a final, and xhtml compliant page for display but it now contains spurious namespace declarations scattered throughout the markup, and I've been accused of littering! Sooo is there any known hack (and I'm guessing it will be a hack as it'll make the parser non-compliant) to get round this? Just curious... Rick


Replies (7)

Please register to reply

RE: XHTML, Saxon and namespaces - Added by Anonymous over 18 years ago

Legacy ID: #3649311 Legacy Poster: Michael Kay (mhkay)

I suspect there is a clean solution to your problem and that it doesn't require any "hacks" at all. But you haven't really explained your problem, so I don't know. It's the usual request: show us your input, show us your stylesheet, show us the output you want, and show us the output you are getting. All, if at all possible, "in miniature", i.e. removing all details that aren't relevant to the problem in hand.

RE: XHTML, Saxon and namespaces - Added by Anonymous over 18 years ago

Legacy ID: #3649417 Legacy Poster: dumbdare (dumbdare)

Ok here's a very basic version of what I'm trying to do, Input XML: <?xml version="1.0" encoding="UTF-8"?> <page title="Test page" xmlns="http://www.w3.org/1999/xhtml"> <panel name="foo"> <body> <p>some markup</p> <script></script> <br></br> <br /> <textarea id="mytext"></textarea> </body> </panel> <panel name="bar"> <p>some other markup</p> <br></br> </panel> </page> The Stylesheet: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" xpath-default-namespace="http://www.w3.org/1999/xhtml" exclude-result-prefixes="#all"> <xsl:output method="xhtml" version="1.0" indent="no" omit-xml-declaration="yes"/> <xsl:template match="page"> <xsl:call-template name="generate_jsp"/> </xsl:template> <!-- generate the jsp file for each panel --> <xsl:template name="generate_jsp"> <xsl:for-each select="panel"> <xsl:variable name="jspFilename" select="concat(@name, '.jsp')"/> <xsl:result-document href="{$jspFilename}" exclude-result-prefixes="#all"> <!-- process tags --> <xsl:apply-templates /> </xsl:result-document> </xsl:for-each> </xsl:template> <!-- match any node --> <xsl:template match="node()"> <xsl:copy copy-namespaces="no"> <xsl:apply-templates select="@"/> <xsl:apply-templates select="child::node()"/> </xsl:copy> </xsl:template> <!-- match any attribute --> <xsl:template match="@"> <xsl:copy /> </xsl:template> </xsl:stylesheet> The output: 1) foo.jsp <body xmlns="http://www.w3.org/1999/xhtml"> <p>some text</p> <script></script> <br /> <br /> <textarea id="mytext"></textarea> </body> 2) bar.jsp <p xmlns="http://www.w3.org/1999/xhtml&quot;&gt;some other text</p> <br xmlns="http://www.w3.org/1999/xhtml" /> Basically the desired output is the same but with no namespace attributes. I've tried to use a number of methods, including trying to recreate the elements in the stylesheet and associate them with another namespace, but it would seem that unless somewhere you state the xhtml namespace the parser treats the markup as html, not xhtml as stated in the output. The namespace is therefore naturally added to the output. I'm actually doing a lot more processing on the markup and replacing tags with custom elements, etc but have removed the majority of the code for simplicity. All I'm really after is the ability to transform xhtml without any namespaces in the output. Any hints would be great as I've tried a whole bunch of different approaches but all the examples always come back to declaring the namespace somewhere on the output document (most strip superfluous declarations just fine, I need to strip all of them). Thanks in advance. Rick

RE: XHTML, Saxon and namespaces - Added by Anonymous over 18 years ago

Legacy ID: #3649518 Legacy Poster: Michael Kay (mhkay)

This seems wrong for a start: <page title="Test page" xmlns="http://www.w3.org/1999/xhtml"> <panel name="foo"> There's a specification of the XHTML namespace, and it doesn't include elements called "page" and "panel". The whole point of namespaces is that if your document says it is using element name N in namespace S, someone can go to the specification of namespace S to find out what N means. So you're starting with a dodgy input document. exclude-result-prefixes="#all" This only affects literal result elements, and your stylesheet doesn't contain any literal result elements, so you might as well leave it out. "Basically the desired output is the same but with no namespace attributes." What you mean is that you don't actually want to copy the source elements unchanged, you want to rename them so that instead of being called {xhtml}foo, they are called {}foo. To rename an element into a different namespace, use <xsl:element name="{local-name()}" namespace=""> rather than using xsl:copy. Remember that namespace declarations are added by the serializer to implement your decisions on what names to give the output elements. If you give the output element the correct name, the namespace declarations will look after themselves. Incidentally, there is nothing Saxon-specific about your question: general XSLT questions should really go on the xsl-list at mulberrytech.com

RE: XHTML, Saxon and namespaces - Added by Anonymous over 18 years ago

Legacy ID: #3651593 Legacy Poster: dumbdare (dumbdare)

Yep, granted panel is not an xhtml tag, I'm actually dealing with an xml document that contains xhtml markup (and ultimately outputting jsp and tiles type tags along with custom tags dealt with by the spring framework), the panel node is basically a container for my custom markup that also contains xhtml markup. The problem I have is that if I don't declare an xhtml namespace somewhere (either on the input or in the styleshet) then the parser will not output the markup as xhtml. I tried to use xsl:element and recreate my tags instead of copy but then I found I had to associate them to a default xhtml namespace in the stylesheet in order for the parser to output xhtml compliant markup. I'm guessing that declaring a blank namespace in the output element tag may have the same result, I'll give it a shot. So is there a way to get the parser to output xhtml without explicitely declaring the xhtml namespace either in the input or the stylesheet as I've not found one. Simply declaring the output in the stylesheet as xhtml doesn't seem to work. Maybe I'm missing something here. In the real input document I have a bunch of other prefixed tags, the exlude prefixes attribute was set to ignore these but as it turns out I ended up recreating these as simple text outputs as I had to do a bunch of processing on them anyway, so the exclude attribute is, indeed, redundant. Point taken that perhaps this is the wrong forum for this, but I think that I wanted to clarify the influence the stylesheet output attributes have on the parsed output and given (IMO) that Saxon is the only parser of note to specifically deal with xhtml output thought I post here. Namespace issues seem to be a common bugbear when dealing with XSLT and I've searched numerous forums and tried a variety of fixes but to no end. I'm think I'm working on the borders of compliancy here as I need to output fragments of markup which in themselves do not form a true xhtml document and need to be rendered through a jsp engine associated with custom tag libraries through Spring. I've started to butt up against some limitations, but nobody said it was easy! Thanks for your advice, will continue this conversation on a more xslt specific forum. Regards Rick Edwards

RE: XHTML, Saxon and namespaces - Added by Anonymous over 18 years ago

Legacy ID: #3652395 Legacy Poster: Michael Kay (mhkay)

You're obviously having some problems getting to grips with namespaces, and it's difficult to help you without writing a tutorial on the subject, which has almost certainly been done better elsewhere. I'm also confused about what output you want. In your last post you said you wanted "Basically the desired output is the same but with no namespace attributes." from which I inferred that you didn't want the output to be in the XHTML namespace; now you seem to be suggesting that you do. Since this is all about XSLT coding and nothing specifically to do with Saxon, I suggest you ask the questions on a different forum.

RE: XHTML, Saxon and namespaces - Added by Anonymous over 18 years ago

Legacy ID: #3652836 Legacy Poster: dumbdare (dumbdare)

Sorry for the confusion, I'm clearly not explaining this well. I need to declare the output as being in the xhtml namespace in order to get the parser to output xhtml (usually you would do this in an html tag I chose my page tag). However, I don't want any xmlns attributes to appear on the output as I'm creating fragments of markup and this ends up littering the code. I suspect this is not possible as I'm trying to break the rules by asking the parser to treat my code as xhtml (by declaring a namespace) but don't declare that namespace on the result. Thanks anyway, I'll leave this issue now. Rick

RE: XHTML, Saxon and namespaces - Added by Anonymous over 18 years ago

Legacy ID: #3652936 Legacy Poster: Michael Kay (mhkay)

If there are no xmlns attributes in the result then the output is not in the XHTML namespace, by definition.

    (1-7/7)

    Please register to reply