Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DTD and XSLT for HTML Script Tag

Status
Not open for further replies.

paulx82

Technical User
Feb 11, 2002
21
AU
I'm in the process of converting my web site from XHTML/CSS to XML/XSLT/CSS. I want to use a single XSLT template for all my XML docs. I can easily add JavaScripts to my XSLT template file, but I don't know how I can add different JavaScripts to my different XML documents, have the XML documents transformed and the JavaScripts work in the resulting documents.

I'm creating my own tags in my dtd, but they are pretty much based on HTML. My DTD excerpt for the script tag is as follows:
<!ELEMENT script (#PCDATA)>
<!ATTLIST script
type CDATA #REQUIRED
src CDATA #IMPLIED
>

I probably will only be referencing external JavaScripts, so I guess I could of done the following:
<!ELEMENT script EMPTY>
<!ATTLIST script
type CDATA #REQUIRED
src CDATA #REQUIRED>

I think my first DTD would enable both inline JavaScript and external javascript files to be referenced, but I'm not sure.

The XSLT code I have tried to use is:
<xsl:template match=&quot;script&quot;>
<xsl:element name=&quot;script&quot;>
<xsl:attribute name=&quot;type&quot;>
<xsl:value-of select=&quot;@type&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;@src&quot;/>
</xsl:attribute>
<xsl:comment>comment inserted for Internet Explorer</xsl:comment>
<xsl:value-of select=&quot;.&quot;/>
</xsl:element>
</xsl:template>

The <xsl:comment> is because someone mentioned that IE has problems with <script> </script> tags that have nothing in between them.

Of course, my method doesn't work. I can't get my head round XSL, yet.

I won't paste all of my xml,dtd and xslt file, because they are too big, but below is some code that may be relevant.

The start of my xslt file:
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>
<xsl:eek:utput method=&quot;xml&quot; indent=&quot;no&quot; doctype-public=&quot; doctype-system=&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; omit-xml-declaration=&quot;no&quot; xml:space=&quot;preserve&quot;/>
<xsl:template match=&quot;/&quot;>
<html xmlns=&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;>

My current dtd for the script tag:
<!ELEMENT script (#PCDATA)>
<!ATTLIST script
type CDATA #REQUIRED
src CDATA #IMPLIED
>

Part of my XML document:
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; standalone=&quot;no&quot;?>
<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;<!DOCTYPE awddoc SYSTEM &quot;<awddoc>
<head>
<title>title</title>
<script type=&quot;text/javascript&quot; src=&quot;/inc/js/no_right_click.js&quot;>
</script>

I'm a little confused about the use of CDATA sections. My external Javascript file contains
//<![CDATA[
<!--//
at the start of the script
//-->
//]]>
at the end of the script

The script I am using worked before I tried to convert to xml/xslt, so that is not an issue. Any help and/or pointers to specific tutorials on this issue would be most appreciated.

Cheers

The discipline used to write things down is the first step in making them a reality.
 
I think what IE doesn't like is a leaf <script> element:

<script type=&quot;text/javascript&quot; src=&quot;/inc/js/no_right_click.js&quot; />

If you put anything between the <script> tags, the browser will probably ignore your reference to the external script because it is assuming that there is data internally.

Try just using <script type=&quot;text/javascript&quot; src=&quot;/inc/js/no_right_click.js&quot;></script>

with no comments in between.
 
Judging by the code snippet you've posted, your XML file seems to be a standard (X)HTML file with some tag names changed to protect the innocent. What's the point? You might as well use an XHTML document and have done with it.

If you really want to go XML-XSL-HTML, use the XML to store the content of your site. Build a DTD based on the structure of that content. Forget about HTML and Javascripts and such presentational matters. Then in the XSL template(s), read the XML and transform it into HTML, adding javascripts (referenced from seperate files) as appropriate. The whole point is, if you change something on the presentation layer - like a javascript call - you DON'T have to change all your source XML.

-- Chris Hunt
Extra Connections Ltd

The real world's OK for a visit, but you wouldn't want to LIVE there!
 
Thanks for your comment, mulligh. I'll fiddle around with the script tags and see if it is definitely an IE issue.

Chris, I take your view on board. At first glance, it doesn't seem worthwhile converting from XHTML. I was planning to store my data in XML and use XSL/CSS solely for presentation. However, I think there are some cases where I would want to add a script to an isolated document. I prefer to have the option of using a script tag then have to use a seperate XSL file for that one document. I guess it is breaking the whole &quot;seperation of content and presentation&quot; motto, but I am still in the transistion phase. I guess I could use some logic in the XSL file to determine whether or not I want a script to be processed; I am just not that adept in XSL yet.

The discipline used to write things down is the first step in making them a reality.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top