469,646 Members | 1,407 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Reading XML in Safari through selectNode

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 :)

Jan 9 '07 #1
11 5855
J_Zanetti wrote:
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')
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 9 '07 #2

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[i] = 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?

Jan 9 '07 #3

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[i] = 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?

Jan 9 '07 #4
J_Zanetti wrote:
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>
--
Rob

Jan 9 '07 #5

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 :)

Jan 9 '07 #6

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 :)

Jan 9 '07 #7
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.

Jan 10 '07 #8
VK said the following on 1/10/2007 4:47 AM:
>Reading XML in Safari through selectNodes

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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 10 '07 #9
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/....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.

--
Rob

Jan 10 '07 #10
J_Zanetti wrote:
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()?
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.
--
Rob

Jan 10 '07 #11
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

Jan 11 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bernard | last post: by
2 posts views Thread by jdlwright | last post: by
4 posts views Thread by Paul W | last post: by
5 posts views Thread by Bill Cohagan | last post: by
34 posts views Thread by Simon Wigzell | last post: by
3 posts views Thread by Jeff Paffett | last post: by
21 posts views Thread by Edward | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.