Reading XML in Safari through selectNode

J

J_Zanetti

Hello everybody,

In my applications I've a ton of scripts that use remote XML file to
fill forms and evaluate contents; In these scripts I always use the
method SelectNode (that, with some workaround, works fine also in
Mozilla).
I've just found out that this method doesnt work in Safari browser,
therefore my applications are not usable by this browser.
Can anyone provide me any solution or workaround to be able to read XML
files in Safari wihout rewriting all of my scripts??

Thanks everyone :)
 
M

Martin Honnen

J_Zanetti said:
In my applications I've a ton of scripts that use remote XML file to
fill forms and evaluate contents; In these scripts I always use the
method SelectNode (that, with some workaround, works fine also in
Mozilla).

Neither IE using MSXML nor Mozilla have any method called SelectNode
exposed on XML DOM objects.
MSXML provides two methods, one called selectSingleNode, the other
called selectNodes. Are you using one of these?
I've just found out that this method doesnt work in Safari browser,
therefore my applications are not usable by this browser.
Can anyone provide me any solution or workaround to be able to read XML
files in Safari wihout rewriting all of my scripts??

I don't think Safari has XPath support so far thus if you really use
XPath expressions as arguments to that functions then you need to write
more code. For other simple tasks you might get away with using
getElementsByTagName e.g. you can replace
xmlDoc.selectNodes('//element-name')
with
xmlDoc.getElementsByTagName('element-name')
 
J

J_Zanetti

Martin Honnen ha scritto:
MSXML provides two methods, one called selectSingleNode, the other
called selectNodes. Are you using one of these?

Thanks for answering Martin!
Yes, Im using selectNodes. Of course it works on IE, and in Mozilla
only if i add some code that redefine the working of this method; to be
precise, i add this code at the beginning of my script

if( document.implementation.hasFeature("XPath", "3.0") )
{
// prototying the XMLDocument
XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
{
if( !xNode ) { xNode = this; }
var oNSResolver = this.createNSResolver(this.documentElement)
var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
var aResult = [];
for( var i = 0; i < aItems.snapshotLength; i++)
{
aResult = aItems.snapshotItem(i);
}
return aResult;
}
// prototying the Element
Element.prototype.selectNodes = function(cXPathString)
{
if(this.ownerDocument.selectNodes)
{
return this.ownerDocument.selectNodes(cXPathString, this);
}
else{throw "For XML Elements Only";}
}
}

This doesnt work under Safari, since Xpath is not supported there; thus
should i write something similar for Safari? Honestly, i wouldnt know
where to start from...
I don't think Safari has XPath support so far thus if you really use
XPath expressions as arguments to that functions then you need to write
more code. For other simple tasks you might get away with using
getElementsByTagName e.g. you can replace

Yes, I use xpath expressions as parameters for the selectNodes method
:(
So If i got it well, there are two options: rewriting my scripts with
getElementsByTagName or redefining selectNodes. Which way do you think
is the best one?
 
J

J_Zanetti

Martin Honnen ha scritto:
MSXML provides two methods, one called selectSingleNode, the other
called selectNodes. Are you using one of these?

Thanks for answering Martin!
Yes, Im using selectNodes. Of course it works on IE, and in Mozilla
only if i add some code that redefine the working of this method; to be
precise, i add this code at the beginning of my script

if( document.implementation.hasFeature("XPath", "3.0") )
{
// prototying the XMLDocument
XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
{
if( !xNode ) { xNode = this; }
var oNSResolver = this.createNSResolver(this.documentElement)
var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
var aResult = [];
for( var i = 0; i < aItems.snapshotLength; i++)
{
aResult = aItems.snapshotItem(i);
}
return aResult;
}
// prototying the Element
Element.prototype.selectNodes = function(cXPathString)
{
if(this.ownerDocument.selectNodes)
{
return this.ownerDocument.selectNodes(cXPathString, this);
}
else{throw "For XML Elements Only";}
}
}

This doesnt work under Safari, since Xpath is not supported there; thus
should i write something similar for Safari? Honestly, i wouldnt know
where to start from...
I don't think Safari has XPath support so far thus if you really use
XPath expressions as arguments to that functions then you need to write
more code. For other simple tasks you might get away with using
getElementsByTagName e.g. you can replace

Yes, I use xpath expressions as parameters for the selectNodes method
:(
So If i got it well, there are two options: rewriting my scripts with
getElementsByTagName or redefining selectNodes. Which way do you think
is the best one?
 
R

RobG

J_Zanetti said:
Hello everybody,

In my applications I've a ton of scripts that use remote XML file to
fill forms and evaluate contents; In these scripts I always use the
method SelectNode (that, with some workaround, works fine also in
Mozilla).
I've just found out that this method doesnt work in Safari browser,
therefore my applications are not usable by this browser.
Can anyone provide me any solution or workaround to be able to read XML
files in Safari wihout rewriting all of my scripts??

Post a minimal example or a link. Safari supports the selectNode
method as the following example shows:

<script type="text/javascript">

function foo(id) {
var range = document.createRange();
range.selectNode(document.getElementById(id));
range.deleteContents();
}

</script>
<button onclick="foo('div0');">Remove div0</button>
<div id="div0">Here is div0</div>
 
J

J_Zanetti

RobG ha scritto:
J_Zanetti wrote:
Post a minimal example or a link. Safari supports the selectNode
method as the following example shows:

<script type="text/javascript">

function foo(id) {
var range = document.createRange();
range.selectNode(document.getElementById(id));
range.deleteContents();
}

</script>
<button onclick="foo('div0');">Remove div0</button>
<div id="div0">Here is div0</div>

Thank you Rob!!
Here you seem to be using the methond selectNode of the objectRange
object.
In my scripts, I have only Document object; would it be enough to
define the Range for every document i have and then use its
selectNode()?
thanks :)
 
J

J_Zanetti

RobG ha scritto:
J_Zanetti wrote:
Post a minimal example or a link. Safari supports the selectNode
method as the following example shows:

<script type="text/javascript">

function foo(id) {
var range = document.createRange();
range.selectNode(document.getElementById(id));
range.deleteContents();
}

</script>
<button onclick="foo('div0');">Remove div0</button>
<div id="div0">Here is div0</div>

Thank you Rob!!
Here you seem to be using the methond selectNode of the objectRange
object.
In my scripts, I have only Document object; would it be enough to
define the Range for every document i have and then use its
selectNode()?
thanks :)
 
V

VK

Reading XML in Safari through selectNodes

Safari has to become a browser yet before talking about any extensive
support. So far it is a rather rare case when buggy beta sketches being
distributed as final release two times in the row (Safari 1.x and
Safari 2.x) As I said in an older post I would understand it from any
Acme, Inc. but not from Apple.
Nightly builds of 3.0 are looking promising though: it seems that the
critics produced some effect and Apple is finally targeted to produce a
decent software product. I'm not saying "great" but at least "usable".

For the time being if Safari support is an absolute must then you can
try a library like ajaxslt:
<http://code.google.com/p/ajaxslt/>
former <http://sourceforge.net/projects/goog-ajaxslt/>

This is "the guy who made it" :) - his project went from sourceforge
to Google, so copyright and license changed a bit in comparison with
the sourceforge stage. Still it remains free to use.

ajaxslt works with Safari 1.2 or higher because it is a "screw them
all" library. When Sarissa uses native XSLT tools of each browser,
whatever it can find, and then covers all differences and bugs under
one interface - ajaxslt-like libraries simply disregard any native XSLT
tools and implement them from the scratch by javascript means. This is
its power and weakness at the same time.
The power is because it is much easier to maintain and to extent. It
also make it really universal - with some modifications one could have
"XSLT" on Netscape 4.x
The weakness is because of lower level processing code they are using
top level javascript statements. This way you may get a noticeable
productivity impact depending on the kind and complexity of your
transformations.
Alas currently - until Apple starts to produce a usable browser - this
is the only option to support XSLT on Safari.
 
R

Randy Webb

VK said the following on 1/10/2007 4:47 AM:
Safari has to become a browser yet before talking about any extensive
support.

And you wonder why people say the things about you that get said.
 
R

RobG

VK wrote:
[...]
For the time being if Safari support is an absolute must then you can
try a library like ajaxslt: [...]
this is the only option to support XSLT on Safari.


21. Does Safari support XSLT?

Safari 1.3 and above supports XSL transformations of XML pages at load
time. XSLT support is limited to XSL page processing instructions
embedded at the top of an XML page:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl"
href="your_transform_file_here.xsl" ?>

XSLT not available via JavaScript for application to arbitrary XML in
an HTML page."

<URL: http://developer.apple.com/internet/safari/faq.html#anchor21 >

It seemed to me that the OP was looking to use XML data to update the
content of DOM elements. For that, DOM 2 Core methods will do the job.
 
R

RobG

J_Zanetti said:
RobG ha scritto:


Thank you Rob!!
Here you seem to be using the methond selectNode of the objectRange
object.
In my scripts, I have only Document object; would it be enough to
define the Range for every document i have and then use its
selectNode()?

I don't know, I don't know what you are trying to do - ranges are
useful for manipulating document fragments in ways that aren't
available using other DOM methods. I only suggested range.selectNode
because I thought you'd made a typo with "SelectNode".

As I understand it, you want to extract data from an XML document and
update the values of DOM elements. For that you can use common DOM
Core methods such as getElementsByTagName, getAttribute, nextSibling,
etc. that probably have wider support than other methods.
 
J

J_Zanetti

RobG ha scritto:
21. Does Safari support XSLT?

It seemed to me that the OP was looking to use XML data to update the
content of DOM elements. For that, DOM 2 Core methods will do the job.

Thank you all guys!
At the end i went to choose for rewriting my functions by using the DOM
object functions.
With getElementsByTagName and the data property, ive been able to get
data for the XML as i needed to do. So finally, my application works
fine in IE, Firefox and Safari too:)

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top