Xml Traversing with javascript
<links>
<osname name="windows xp" links="xyz" />
<osname name="windows 2k" links="xyz" />
</links>
<owners name="microsoft">
<os name="windows xp" />
<os name="windows 2k" />
<os name="windows 2003" />
<os name="windows 7" />
</owners>
<owners name="mi开发者_StackOverflow社区crosoft">
<os name="windows xp" />
<os name="windows 95" />
<os name="windows 98" />
<os name="windows vista" />
</owners>
Javascript
it should take the links from links->osname and match it with the owners->os =>name
and os name is suppose to be once only, it should not repeat later on.
Thanks in advance
Assuming the above is store in variable txt:
if (window.DOMParser) {
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
} else {
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
}
And then to access the XML you provided in javascript:
// links.osname[0].attribute(name)
xmlDoc.childNodes[0].childNodes[0].getAttribute('name');
// outputs: windows xp
// owners.os[2].attribute(name)
xmlDoc.childNodes[1].childNodes[2].getAttribute('name');
// outputs: windows 2003
There's quite a lot of code out on the nets explaining all this (see also: getNamedItem
, getElementsByTagName
, nodeValue
... and lots more)
To traverse:
for(i=0;i<xmlDoc.childeNodes[1].childNodes.length;i++) {
//Access each node in the set:
xmlDoc.childNodes[1].childNodes[i]
}
This stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:key name="kOwnersByName" match="owners" use="@name"/>
<xsl:key name="kOsByOwnerAndName" match="os"
use="concat(../@name,'+++',@name)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="owners"/>
<xsl:template match="owners[count(.|key('kOwnersByName',@name)[1])=1]">
<xsl:variable name="vOwner" select="@name"/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each select="../links/osname/@name">
<xsl:apply-templates
select="key('kOsByOwnerAndName',concat($vOwner,'+++',.))[1]"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With proper input:
<root>
<links>
<osname name="windows xp" links="xyz" />
<osname name="windows 2k" links="xyz" />
</links>
<owners name="microsoft">
<os name="windows xp" />
<os name="windows 2k" />
<os name="windows 2003" />
<os name="windows 7" />
</owners>
<owners name="microsoft">
<os name="windows xp" />
<os name="windows 95" />
<os name="windows 98" />
<os name="windows vista" />
</owners>
</root>
Output what I think is what you want:
<root>
<links>
<osname name="windows xp" links="xyz"></osname>
<osname name="windows 2k" links="xyz"></osname>
</links>
<owners name="microsoft">
<os name="windows xp"></os>
<os name="windows 2k"></os>
</owners>
</root>
More convenient way of working with xml, is to use jQuery.
Just retrieve the data:
$.ajax({ url: '/Document.xml', success: ProcessData, contentType: 'text/xml' });
and do whatever you want:
function ProcessData(data) {
var xml = $(data);
xml.find("links osname[name]").each(function () {
var value = $(this).attr("links"));
// etc.
});
}
If you know jQuery, it should be easy task for you.
精彩评论