473,659 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSL Error with document() function

Can someone please explain why this happens?
The expected output is 3, but uncommenting line 7 makes the output 0.
Why ???

VB.NET code: ** note the commented line, this is the culprit **
Dim xsl As New System.Xml.Xsl. XslTransform()
Dim xw As New System.IO.Strin gWriter()
Dim xmldoc As New System.Xml.XmlD ocument()
Dim xsldoc As New System.Xml.XmlD ocument()
xmldoc.Load("te st.xml")
xsldoc.Load("te st.xsl")
'xsldoc.LoadXml (xsldoc.OuterXm l)
xsl.Load(xsldoc .CreateNavigato r)
xsl.Transform(x mldoc.CreateNav igator, Nothing, xw)
Console.Write(x w.ToString)
XML input: (test.xml)
<xml>
<node1 val="hello world" />
</xml>

XSL input: (test.xsl)
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(d ocument('')//*)" />
</xsl:template>
</xsl:stylesheet>

Please explain why this happens, I do not want to use any file I/O in my
program!
Thanks.

--scott
Nov 12 '05 #1
17 4603
Hi

What are you trying to achieve here - is it the count of elements inside the
source document, or the stylesheet? From the MSXML reference:

If an empty string is passed to the document() function, the result is the
source XML of the XSLT document itself, unless the second argument is given
(and is not null). In the latter case, the URL of the document is the base
URL of the node contained in the second element.

So if you use an empty string for the document function, then the context is
actually the stylesheet, not the source document.

So leave the document('') function out!

HTH

Nigel

"rox.scott" wrote:
Can someone please explain why this happens?
The expected output is 3, but uncommenting line 7 makes the output 0.
Why ???

VB.NET code: ** note the commented line, this is the culprit **
Dim xsl As New System.Xml.Xsl. XslTransform()
Dim xw As New System.IO.Strin gWriter()
Dim xmldoc As New System.Xml.XmlD ocument()
Dim xsldoc As New System.Xml.XmlD ocument()
xmldoc.Load("te st.xml")
xsldoc.Load("te st.xsl")
'xsldoc.LoadXml (xsldoc.OuterXm l)
xsl.Load(xsldoc .CreateNavigato r)
xsl.Transform(x mldoc.CreateNav igator, Nothing, xw)
Console.Write(x w.ToString)
XML input: (test.xml)
<xml>
<node1 val="hello world" />
</xml>

XSL input: (test.xsl)
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(d ocument('')//*)" />
</xsl:template>
</xsl:stylesheet>

Please explain why this happens, I do not want to use any file I/O in my
program!
Thanks.

--scott

Nov 12 '05 #2
Yes, I want count of the stylesheet nodes.
This is a simplification of my real-world case, I don't actually want a node
count, but illustrates the problem.

In either case, I should not get 0 as a result.

--scott

"Nigel Armstrong" wrote:
Hi

What are you trying to achieve here - is it the count of elements inside the
source document, or the stylesheet? From the MSXML reference:

If an empty string is passed to the document() function, the result is the
source XML of the XSLT document itself, unless the second argument is given
(and is not null). In the latter case, the URL of the document is the base
URL of the node contained in the second element.

So if you use an empty string for the document function, then the context is
actually the stylesheet, not the source document.

So leave the document('') function out!

HTH

Nigel

"rox.scott" wrote:
Can someone please explain why this happens?
The expected output is 3, but uncommenting line 7 makes the output 0.
Why ???

VB.NET code: ** note the commented line, this is the culprit **
Dim xsl As New System.Xml.Xsl. XslTransform()
Dim xw As New System.IO.Strin gWriter()
Dim xmldoc As New System.Xml.XmlD ocument()
Dim xsldoc As New System.Xml.XmlD ocument()
xmldoc.Load("te st.xml")
xsldoc.Load("te st.xsl")
'xsldoc.LoadXml (xsldoc.OuterXm l)
xsl.Load(xsldoc .CreateNavigato r)
xsl.Transform(x mldoc.CreateNav igator, Nothing, xw)
Console.Write(x w.ToString)
XML input: (test.xml)
<xml>
<node1 val="hello world" />
</xml>

XSL input: (test.xsl)
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(d ocument('')//*)" />
</xsl:template>
</xsl:stylesheet>

Please explain why this happens, I do not want to use any file I/O in my
program!
Thanks.

--scott

Nov 12 '05 #3
rox.scott wrote:
Can someone please explain why this happens?
The expected output is 3, but uncommenting line 7 makes the output 0.


When you load XSLT from a string, it has no base URI and relatiev URIs
cannot be resolved therefore.
Don't load XSLT from a string or if you do need, provide base URI:
xslt.Load(new XmlTextReader(" foo.xsl", new StringReader(st rXSL)));

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4
Nigel Armstrong wrote:
So if you use an empty string for the document function, then the context is
actually the stylesheet, not the source document.

So leave the document('') function out!


In fact document('') is quite useful technique, which enables XSLT
introspection - a stylesheet can process itself. E.g. this is commonly
used for embedding loopup tables within a stylesheet.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #5
It is strange, this all works just fine using MSXML4 objects instead of XML.NET
I guess between the implementation of MSXML4 and XML.NET they forgot the
purpose of the special case document('').

Obviously if I were capable of resolving a URI then I would just use
document(URI) instead of empty string. I imagine the original programmers
thought of this case and that is why the document('') was originally offered.
I am not sure of its value anymore. When I provide a base URI as in Oleg's
code above, the content of the XSL in memory is ignored and the resolved URI
is used instead.

W3C spec or not, it is too bad that XML.NET is intrinsically tied to the
file system. My program has access neither to write nor read from the file
system. I guess I will use MSXML4.

Thank you.
"Oleg Tkachenko [MVP]" wrote:
rox.scott wrote:
Can someone please explain why this happens?
The expected output is 3, but uncommenting line 7 makes the output 0.


When you load XSLT from a string, it has no base URI and relatiev URIs
cannot be resolved therefore.
Don't load XSLT from a string or if you do need, provide base URI:
xslt.Load(new XmlTextReader(" foo.xsl", new StringReader(st rXSL)));

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #6
Hi Rox

I think this may be classed as a bug - I've done some limited testing using
the System.Xml.Quer y.XsltCommand in .NET 2.0 as well...there is an improvment
from .NET 1.0/1.1, in that the Exception messages are more clear, but I don't
think that the issue is fixed.

I know you can't have File IO, but as an alternative, could you return your
stylesheets from a URL using ASP.NET to hook into your (I guess) database
storage.

Nigel

"rox.scott" wrote:
It is strange, this all works just fine using MSXML4 objects instead of XML.NET
I guess between the implementation of MSXML4 and XML.NET they forgot the
purpose of the special case document('').

Obviously if I were capable of resolving a URI then I would just use
document(URI) instead of empty string. I imagine the original programmers
thought of this case and that is why the document('') was originally offered.
I am not sure of its value anymore. When I provide a base URI as in Oleg's
code above, the content of the XSL in memory is ignored and the resolved URI
is used instead.

W3C spec or not, it is too bad that XML.NET is intrinsically tied to the
file system. My program has access neither to write nor read from the file
system. I guess I will use MSXML4.

Thank you.
"Oleg Tkachenko [MVP]" wrote:
rox.scott wrote:
Can someone please explain why this happens?
The expected output is 3, but uncommenting line 7 makes the output 0.


When you load XSLT from a string, it has no base URI and relatiev URIs
cannot be resolved therefore.
Don't load XSLT from a string or if you do need, provide base URI:
xslt.Load(new XmlTextReader(" foo.xsl", new StringReader(st rXSL)));

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #7
rox.scott wrote:
It is strange, this all works just fine using MSXML4 objects instead of XML.NET
It works fine in .NET if you provide base URI for your stylesheet.
I guess between the implementation of MSXML4 and XML.NET they forgot the
purpose of the special case document('').
There is nothing special about document('') - it's just zero-length
realtive URI to be resolved.
Obviously if I were capable of resolving a URI then I would just use
document(URI) instead of empty string. I imagine the original programmers
thought of this case and that is why the document('') was originally offered.
I am not sure of its value anymore. When I provide a base URI as in Oleg's
code above, the content of the XSL in memory is ignored and the resolved URI
is used instead.
You seems to be working with MSXML too much. There is no such thing as
XSL in memory. In .NET as well as in many other XSLT implementations
XSLT is being compiled to binary representation prior to transformation
and this binary compiled stylesheet of course doesn't contain source
document.
W3C spec or not, it is too bad that XML.NET is intrinsically tied to the
file system.
It doesn't.
My program has access neither to write nor read from the file
system. I guess I will use MSXML4.

You can easily workaround this problem in .NET having source of the XSLT
stylesheet stored in some form (string or XPathDocument) and providing
custom XmlResolver, which resolves "" to that document (return it as
Stream or XPathNavigator in GetEntity() method).

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #8
Nigel Armstrong wrote:
I think this may be classed as a bug - I've done some limited testing using
the System.Xml.Quer y.XsltCommand in .NET 2.0 as well...there is an improvment
from .NET 1.0/1.1, in that the Exception messages are more clear, but I don't
think that the issue is fixed.


I don't think that's a bug. That's by design. XSLT allows such behaviour
when compiled XSLT stylesheet has no source representation - e.g. when
you compile C++ code to exe, there is no way for exe to return you
source C++ code, right? So why in XSLT it's a bug?

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #9
Hi Oleg

I've just written some sample code supplying a base URI when loading the
stylesheet, and it doesn't seem to be working for me. Perhaps you could take
a look at it!

Nigel

test.xml
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='test.xsl' ?>
<root><child>te xt</child></root>

test.xsl
<?xml version='1.0'?>
<xsl:styleshe et version='1.0'
xmlns:xsl='http ://www.w3.org/1999/XSL/Transform' >
<xsl:template match='/'>
<root><xsl:valu e-of select='count(d ocument(&apos;& apos;)//*)'/></root>
</xsl:template>
</xsl:stylesheet>

Works:
Dim r As New MyResolver

'Dim s As New Xml.XmlTextRead er("file:///C:/test.xsl", New
IO.FileStream(" ../test.xsl", IO.FileMode.Ope n))
Dim d As New Xml.XmlDocument
d.Load("../test.xml")

Dim t As New Xml.Xsl.XslTran sform
t.Load("../test.xsl", r)
't.Load(s, r, Nothing)
Dim sw As New IO.StringWriter

t.Transform(d, Nothing, sw, r)

MessageBox.Show (sw.ToString())
Doesn't work:
Dim r As New MyResolver

Dim s As New Xml.XmlTextRead er("file:///C:/test.xsl", New
IO.FileStream(" ../test.xsl", IO.FileMode.Ope n))
Dim d As New Xml.XmlDocument
d.Load("../test.xml")

Dim t As New Xml.Xsl.XslTran sform
't.Load("../test.xsl", r)
t.Load(s, r, Nothing)
Dim sw As New IO.StringWriter

t.Transform(d, Nothing, sw, r)

MessageBox.Show (sw.ToString())
Custom resolver that always returns a reference to the same stylesheet
Public Class MyResolver
Inherits Xml.XmlResolver

Public Overrides WriteOnly Property Credentials() As
System.Net.ICre dentials
Set(ByVal Value As System.Net.ICre dentials)

End Set
End Property

Public Overrides Function GetEntity(ByVal absoluteUri As System.Uri,
ByVal role As String, ByVal ofObjectToRetur n As System.Type) As Object
Return New IO.FileStream(" ..\test.xsl", IO.FileMode.Ope n)
End Function
End Class

"Oleg Tkachenko [MVP]" wrote:
rox.scott wrote:
It is strange, this all works just fine using MSXML4 objects instead of XML.NET


It works fine in .NET if you provide base URI for your stylesheet.
I guess between the implementation of MSXML4 and XML.NET they forgot the
purpose of the special case document('').


There is nothing special about document('') - it's just zero-length
realtive URI to be resolved.
Obviously if I were capable of resolving a URI then I would just use
document(URI) instead of empty string. I imagine the original programmers
thought of this case and that is why the document('') was originally offered.
I am not sure of its value anymore. When I provide a base URI as in Oleg's
code above, the content of the XSL in memory is ignored and the resolved URI
is used instead.


You seems to be working with MSXML too much. There is no such thing as
XSL in memory. In .NET as well as in many other XSLT implementations
XSLT is being compiled to binary representation prior to transformation
and this binary compiled stylesheet of course doesn't contain source
document.
W3C spec or not, it is too bad that XML.NET is intrinsically tied to the
file system.


It doesn't.
My program has access neither to write nor read from the file
system. I guess I will use MSXML4.

You can easily workaround this problem in .NET having source of the XSLT
stylesheet stored in some form (string or XPathDocument) and providing
custom XmlResolver, which resolves "" to that document (return it as
Stream or XPathNavigator in GetEntity() method).

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #10

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

Similar topics

1
1418
by: Miguel Cañedo | last post by:
Hi: I'm testing with fireforx 1.0 on MdkLinux10.0 I can not figure out why am I getting this error: Error: " nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "JS frame :: file:///home/mcanedo/Desktop/forma.html :: validar :: line 151" data: no] Archivo Fuente: file:///home/mcanedo/Desktop/forma.html Línea: 151
4
3793
by: ashkaan57 | last post by:
Hi, I am using the following code to show/hide part of an html page. It works in Netscape and Firefox but dies in IE: "Error: document.layers is null or not an object" <style> ..noshow { display: none; } ..menu {
1
3746
by: IkBenHet | last post by:
Hello, I found this script to create a simple rich text form (http://programmabilities.com/xml/index.php?id=17): <html> <head> <title>Rich Text Editor</title> </head> <body>
9
2507
by: torbs | last post by:
Hi I am creating a page for stretchfilm. In this page you can chose between a short medium and long version of the film at any point in the film. It is in norwegian, but just press play. URL: http://www.media.uio.no/sherlockholmes/main.php?plugin=quicktime&hastighet=lav
7
16271
by: dd | last post by:
Hello all. First, I'm a newbie to javascript but not to ASP. Secondly I've been searching for the answer for two hours and I decided to finally post this question. I'm trying to take a javascript and make it work with asp. Any thought, ideas or pointers in the right direction would be greatly appreciated. I am getting the following error: Microsoft JScript runtime error '800a1391'
5
4431
by: HopfZ | last post by:
I made two shortcut functions for document.getElementById as: function EBI2(id){return document.getElementById(id)}; var EBI3 = document.getElementById; But EBI3 don't work. EBI2('myText'); //works EBI3('myText'); //unexpected error
7
3601
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
4
2627
by: dr1ft3r | last post by:
Hey guys, I'm building a site for a landscaping business down the street and can't seem to get part of the code functioning correctly. The code fails on line 68 where I make a reference to an iframe's src property. Being that IE does not follow standard and considers an id, name, etc as a qualifying identifier for the document.getElementById object, I double checked to make sure that there's only one instance of id = "servif" and I never use...
1
3879
by: mudasserrafiq | last post by:
I am using following asp file default.asp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <META http-equiv=Content-Type content="text/html; charset=windows-1252"> <META content="0 Days" name=revisit-after> <META content=info@siderinternational.com name=email> <META content="Omer Safdar" name=author> <META content=MegaStudios.com name=publisher> <META content="Copyright ©2005 - MegaStudios.com" name=copyright> <SCRIPT...
9
3185
by: ahilar12 | last post by:
1. <head> 2. <script type="text/javascript"> 3. </script> 4. </head> 5. <body> 6. <form> 7. <select name="team" id="mylist" > 8. <option></option> 9. <option>north</option> 10. <option>south</option>
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8525
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.