473,394 Members | 2,071 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,394 software developers and data experts.

Multiple XSLt Transforms?

I have a DTD that defines new elements "mytextfield" and "mysn", and
does it as an extension to XHTML. The idea is that my XML markup is
actually valid XHTML according to my DTD. This all works just fine.

After getting the DTD to work, I defined a XSL stylesheet that would
transform my markup into XHTML that would display correctly in a
browser. At first this meant that everywhere I encontered a
"mytextfield" element in the markup, I would transform it into a
<input type="text" /> element in the output, and preserve whatever
attributes were on the "mytextfield" element. Again, this worked just
fine.

My next step was to define the "mysn" element. A "mysn" is actually a
collection of my custom elements, each of which has a XHTML (browser)
representation. The simplified example below shows mysn as a
mytextfield
with a custom text label. I could have written the transform so that
the output is in terms of standard XHTML elements, but what I want to
do is define the mysn transformation in terms of my underlying
elements,
then use their transformations to create the XHTML that the browser
will
display nicely. This is also because I have a case where my original
markup will be written in terms of the more abstract elements, such as
mysn, but I need to transform it into the less abstract but still
custom
elements (mytextfield).

For instance, if this is my original (simplified) markup:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="myxform.xsl" type="text/xsl"?>
<!DOCTYPE html SYSTEM "mydtd.dtd">
<html><head></head>
<body><form>
<p><mytextfield size="30" name="FirstName" />First Name</p>
<mysn />
</form></body>
</html>
I would like the intermediate transform to be:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="myxform.xsl" type="text/xsl"?>
<!DOCTYPE html SYSTEM "mydtd.dtd">
<html><head></head>
<body><form>
<p><mytextfield size="30" name="FirstName" />First Name</p>x
<p><mytextfield name="NewSerialNum" size="40"/>Please enter your
serial number</p>
</form></body>
</html>

But I want the transform that the browser sees to be:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="myxform.xsl" type="text/xsl"?>
<!DOCTYPE html SYSTEM "mydtd.dtd">
<html><head></head>
<body><form>
<p><input type="text" size="30" name="FirstName" />First Name</p>
<p><input type="text" name="NewSerialNum" size="40"/>Please enter
your serial number</p>
</form></body>
</html>

Here is the form of the XSL stylesheet that I want to use:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" version="4.0" />

<!--================================================== ======
Template for matching an mytextfield element in a form.
Morph it into a text input, copying some of the attributes.
================================================== ==========-->
<xsl:template match="form//mytextfield">
<xsl:element name="input">
<xsl:attribute name="type">
<xsl:text>text</xsl:text>
</xsl:attribute>

<xsl:for-each select="@id | @size | @maxlength | @name">
<xsl:attribute name="{name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:for-each>

</xsl:element>
</xsl:template>

<!--=================================================
Template for matching an mysn elementiwithin a form.
Turn it into a mytextfield with a known name
================================================== ====-->
<xsl:template priority="1.0" match="form//mysn">
<xsl:element name="p">
<mytextfield name="NewSerialNum" size="40"/>
<xsl:text>Please enter your serial number</xsl:text>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

The actual usage scenario is much more complicated than this. People
that I do not know and cannot easily interact with will be writing the
markup. I need a way to validate what they write, but I would really
like it if they could just display it with a web browser.

I have spent a couple of days reading and searching the web, looking
for examples. I have also tried using entitities, <xsl:include>, and
variations on named templates. It looks like XSLT version 2.0 will
handle this case, but that 1.0 probably can't. Does anyone know of
some clever way to implement such two stage transformations using
version 1.0?

Thanks and sorry for the long post,
Rush

Jul 20 '05 #1
3 1540
<xsl:template match="form//mytextfield">
<xsl:element name="input">
<xsl:attribute name="type">
<xsl:text>text</xsl:text>
</xsl:attribute>

<xsl:for-each select="@id | @size | @maxlength | @name">
<xsl:attribute name="{name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:for-each>

</xsl:element>
</xsl:template>
That could be written much more simply as

<xsl:template match="mytextfield">
<input type="text">
<xsl:copy-of select="@id | @size | @maxlength | @name"/>
</input>
</xsl:template>
To answer your question:

It looks like XSLT version 2.0 will
handle this case, but that 1.0 probably can't. Does anyone know of
some clever way to implement such two stage transformations using
version 1.0?

the standard XSLT 1.0 way to do two transforms is just simply to use two
xslt stylesheets and call one then the other. Even in a browser setting
this is possible with a bit of javascript.

Alternatively you may be able to do the two transforms within one
stylesheet by first transforming into a varibale and then applying the
templates of the second transform to that variable. this however relies
on a node-set() extension function to convert the result tree fragment
in the variable to a node set suitable for input to the second pass.
Pretty much every XSLT engine has this extension (the notable exception
being the transformiix engine in mozilla)

David
Jul 20 '05 #2
Thanks for the reply, David. Comments and followup questions are
inline, if you care to spend any more time on this. I am new to
this XML/XSL stuff and I don't yet know what all the pieces are,
much less how to put them all together. I really appreciate the help.
That could be written much more simply as
<xsl:template match="mytextfield">
<input type="text">
<xsl:copy-of select="@id | @size | @maxlength | @name"/>
</input>
</xsl:template>

This is cool! I didn't realize you could use copy-of with attributes.
Thanks.

To answer your question:
It looks like XSLT version 2.0 will
handle this case, but that 1.0 probably can't. Does anyone know of
some clever way to implement such two stage transformations using
version 1.0?
the standard XSLT 1.0 way to do two transforms is just simply to use

two xslt stylesheets and call one then the other. Even in a browser setting this is possible with a bit of javascript.
You wouldn't care to elaborate a little bit here, would you?

Alternatively you may be able to do the two transforms within one
stylesheet by first transforming into a varibale and then applying the templates of the second transform to that variable. this however relies on a node-set() extension function to convert the result tree fragment in the variable to a node set suitable for input to the second pass.
Pretty much every XSLT engine has this extension (the notable exception being the transformiix engine in mozilla)

David


I found this:
http://www-106.ibm.com/developerwork...tipxsltmp.html
which showed exactly how to do this with node-set(). I tried it using
xsltproc
and it worked like a charm! Firefox won't deal with it (as you
mentioned),
but I can figure out some way to deal with that.

- Rush

Jul 20 '05 #3
this is possible with a bit of javascript.
You wouldn't care to elaborate a little bit here, would you?

The js interface to XSLT in moz is here

http://www.mozilla.org/projects/xslt/js-interface.html

IE has something similar.
Basically the idea is you use that interface to apply your first
transform, you then get a DOM which you stuff into a second transform
that produces a DOM with (X)HTML elements which you then allow to drop
through to the browser's renderer.

David
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jeff Rubard | last post by:
I am curious to know whether anyone has experience using XSLT for web XML (non-XHTML) styling, either with CSS or standalone. I myself have engaged in rather unsuccessful experiments with the...
2
by: sneill | last post by:
Hello all, In the August 2003 edition of "XML Journal" (see: http://xml.sys-con.com/read/40671.htm) Pietro Michelucci wrote an interesting article entitled "Object-Oriented XSLT". The article...
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...
2
by: RJN | last post by:
Hi Can XSLT be applied for transforming .txt files or are they applicable only for xml to xml transformation. Currently all the clients send file to a server in one particular format. The...
3
by: Teksure | last post by:
Hi group, searching in the Internet I found two products for XML which incorporate a very robust debugger for XSL/XSLT, I would like you to see these products and then, give me your opinion about...
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: Random | last post by:
Can anyone provide a good example of the simplest way to do multiple transformations on an xml file? I start out with an xml string and three physical xslt files, and want to transform the xml...
13
by: Toby Newman | last post by:
I started learning XML on Monday. I have an XML file and have written an XSL file to render it to HTML for formatted viewing in a browser. I'd like to create a second alternative view of the...
4
by: sadc1986 | last post by:
How does one introduce multiple worksheets in a excel using Xslt Transforms my code... Please suggest <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.