English 中文(简体)
with
原标题: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="microsoft">
    <os name="windows xp" />
    <os name="windows 95" />
    <os name="windows 98" />
    <os name="windows vista" />
</owners>

Java

它应当利用链接和链接; 名称,并与所有人和所有者匹配;os => 名称。

os的名字只是一纸空话,以后不应重复。

预 收

最佳回答

假定以上是储存在可变轴上:

if (window.DOMParser) {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
} else {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(txt); 
}

然后,查阅你在javascript中提供的XML:

// 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

说明所有这些内容的网络上有许多代码(另见:getNamedItem,getElementsByTagName,nodeValue......和抽签)。

反之:

for(i=0;i<xmlDoc.childeNodes[1].childNodes.length;i++) {
  //Access each node in the set:
  xmlDoc.childNodes[1].childNodes[i]
}
问题回答

这一风格:

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

提供适当的投入:

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

产出我认为是你想要的:

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

与xml合作更为方便的方式是使用j Query

仅检索数据:

$.ajax({ url:  /Document.xml , success: ProcessData, contentType:  text/xml  });

并尽力:

function ProcessData(data) {
    var xml = $(data);
    xml.find("links osname[name]").each(function () { 
        var value = $(this).attr("links")); 
        // etc.
    });
}

如果你们知道 j,那就应该容易。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签