By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

generic xslt to nest a xml-document as text within an element

P: n/a
Hallo,

can you help me writing a generic xslt transformation (useable with
xsql from oracle)? The problem is how to get the escaping characters
....

=== INPUT-File in.xml
<?xml version = '1.0'?>
<person><who>scott</who></person>

=== Needed OUTPUT-File out.xml
=== Note: the format must be exactly this to be
=== processable by xsql using xsql-insert
<?xml version = '1.0'?>
<ROWSET><ROW>
<NAME>&lt;person>&lt;who>scott&lt;/who>&lt;/person></NAME>
</ROW></ROWSET>

=== My xslt-File p.xsl, that misses the important fact
=== of replacing < with &lt;
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:element name="ROWSET">
<xsl:element name="ROW">
<xsl:element name="NAME">
<xsl:copy-of select="."/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

=== my output, that is not the expected
oraxsl in.xml p.xsl
<ROWSET><ROW><NAME><person><who>scott</who></person></NAME></ROW></ROWSET>

Any ideas?
Thank you if you find the time for helping,
Martin
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <10**************************@posting.google.com >,
Martin <ms*****@csc.com> wrote:

% === INPUT-File in.xml
% <?xml version = '1.0'?>
% <person><who>scott</who></person>
%
% === Needed OUTPUT-File out.xml
% === Note: the format must be exactly this to be
% === processable by xsql using xsql-insert
% <?xml version = '1.0'?>
% <ROWSET><ROW>
% <NAME>&lt;person>&lt;who>scott&lt;/who>&lt;/person></NAME>
% </ROW></ROWSET>

% === My xslt-File p.xsl, that misses the important fact
% === of replacing < with &lt;
% <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
% version="1.0">
% <xsl:output method="xml" omit-xml-declaration="yes"/>
% <xsl:template match="@*|node()">
% <xsl:element name="ROWSET">
% <xsl:element name="ROW">
% <xsl:element name="NAME">
% <xsl:copy-of select="."/>
% </xsl:element>
% </xsl:element>
% </xsl:element>
% </xsl:template>
% </xsl:stylesheet>

You should keep in mind that the output of the XSLT process is a tree,
not just text. When you use copy-of, what you're doing is copying the
tree structure related to the node, not the tags themselves.
So, how do you get the tags? Probably node-by-node, using the name()
function. If all you want is a copy of the original document with
< and & escaped, I'm not sure XSLT is the best thing to use, but
let's have an untested go at it

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>

<!-- need to handle elements, pis, comments, and attributes separately
since we need to format the tags. We start with elements -->
<xsl:template match="*">
<!-- no need for xsl:element here, so let's not use it -->
<ROWSET>
<ROW>
<NAME>
<xsl:text>&lt;</xsl:text>
<xsl:value-of select="name()"/>

<!-- attributes go here -->
<xsl:for-each select="@*">
<xsl:value-of select='name()'/.
<xsl:text>='</xsl:text>
<!-- you might need to deal with quotes here -->
<xsl:value-of select='.'/.
<xsl:text>'</xsl:text>
</xsl:for-each>
<xsl:text>&gt;</xsl:text>

<xsl:apply-templates/>

<xsl:text>&lt;/</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>&gt;</xsl:text>
</NAME>
</ROW>
</ROWSET>
</xsl:template>

<xsl:template match="processing-instruction()">
<xsl:text>&lt;?</xsl:text>
<xsl:value-of select='name()'/>
<xsl:text> </xsl:text>
<xsl:value-of select='.'/>
<xsl:text>?&gt;</xsl:text>
</xsl:template>

<xsl:template match="comment()">
<xsl:text>&lt;-- </xsl:text>
<xsl:value-of select='.'/>
<xsl:text> --&gt;</xsl:text>
</xsl:template>

</xsl:stylesheet>
--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #2

P: n/a
Hallo Patrick,

for an untested go your answer was really excellent.
I've found (after quite some more study of xsl/xpath) a solution,
but comparing it with your solution i got to know the
difference between a beginner and an expert (simply because my
solution was neither elegant, nor did I cover points like
comment-nodes or processing instructions).

A big thank you for your answer!

For other readers: see the finished solution below ...

Greetings from vienna,
Martin

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

<xsl:template match="/">
<xsl:element name="ROWSET">
<xsl:element name="ROW">
<xsl:element name="DOC">
<xsl:apply-templates/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>

<!-- need to handle elements, pis, comments, and attributes separately
since we need to format the tags. We start with elements -->
<xsl:template match="*">
<xsl:text>&lt;</xsl:text>
<xsl:value-of select="name()"/>

<!-- attributes go here -->
<xsl:for-each select="@*">
<xsl:text> </xsl:text>
<xsl:value-of select='name()'/>
<xsl:text>="</xsl:text>
<!-- Assumption: input file has not apostrophs -->
<xsl:value-of select='.'/>
<xsl:text>"</xsl:text>
</xsl:for-each>
<xsl:text>&gt;</xsl:text>

<xsl:apply-templates/>

<xsl:text>&lt;/</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>&gt;</xsl:text>
</xsl:template>

<xsl:template match="processing-instruction()">
<xsl:text>&lt;?</xsl:text>
<xsl:value-of select='name()'/>
<xsl:text> </xsl:text>
<xsl:value-of select='.'/>
<xsl:text>?&gt;</xsl:text>
</xsl:template>

<xsl:template match="comment()">
<xsl:text>&lt;-- </xsl:text>
<xsl:value-of select='.'/>
<xsl:text> --&gt;</xsl:text>
</xsl:template>

</xsl:stylesheet>
===== output from above sample
C:\XDK>oraxsl in.xml rowset_row_doc.xsl
<?xml version = '1.0'?>
<ROWSET><ROW><DOC>&lt;person>&lt;who>scott&lt;/who>&lt;/person></DOC></ROW></ROWSET>
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.