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!

Paging XML with XSL

Status
Not open for further replies.

fukudahs

Programmer
Sep 19, 2002
26
BR
Hi everyone!
I have got the following files "x.xml" and "x.xsl".
The x.xsl was supposed to transform the x.xml to show me a html in my brownser... I would show me the a page &quot;paged&quot; like &quot;<<Prev 1 2 3 4 5 Next>>&quot;.
But when I open the x.xml in my brownser, the follow message appear:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

Keyword xsl:script may not be used in namespace
Can somebody help me to solve this problem?
I there is no solution, can somebody send me a example of how to create a &quot;paging&quot; xsl???

Tks in advance!

cogubr@hotmail.com
Humberto



--------------------------------------------
x.xml
--------------------------------------------
<?xml version=&quot;1.0&quot;?>
<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;x.xsl&quot;?>
<series>
<page>
<title>Client-side Paging.</title>
<blerb>
<p>
I often get email asking me &quot;How do I only display parts of a page
on the client.&quot; or &quot;How do I page through some XML on the client&quot;.
</p>
<p>
I am going to use this tutorial as an example. If you look at the
<a href=&quot;Javascript:sourceWindow('slider.xml_')&quot;>XML</a> you will see many pages in the series but only
the first one (this page) is displayed.
</p>
<p>
This is achieved by passing a parameter to the stylesheet
<code><xsl:param name=&quot;pagenumber&quot; select=&quot;1&quot; /></code>
then at the top level only applying templates to the parts you
want displayed i.e.
<code><xsl:apply-templates select=&quot;page[$pagenumber]&quot; /></code>
I have set the parameter to 1 (select=&quot;1&quot;) which instructs the
XSLT processor to use 1 if no parameter is passed to the stylesheet.
So the first time this XML is opened
the first page is displayed. See section x on how to change
this. You could change this manualy in the stylesheet to
display any page when the XML is opened.
</p>
</blerb>
</page>
<page>
<title>Displaying Page Links</title>
<blerb>
<p>
You will often want to add links to pages at the foot of
your page as is done in most of the search engines on the web.
</p>
<p>
This can't be done simply with an <xsl:apply-templates />
unless you want to get into modes. An <xsl:call-template />
to <a href=&quot;footerpagestemplate1.xsl&quot;>footerPages</a> is used
from the <a href=&quot;seriestemplate1.xsl&quot;>series</a> template.
</p>
<p>
As you can see from the template 2 parameters are
passed &quot;pagenumber&quot; and &quot;element&quot;. The &quot;pagenumber&quot; parameter is
obvious it is the page currently being displayed. The &quot;element&quot;
parameter is the name of the element that is being paged in
this case I am using &quot;page&quot; although it could be any repeating
element that you want paged.
</p>
<p>
The output of this template is
<code> </code>
</p>
</blerb>
</page>
<page>
<title>Changing Pages</title>
<blerb>
<p>
You can see from the <a href=&quot;footerpagestemplate1.xsl&quot;>footerPages</a>
template that it uses an anchor to change the page.
This would be ok if it pointed to some server CGI which served up
the next page i.e.
<code><a href=&quot;slider.asp?pagenumber=8&quot;>8</a></code>
But we are trying to do this client-side.
</p>
<p>
There are 2 solutions to this the first is to have the anchor point
to a JavaScript function
which will reprocess the XML passing in the &quot;pagenumber&quot; parameter.
So you can add either this
<code><a href=&quot;JavaScript:changePage({$pagenumber +1});&quot;>Next</a></code>
or this
<code><a><br/>
<xsl:attribute name=&quot;href&quot;>JavaScript:changePage(<xsl:value-of select=&quot;$pagenumber+1&quot; />);<xsl:attribute><br />
Next</a>
</code>
to the template.
Here is the resulting <a href=&quot;footerpagestemplate1.xsl&quot;>footerPages</a>
template.
</p>
</blerb>
</page>
<page>
<title>The JavaScript</title>
<blerb>
<p>
The JavaScript is included into the HTML by the top level
<a href=&quot;seriestemplate2.xsl&quot;>series</a> template. Clicking on the &quot;Prev&quot;,
&quot;Next&quot; or number links causes the changePage function to be called with
the pagenumber passed as a parameter.
</p>
<p>
When you load an XML document with an associated stylesheet into IE
the properties document.XMLDocument and XSLDocument are available and
they point to the parsed XML and XSL DOM objects. These can be used
to reprocess the document. Here is some pseudo code on how to do it.
<code><pre>
function changePage(number){
var x = document.XMLDocument;
var s = document.XSLDocument;
s.addParameter(&quot;pagenumber&quot;, number);
var html = x.transformNode(s);
document.write(html);
}</pre>
</code>
The <a href=&quot;slider.js_&quot;>actual code</a> isn't much longer than this.
The main difference being that a processor needs to be created so that
you can add the &quot;pagenumber&quot; parameter. Also because the XML document
has been replaced with HTML the XMLDocument XSLDocument are stored
in the navigator object.
</p>
</blerb>
</page>
<page>
<title>The Final Stylesheet</title>
<blerb>
<p>
The <a href=&quot;Javascript:showSource('slider.xsl');&quot;>stylesheet</a>
just needs a couple of other templates to bring it all
together and display this tutorial.
</p>
</blerb>
</page>
</series>

--------------------------------------
x.xsl
--------------------------------------
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot; <xsl:param name=&quot;pagenumber&quot; select=&quot;1&quot; />

<xsl:template match=&quot;*|@*&quot;>
<xsl:copy>
<xsl:apply-templates select=&quot;@* | * | comment() | processing-instruction() | text()&quot;/>
</xsl:copy>
</xsl:template>
<xsl:template match=&quot;series&quot;>
<html>
<head>
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;slider.css&quot; />
<script language=&quot;javascript&quot; src=&quot;slider.js&quot; />
</head>
<body>
<xsl:if test=&quot;1=0&quot;>
<h1 style=&quot;color:red;&quot;>You need to be in &quot;replace&quot; mode to run this!!! <a href=&quot; here</a></h1>
</xsl:if>
<xsl:apply-templates select=&quot;page[$pagenumber]&quot; />
<div class=&quot;blerb&quot;>
<xsl:call-template name=&quot;footerPages&quot;>
<xsl:with-param name=&quot;element&quot; select=&quot;'page'&quot; />
<xsl:with-param name=&quot;pagenumber&quot; select=&quot;$pagenumber&quot; />
</xsl:call-template>
</div>
</body>
</html>
</xsl:template>
<xsl:template match=&quot;page&quot;>
<xsl:apply-templates />
</xsl:template>
<xsl:template match=&quot;title&quot;>
<h1><xsl:value-of select=&quot;.&quot; /></h1>
</xsl:template>

<xsl:template match=&quot;blerb&quot;>
<div class=&quot;blerb&quot;>
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match=&quot;p&quot;>
<p>    <xsl:apply-templates /></p>
</xsl:template>
<xsl:template match=&quot;code&quot;>
<div class=&quot;code&quot;><xsl:apply-templates /></div>
</xsl:template>

<xsl:template name=&quot;footerPages&quot;>
<xsl:param name=&quot;element&quot; />
<xsl:param name=&quot;pagenumber&quot; />
<xsl:variable name=&quot;total&quot; select=&quot;count(*[name() = $element])&quot; />
<center>
<xsl:if test=&quot;$pagenumber > 1&quot;>
<a href=&quot;JavaScript:changePage({$pagenumber -1});&quot;>Prev</a>
</xsl:if>
<xsl:for-each select=&quot;*[name() = $element]&quot;>
<xsl:choose>
<xsl:when test=&quot;not(count(preceding-sibling::*)+1 = $pagenumber)&quot;>
 <a href=&quot;JavaScript:changePage({count(preceding-sibling::*)+1});&quot;><xsl:value-of select=&quot;count(preceding-sibling::*)+1&quot; /></a>
</xsl:when>
<xsl:eek:therwise>
 <xsl:value-of select=&quot;count(preceding-sibling::*)+1&quot; />
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>
<xsl:if test=&quot;$pagenumber < $total&quot;>
 <a href=&quot;JavaScript:changePage({$pagenumber +1});&quot;>Next</a>
</xsl:if>
</center>
</xsl:template>



<xsl:script><![CDATA[

function changePage(number){
try{
var s = new ActiveXObject(&quot;MSXML2.FreeThreadedDOMDocument&quot;);
var x = document.XMLDocument;
if (x == null){
x = navigator.XMLDocument;
s.loadXML(navigator.XSLDocument.xml);
}else{
s.loadXML(document.XSLDocument.xml);
}
var tem = new ActiveXObject(&quot;MSXML2.XSLTemplate&quot;);
tem.stylesheet = s;
var proc = tem.createProcessor();
proc.addParameter(&quot;pagenumber&quot;, number);
proc.input = x;
proc.transform();
var str = proc.output;

var newDoc = document.open(&quot;text/html&quot;, &quot;replace&quot;);
newDoc.write(str);
navigator.XMLDocument = x;
navigator.XSLDocument = s;
newDoc.close();
}catch(exception){
}
}



]]></xsl:script>
</xsl:stylesheet>
 
The error message is basically saying &quot;
Code:
<xsl:script>
&quot; is not part of the version of XSL(T) that you are using. The namsepace you've declared specifically says you are using XSL(T) version 1.0, and the &quot;
Code:
<xsl:script>
&quot; element is part of XSL(T) 1.1.

You can try changing the XSL(T) version number from &quot;1.0&quot; to &quot;1.1&quot; (NOT the XML version number); however, depending on the parser you are using to perform the transformation, this may not work.

If the version change doesn't work, then try putting your Javascript (currently in the CDATA seciton) into regular script tags inside the &quot;series&quot; template (similar to script tag which calls the external file 'slider.js'), use version 1.0 for your XSLT namespace, and skip the
Code:
<xsl:script>
element altogether.

Hope this helps. Kim
 
Tks a LOOOOOT Kim1234!!!

I tried many things you said (and many other things...), and finally I could make it work!!! :)))

Here is the final version:

<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot; <xsl:param name=&quot;pagenumber&quot; select=&quot;0&quot; />
<xsl:param name=&quot;linhasporpagina&quot; select=&quot;3&quot; />

<xsl:template match=&quot;/&quot;>
<style>
.default_pag
{
BACKGROUND-COLOR: #FFFFff;
COLOR: #000000;
FONT-FAMILY: Tahoma,Arial,Verdana,MS Sans Se;
FONT-SIZE: 0.75em;
TEXT-ALIGN: center;
}

.tabela_pag
{
BACKGROUND-COLOR: #FFFFff;
COLOR: #000000;
FONT-FAMILY: Tahoma,Arial,Verdana,MS Sans Se;
FONT-SIZE: 0.75em;
TEXT-ALIGN: right;
BORDER: #C6C3C6 1pt solid;
}
.celula_pag
{
BACKGROUND-COLOR: #FFFFff;
COLOR: #000000;
FONT-FAMILY: Tahoma,Arial,Verdana,MS Sans Se;
FONT-SIZE: 0.75em;
TEXT-ALIGN: right;
BORDER: #C6C3C6 1pt solid;
}
</style>



<xsl:copy>
<xsl:apply-templates select=&quot;//rh&quot;/>
</xsl:copy>
</xsl:template>
<xsl:template match=&quot;rh&quot;>
<html>
<head>
<script language=&quot;javascript&quot;>
function changePage(number,genre){
try{
var s = new ActiveXObject(&quot;MSXML2.FreeThreadedDOMDocument&quot;);
var x = document.XMLDocument;
if (x == null){
x = navigator.XMLDocument;
s.loadXML(navigator.XSLDocument.xml);
}else{
s.loadXML(document.XSLDocument.xml);
}
var tem = new ActiveXObject(&quot;MSXML2.XSLTemplate&quot;);
tem.stylesheet = s;
var proc = tem.createProcessor();
proc.addParameter(&quot;pagenumber&quot;, number);
proc.addParameter(&quot;linhasporpagina&quot;, genre);
proc.input = x;
proc.transform();
var str = proc.output;

var newDoc = document.open(&quot;text/html&quot;, &quot;replace&quot;);
newDoc.write(str);
navigator.XMLDocument = x;
navigator.XSLDocument = s;
newDoc.close();
}catch(exception){
}
}
</script>
</head>
<body>

<table>
<tr>
<td valign=&quot;top&quot; class=&quot;default_pag&quot;>N&#250;mero de linhas por p&#225;gina:</td>
<td>
<form method=&quot;post&quot; action=&quot;&quot;>
<select name=&quot;genre&quot; value=&quot;{$linhasporpagina}&quot; onchange=&quot;JavaScript:changePage(0,this.value);&quot;>
<option value=&quot;3&quot;><xsl:if test=&quot;$linhasporpagina='3'&quot;><xsl:attribute name=&quot;selected&quot;>Selected</xsl:attribute></xsl:if>3</option>
<option value=&quot;5&quot;><xsl:if test=&quot;$linhasporpagina='5'&quot;><xsl:attribute name=&quot;selected&quot;>Selected</xsl:attribute></xsl:if>5</option>
<option value=&quot;10&quot;><xsl:if test=&quot;$linhasporpagina='10'&quot;><xsl:attribute name=&quot;selected&quot;>Selected</xsl:attribute></xsl:if>10</option>
</select>
</form>
</td>
</tr>
</table>

<xsl:if test=&quot;1=0&quot;>
<h1 style=&quot;color:red;&quot;>Vc esta com a versao errada de MSXML!!! <a href=&quot; aqui!</a></h1>
</xsl:if>
<table cellspacing=&quot;0&quot; cellpadding=&quot;2 &quot; class=&quot;tabela_pag&quot;>
<xsl:for-each select=&quot;rw[position() &gt; $pagenumber*$linhasporpagina and position() &lt;= ($pagenumber*$linhasporpagina)+$linhasporpagina]&quot;>
<tr>
<xsl:for-each select=&quot;h&quot;>
<td class=&quot;celula_pag&quot;><xsl:attribute name=&quot;rowspan&quot;><xsl:value-of select=&quot;@rs&quot;/></xsl:attribute>
<xsl:attribute name=&quot;colspan&quot;><xsl:value-of select=&quot;@csp&quot;/></xsl:attribute>
<xsl:value-of select=&quot;fv&quot;/>
</td>
</xsl:for-each>
<xsl:for-each select=&quot;mv&quot;>
<td class=&quot;celula_pag&quot;><xsl:attribute name=&quot;rowspan&quot;><xsl:value-of select=&quot;@rs&quot;/></xsl:attribute>
<xsl:attribute name=&quot;colspan&quot;><xsl:value-of select=&quot;@csp&quot;/></xsl:attribute>
<xsl:value-of select=&quot;.&quot;/>
</td>
</xsl:for-each>
<xsl:for-each select=&quot;sv&quot;>
<td class=&quot;celula_pag&quot;><xsl:attribute name=&quot;rowspan&quot;><xsl:value-of select=&quot;@rs&quot;/></xsl:attribute>
<xsl:attribute name=&quot;colspan&quot;><xsl:value-of select=&quot;@csp&quot;/></xsl:attribute>
<xsl:value-of select=&quot;.&quot;/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
<xsl:call-template name=&quot;footerPages&quot;>
<xsl:with-param name=&quot;element&quot; select=&quot;'rw'&quot; />
<xsl:with-param name=&quot;pagenumber&quot; select=&quot;$pagenumber&quot; />
</xsl:call-template>

</body>
</html>
</xsl:template>

<xsl:template name=&quot;footerPages&quot;>
<xsl:param name=&quot;element&quot; />
<xsl:param name=&quot;pagenumber&quot; />
<xsl:variable name=&quot;total&quot; select=&quot;count(*[name() = $element])&quot; />
<center>
<xsl:if test=&quot;$pagenumber &gt; 0&quot;>
<a href=&quot;JavaScript:changePage({$pagenumber -1},{$linhasporpagina});&quot;>&#60;&#60;</a>
</xsl:if>

<xsl:choose>
<xsl:when test=&quot;0 = $pagenumber&quot;>
<font class=&quot;default_pag&quot;>&#160;[0]</font>
</xsl:when>
<xsl:eek:therwise>
<font class=&quot;default_pag&quot;>&#160;<a href=&quot;JavaScript:changePage(0,{$linhasporpagina})&quot;>0</a></font>
</xsl:eek:therwise>
</xsl:choose>

<xsl:for-each select=&quot;*[name() = $element]&quot;>
<xsl:if test=&quot;(count(preceding-sibling::*)+1)*$linhasporpagina &lt; $total&quot;>
<xsl:choose>
<xsl:when test=&quot;not(count(preceding-sibling::*)+1 = $pagenumber)&quot;>
<font class=&quot;default_pag&quot;>&#160;<a href=&quot;JavaScript:changePage({count(preceding-sibling::*)+1},{$linhasporpagina});&quot;><xsl:value-of select=&quot;count(preceding-sibling::*)+1&quot; /></a></font>
</xsl:when>
<xsl:eek:therwise>
<font class=&quot;default_pag&quot;>&#160;[<xsl:value-of select=&quot;count(preceding-sibling::*)+1&quot; />]</font>
</xsl:eek:therwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>

<xsl:if test=&quot;$pagenumber*$linhasporpagina+$linhasporpagina &lt; $total&quot;>
<font class=&quot;default_pag&quot;>&#160;<a href=&quot;JavaScript:changePage({$pagenumber+1},{$linhasporpagina});&quot;>&#62;&#62;</a></font>
</xsl:if>
</center>
</xsl:template>
</xsl:stylesheet>

cogubr@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top