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
utput 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
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
<!--======================================================== 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