sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Dennis Benzinger's Avatar

Copying xml declaration with XSLT


Question posted by: Dennis Benzinger (Guest) on July 13th, 2006 11:55 AM
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
5 Answers Posted
Bjoern Hoehrmann's Avatar
Bjoern Hoehrmann July 13th, 2006 12:25 PM
Guest - n/a Posts
#2: Re: Copying xml declaration with XSLT

* Dennis Benzinger wrote in comp.text.xml:
Quote:
Originally Posted by
>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:output settings (e.g.,
you can "change" the character encoding which would require to change
the encoding specified by the xsl:output element).
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Dimitre Novatchev's Avatar
Dimitre Novatchev July 15th, 2006 05:15 AM
Guest - n/a Posts
#3: Re: Copying xml declaration with XSLT

(Use case: I have a xml document where I just want to add a processing
Quote:
Originally Posted by
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


"Dennis Benzinger" <Dennis.Benzinger@gmx.netwrote in message
news:44b627ed$1@news.uni-ulm.de...
Quote:
Originally Posted by
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



Dennis Benzinger's Avatar
Dennis Benzinger July 16th, 2006 08:35 PM
Guest - n/a Posts
#4: Re: Copying xml declaration with XSLT

Dimitre Novatchev wrote:
Quote:
Originally Posted by
Quote:
Originally Posted by
>(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.


Yes, I know that. But I don't want to copy the xml declaration, I want
to add a processing instruction.
Quote:
Originally Posted by
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
Dimitre Novatchev's Avatar
Dimitre Novatchev July 17th, 2006 05:15 AM
Guest - n/a Posts
#5: Re: Copying xml declaration with XSLT

>A processing instruction is represented in the XML Infoset and can easily
Quote:
Originally Posted by
Quote:
Originally Posted by
>be copied from one document to another.
> [...]

>
So please show me how.


Use the <xsl:copy-ofinstruction.

Here's a simple example:

This transformation:

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

<xsl:output 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" <Dennis.Benzinger@gmx.netwrote in message
news:44ba96d9$1@news.uni-ulm.de...
Quote:
Originally Posted by
Dimitre Novatchev wrote:
Quote:
Originally Posted by
Quote:
Originally Posted by
>>(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.

>
Yes, I know that. But I don't want to copy the xml declaration, I want to
add a processing instruction.
>
Quote:
Originally Posted by
>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



Dennis Benzinger's Avatar
Dennis Benzinger July 18th, 2006 02:55 PM
Guest - n/a Posts
#6: Re: Copying xml declaration with XSLT

Dennis Benzinger wrote:
Quote:
Originally Posted by
Dimitre Novatchev wrote:
Quote:
Originally Posted by
Quote:
Originally Posted by
>>(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.

>
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?
Quote:
Originally Posted by
Quote:
Originally Posted by
>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
 
Not the answer you were looking for? Post your question . . .
196,995 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,995 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors