[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Doctype and namespaces in source/generated files


Subject: Re: [xsl] Doctype and namespaces in source/generated files
From: Mayo <mayo@xxxxxxx>
Date: Fri, 28 May 2004 18:44:15 -0700

Thanks, this solved almost all of my problems!

I've figured out how to generate doctype for the document, but whenever I introduce

doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"

into <xsl:output> element, the spacing in the output gets really weird. I know there is <xsl:strip-space>, but that still doesn't fix things. (ie. when I generate elements with <xsl:element> as follows

<xsl:template match="tpl:var">
    <xsl:element name="b">
        <xsl:value-of select="'blarg'"/>
    </xsl:element>
</xsl:template>

The output would have line break (newline, not <br>) right before the element that is being created. (Regardless whether the value-of is selected from another variable, node, or attribute)

For eg, from source
[snip]
    <body>
        <p>xsl test1: <tpl:var/></p>
	</body>
[/snip]
i would get
[snip]
  <body>
    <p>xsl test1:
      <b>blarg
</b>
  </body>
[/snip]

(this is using <xsl:strip-space elements="*"/>, without that, there is about 3 blank lines instead of the breaks.)

Regards,
Mayo


On May 28, 2004, at 15:38, G. Ken Holman wrote:


At 2004-05-28 14:34 -0700, Mayo wrote:
I'm trying to process and output xhtml documents, but I'm having some
problems with namespaces. Each source document has extra namespace(s)
(other than the xhtml one) for custom tags (the ones that the xslt
stylesheet will be processing).

The xslt I'm using matches elements in undefined namespaces, and copies
those into the output, and processes the elements in my custom
namespaces. (In other words I want to copy everything other than my
custom namespaces, and process what's not copied)

Then you can have a template rule for each of your custom elements:


<xsl:template match="custom:this">...

<xsl:template match="custom:that">...

<xsl:template match="custom:other">...

... and a catch all for anything unexpected in your custom namespace:

<xsl:template match="custom:*">...

the match rule i'm using is:
<xsl:template match="*[not(namespace-uri())]">

That is only for elements in no namespace.


than I simply xsl:copy, and apply templates.

Careful about using <xsl:copy> as that will also copy all of the in-scope namespaces that are attached to the element as namespace nodes. You might find it safer to do:


  <xsl:element name="{local-name(.)}">
     <xsl:apply-templates/>
  </xsl:element>

as this will not copy any source-tree namespace nodes attached to the current element.

This prevents me to
specify DOCTYPE or namespaces for xhtml in the source documents, as as
soon as I specify them (in the source documents) the match rule will
stop working, and strips all my xhtml out.

Or you could just add namespace-qualified XHTML elements to your match:


<xsl:template match="*[not(namespace-uri())] | xhtml:*">

with an in-scope declaration of the XHTML namespace URI for that prefix.

My quesiton is, is there a better way to do this,

To do what? To create an output tree without undesirable namespace nodes? If that is what you are asking for, then use the <xsl:element> instruction above.


or is there any easy
way that I can manipulate what namespaces/doctypes the output files
will have? (I want to strip out my custom namespaces, preserve any
other ones,

When preserving a namespace you cannot use <xsl:copy> without "accidentally" copying all in-scope namespaces. Use the following:


   <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
     ...

... as this will reconstruct the given source tree element and not have the namespace-node baggage that source tree elements have.

add the xhtml one, as well as add a doctype to the
generated document).

To add a document type declaration, use doctype-system= and optionally doctype-public= to the <xsl:output> instruction.


Ideally, I would like to specify doctype and all the proper namespaces
in the source files.

Specify the document type declaration using <xsl:output> ... the namespaces are managed in XSLT 1 by the implicit copying of elements and the use of namespace= on <xsl:element>, though there are some tricks you can use with <xsl:copy-of/>.


I hope this helps.

....................... Ken

--
Public courses: Spring 2004 world tour of hands-on XSL instruction
Next: 3-day XSLT/XPath; 2-day XSL-FO - Birmingham, UK June 14,2004

World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


Current Thread
Keywords