Copying xml declaration with XSLT

D

Dennis Benzinger

How can I copy the xml declaration from one document to another document
using XSLT?

(Use case: I have a xml document where I just want to add a processing
instruction without modifing the rest of the document)


Thanks,
Dennis
 
B

Bjoern Hoehrmann

* Dennis Benzinger wrote in comp.text.xml:
How can I copy the xml declaration from one document to another document
using XSLT?

(Use case: I have a xml document where I just want to add a processing
instruction without modifing the rest of the document)

That's not possible without using processor-specific extensions; in the
XPath data model, which XSLT operates on, this information is lost and
re-created based on among other things the xsl:eek:utput settings (e.g.,
you can "change" the character encoding which would require to change
the encoding specified by the xsl:eek:utput element).
 
D

Dimitre Novatchev

(Use case: I have a xml document where I just want to add a processing
instruction without modifing the rest of the document)

The xml declaration is not a processing instruction. A processing
instruction is represented in the XML Infoset and can easily be copied from
one document to another.

Cheers,
Dimitre Novatchev
 
D

Dennis Benzinger

Dimitre said:
The xml declaration is not a processing instruction.

Yes, I know that. But I don't want to copy the xml declaration, I want
to add a processing instruction.
A processing instruction is represented in the XML Infoset and can easily
be copied from one document to another.
[...]

So please show me how.

Dennis
 
D

Dimitre Novatchev

A processing instruction is represented in the XML Infoset and can easily
be copied from one document to another.
[...]

So please show me how.

Use the <xsl:copy-of> instruction.

Here's a simple example:

This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput omit-xml-declaration="yes"/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="processing-instruction()">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

when applied on this source xml document:

<t>
<?PI xxx="yyy" ?>
</t>

produces this result:

<t>
<?PI xxx="yyy" ?>
</t>

The processing instruction matched by the 2nd (last template) and this
template is selected for processing it. The action is simply to copy the
current node (the processing instruction) to the output.


Hope this helps.

Cheers,
Dimitre Novatchev


Dennis Benzinger said:
Dimitre said:
The xml declaration is not a processing instruction.

Yes, I know that. But I don't want to copy the xml declaration, I want to
add a processing instruction.
A processing instruction is represented in the XML Infoset and can easily
be copied from one document to another.
[...]

So please show me how.

Dennis
 
D

Dennis Benzinger

Dennis said:
Yes, I know that. But I don't want to copy the xml declaration, I want
to add a processing instruction.

Must have been quite confused when I wrote this. Of course I want to
copy a xml declaration like I wrote in my first post. After all I want
to add a processing instruction to a xml document without modifing the
rest of the document. But as Bjoern Hoermann wrote that's not possible
with XSLT. Maybe XSLT 2.0 helps?
A processing instruction is represented in the XML Infoset and can easily
be copied from one document to another.
[...]

So please show me how.

Dennis

Thanks to Dimitre Novatchev for anwsering my useless question.


Dennis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top