Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identical elements with different child nodes possible?

Status
Not open for further replies.

nondrinker

Programmer
Jan 23, 2002
53
US

Hello, being new to the world of XML, i am not
sure if this is a correct XML document or not, or
is it possible to generate a document like this.

I have to generate two identicle elements, in this
case the "<Document>" element with 2 different
sets of child nodes. Like in the example
below, the first set of <Document> tag
does not have a <zip> child node.


<Response>
<Document>
<Name>Test1</Name>
<Address>Test2</Address>
<City>Test3</City>
</Document>
<Document>
<Name>Test1</Name>
<Address>Test2</Address>
<City>Test3</City>
<Zip>Test4</Zip>
</Document>
</Response>

I was wondering, if this is possible, or do i have to
do something like this, by making a new <Document2> element:
<Response>
<Document>
<Name>Test1</Name>
<Address>Test2</Address>
<City>Test3</City>
</Document>
<Document2>
<Name>Test1</Name>
<Address>Test2</Address>
<City>Test3</City>
<Zip>Test4</Zip>
</Document2>

Thank you.
 
There's no problem with the first example. The second would cause problems. The basic idea in XML is to create a template of sorts and stick different data in different occurrences of the template.

Under some circumstances, you might want to insert an empty Zip tag in your first example, which would make the template identical for both data occurrences.
 
Well if i don't make a <Document2> (which i don't want to) then when it comes to inserting the new tag in the <Document> tab, thats where i am having problem
in specifying that i need the <zip> tag in the 2nd <Document>, not the first <Document>


This is the code i am using:


Set nRoot = xml.selectSingleNode("Response/Document")
Set oNElement = xml.createElement("Zip")

The problem is that when i look for the pattern of Response/Document it inerts my <zip> in the
first <Document> instead of the 2nd one. But if i do :

Set nRoot = xml.selectSingleNode("Response/Document2")
then it does insert it in the <Document2> element.

And i cannot have an empty </zip> in my first <Document> because i have to export the data and i have
been told that i should either have the <zip> tag or do not.

Is there a way to insert the <zip> in the 2nd <Document> only.

Thank you.
 
Code:
Set nRoot = xml.selectSingleNode("Response/Document")
Your XPath here is only going to return you the first node. If you want the second one, you need to tell it:
Code:
Set nRoot = xml.selectSingleNode("Response/Document[2]")
(the index is one-based)

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thank you very much. It worked. Looks like the index is 0 based, because when i tried [2] it gave me the error but [1] did the work.

Again, thank you very much and thank you to harebrain also.
 
I was on XMLSpy -- your parser may be different.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
heh... I would use this code, if working with DOM ;-)
Code:
Set Temp = xml.selectSingleNode("Response/Document[1]")
Temp.appendChild(xml.createElement("zip")).Text = "TestX"

Or...
Code:
xml.selectSingleNode("Response/Document[1]").appendChild(xml.createElement("zip")).Text = "TestX"

Or...

Code:
xml.GetTagsByName("Document")(1).appendChild(xml.createElement("zip")).Text = "TestX"

But thats just me... ;-)

I like Short Compact Code...


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top