473,322 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
2 3103
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hayko Riemenschneider | last post by:
Hi! I've got me an XSL tranformation stylesheet for my XML file. In the XSL file I now wish to use PHP to do some scripting. So I thought I'll use the PIs like this: ...
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
1
by: Mohit | last post by:
Hi Friends I have to call 1 of the 2 child XSLT files from the Main XSLT file based on some criteria. I want one child XSLT file will be executed by version 1 of XSLT processor and the other by...
8
by: Timothy Casey | last post by:
Is there any way to code an external style sheet to ensure that all pages opening with a reference to that style sheet include generic content such as headers, logos, taglines, universal navigation...
3
by: Jack Fox | last post by:
I've never had the need to work with XML, but I believe I now have an appropriate application. I have time-series data in objects organized as a tree that I want an ASP.NET program to write out to...
0
by: Brendan Reynolds | last post by:
I have a web service that takes the XML representation of a dataset and uses XSLT to transform it to the format required to open a disconnected ADODB recordset on the XML, and returns the...
1
by: womber | last post by:
What do you think is the best design approach when your trying to get nested XML results from dataSet.GetXML. Define the relations in the dataset or apply an XSLT stylesheet to nest the results by...
6
by: Christopher | last post by:
I am currently in the process of evaluating the performance hits of moving to the .NET platform for our application. I created a sample project that loads the transforms the same XML and XSLT in...
3
by: Jensen bredal | last post by:
Hello, I need to write different kind of forms that the customer can use to enter data and save the data into database. (The data access will be hand coded) My problem is that the form may vary...
2
by: veracon | last post by:
Hello, I'm looking to use XML and XSLT for templates in a system I'm writing, however I'm not really sure which parser is the "best". Basically, which library has the most features, and which is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.