Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 15th, 2006, 01:15 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default schema stops stylesheet from working?

When I link my XML document to an external schema (Docbook 5.0b5 schema) my stylesheet no longer works.

Why is this?

My XML looks like this

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="basic.xslt"?>
<article xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://docbook.org/ns/docbook
docbook.xsd">
    <title>trying out</title>
    <para>first paragraph</para>
    <para>second paragraph</para>
</article>


My Stylesheet looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <html>
        <head>
            <title></title>


        </head>

        <body>

        <xsl:apply-templates/>


        </body>

    </html>
</xsl:template>
<xsl:template match="title">

<h1><xsl:apply-templates select="text()"/></h1>
</xsl:template>

<xsl:template match="para">

<p><xsl:apply-templates select="text()"/></p>
</xsl:template>


</xsl:stylesheet>



Reply With Quote
  #2 (permalink)  
Old June 15th, 2006, 02:27 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's not linking to a schema that's the problem, it's changing the namespace. Your document no longer contains elements called "title" and "para", it now contains elements called "docbook:title" and "docbook:para", where "docbook" represents the namespace http://docbook.org/ns/docbook. Search for "XSLT matching default namespace".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old June 15th, 2006, 12:04 PM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK - thanks, I worked it out. If I add xmlns:ns="http://docbook.org/ns/docbook" to the stylesheet, and add a "ns:" when referring to an element, it works. But what about this:

I have javascript in an html page which associates different stylesheets to a xml document object depending on what button is pressed.
.................................................. .....
<html>
   <head>
      <title>testing</title>

      <script language="JavaScript">


var processor; /* will hold reference to XSLT processor object */
var dataXML; /* will hold reference to XML document object */
var xslfile; /* variable to hold string reference to xslt file*/
var newStart = "true"; /* variable to check whether buttons must be enabled*/

 /*Start() function is called in opening </body> tag*/

function Start()
{

   processor = new XSLTProcessor(); /* a new XSLT processor object is declared and associated with variable "processor"*/

    loadData (); /* function to create empty XML document */

   xslfile = 'notes.xslt'; /* default stylesheet assigned*/
}


/* this is called by the Start() function */

function loadData ()
{

  /* creates a new empty XML document object and associates it with
  * the variable dataXML*/

   dataXML = document.implementation.createDocument("", "", null);

   /*below sets the loading of the xml file to be
   * synchronous - the function waits until the document
   * has finished loading before it returns the document
   * and the processing continues.
   *Otherwise it would be neccesary to check if the document
   * has finished loading*/

   dataXML.async = false;


   dataXML.load("notes.xml"); /* uses the "load()" method of DOM interface to load the XML document "notes.xml" into the document object dataXML */

}


/* the WriteHTML function is called by the buttons */

function WriteHTML ( afile )
{
   xslfile = afile; /* the current stylesheet is passed to the WriteHTML function*/
   refresh(); /* function defined below which creates a new XML document object and uses it to switch stylesheets*/
}

/* the refresh() function is called by the
*WriteHTML() function, which in turn is called
* buttons*/

function refresh()
{
   var dataXSL = document.implementation.createDocument("", "", null); /* a new XML document object is created and associated with variable "dataXSL"*/
   dataXSL.async = false; /* set loading to be synchoronous */
   dataXSL.load(xslfile); /* associate the current xslt file with dataXSL object*/

   processor.reset(); /* reset() method of XSLT processor object removes all parameters and stylesheets from the XML object "processor"*/
   processor.importStylesheet(dataXSL); /*importStylesheet() method of XSLT processor imports the stylesheet associated with dataXSL into "processor"*/

   var result = processor.transformToDocument(dataXML, document); /*"result" is new document object. dataXML is transformed */

   /*part of the browser document is removed and replaced with the transformed document*/

    document.getElementById ("here").removeChild (document.getElementById ("here").firstChild);
   document.getElementById ("here").appendChild(result.firstChild);
}
</script>

<meta content="MSHTML 6.00.2800.1528" name=GENERATOR></head>

<body onload=Start()>
<form>

<button onclick="WriteHTML('notesa.xslt'); return false">Generate Ascending</button>

<button onclick="WriteHTML('notesd.xslt'); return false">Generate Descending</button>

</form>
<div id=here>blank</div>

</body></html>
.............................................
If the XML is something like this:
.............................................
<?xml version="1.0" encoding="UTF-8"?>

<article xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://docbook.org/ns/docbook
docbook.xsd">
    <title/>
    <para>a</para>
    <para>b</para>
    <para>c</para>
</article>

.................................................. ...............
And the stylesheets are something like this:
.................................................. ...............
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://docbook.org/ns/docbook">


<xsl:template match="/">

<html>
    <head>
        <title></title>
    </head>
    <body>

    <xsl:apply-templates select="//para">

    <xsl:sort select="." order="descending"/>

    </xsl:apply-templates>


    </body>
</html>


</xsl:template>

<xsl:template match="ns://para">
<h1><xsl:apply-templates select="text()"/></h1>
</xsl:template>

</xsl:stylesheet>
.................................................. ...............

What is wrong with the namespace here?


Reply With Quote
  #4 (permalink)  
Old June 15th, 2006, 12:34 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to add the namespace prefix to your select.
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://docbook.org/ns/docbook">


<xsl:template match="/">

<html>
    <head>
        <title></title>
    </head>
    <body>

    <xsl:apply-templates select="//ns:para">

    <xsl:sort select="." order="descending"/>    

    </xsl:apply-templates>


    </body>
</html>


</xsl:template>

<xsl:template match="ns://para">
<h1><xsl:apply-templates select="text()"/></h1>
</xsl:template>

</xsl:stylesheet>
If your two stylesheets just vary by the sort order attribute it would be more elegant to use a parameter:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://docbook.org/ns/docbook">
<xsl:variable name="sortOrder" select="'ascending'"/>
<xsl:template match="/">

<html>
    <head>
        <title></title>
    </head>
    <body>

    <xsl:apply-templates select="//ns:para">

    <xsl:sort select="." order="{$sortOrder}"/>    

    </xsl:apply-templates>


    </body>
</html>


</xsl:template>

<xsl:template match="ns://para">
<h1><xsl:apply-templates select="text()"/></h1>
</xsl:template>

</xsl:stylesheet>
You can then modify the parameter before transforming if you need descending.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #5 (permalink)  
Old June 15th, 2006, 02:02 PM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh - ok got that.

In my actual working example, the xml gets sorted according to the value of either of two attributes on an element:

<section sensing = 1 intuitive = 2>
</section>

<section sensing = 2 intuitive = 1>
</section>

One stylesheet sorts according to "sensing" and the other according to "intuitive". Depending on which button gets clicked the stylesheets get switched and the xml gets sorted according to a different attribute.

Would I be able to use your suggested method - of using a variable in my xslt stylesheet in such a situation as well? I guess I would have to set the value of that variable in the xslt stylesheet to "sensing" or "intuitive" depending on what button gets pressed in the html page. How would I go about doing that?


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IE6: Hover stops working on AJAX page sigilaea Ajax 0 March 17th, 2008 09:58 AM
Schema to Schema conversion onlyda XSLT 5 March 23rd, 2006 12:25 PM
CDONTS sendmail stops working suddenly Cinderella Classic ASP Basics 1 March 9th, 2006 08:45 PM
XPathExpression.AddSort not working with Schema mokka°logic XML 0 September 10th, 2003 01:21 PM



All times are GMT -4. The time now is 05:53 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.