Click to See Complete Forum and Search --> : Obtaining Elements from the DOM


Bill Crawley
November 25th, 2003, 03:57 AM
Hi All,

Using The DOM. How do I obtain the following.

The XML DOC is made up thus:

<Body>
<Person>
<title>Mr</Title>
.....
.....
</Person>
</Body>

Where Person element will always have other elements below it, but not always the same.

I want to get all Person elements and then iterate through the child elemnts.

thanks

professorX
November 25th, 2003, 04:25 AM
Each element "Person" must have attribute "id" (or anything other - unique attribute) and "parentid" - id of Parent for this element.
Then You can use this xsl-code, for example:

<xsl:template match="/Body">
<table>
<xsl:apply-templates select="//Person[@parentid=0]"/>
</table>
</xsl:template>

<xsl:template match="Person">
<xsl:variable name="ThisID"><xsl:value-of select="@id"/></xsl:variable>

<tr>
<td><img src="images/i.gif" width="10" height="1"/></td>
<td>...instructions for this element... </td>
</tr>

<xsl:if test="count(Person[descendant-or-self::Person])>0">
<tr>
<td><img src="images/i.gif" width="10" height="1"/></td>

<td>
<table border="0" cellspacing="0" cellpadding="0">
<xsl:apply-templates select="Person[@parentid=$ThisID]"/>
</table>
</td>
</tr>
</xsl:if>
</xsl:template>

Bill Crawley
November 25th, 2003, 04:37 AM
I want to use the DOM and not an XSL Document.

There are NO attributes on the incoming XML document.

So far I have come up with this (not sure if it's right though):

For intNodeCount = 0 to objXMLDOM.documentElement.selectNodes("//Body/person").length - 1
set oElement = objXMLDOM.getElementsByTagName("*").item(intNodeCount)
select case oElement.nodeName
case "title"
Title = oElement.nodeValue
....
.....