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

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

1
  • Please, provide the desired XML output from the XSLT transformation. Leaving people to guess isn't likely to provide the solution you need. Commented Sep 2, 2010 at 2:50

3 Answers 3

1

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]
}
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.