XML question

If you have a XML like this

set XMLSource to {{

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 1234 Test1 1235 Test2 }}

How do you get the value in both TestCaseId(1234 & 1235) & the Name(Test1 & Test2)?

I read Chapter 16 and I’m still confused.

Thanks,
Jason

You can use “every node” to select the XML nodes you want, and then get the text of those nodes to retrieve the value. Try this and see if it gets you what you’re looking for:

set XMLSource to {{ 
<xml> 
<Report> 
<TestCaseList> 
<TestCaseData> 
<TestCase> 
<TestCaseId>1234</TestCaseId> 
<Name>Test1</Name> 
</TestCase> 
<TestCase> 
<TestCaseId>1235</TestCaseId> 
<Name>Test2</Name> 
</TestCase> 
</TestCaseData> 
</TestCaseList> 
</Report> 
</xml> 
}} 
set xml to documentTreeFromXML(XMLSource) // edited 12/13/12

put every node "//TestCaseId" of xml into TestCaseIdNodes
put every node "//Name" of xml into NameNodes
put the text of each item of TestCaseIdNodes
put the text of each item of NameNodes

That works! thanks

A follow-up question to jasonyoo’s questions…

You demonstrated how to access XML using the following:

put every node "//TestCaseId" of xml into TestCaseIdNodes 
put every node "//Name" of xml into NameNodes 
put the text of each item of TestCaseIdNodes 
put the text of each item of NameNodes

Using the same example, how would I get to a specific node?
If I wanted just the Name value from TestCaseId 1234, how would I write a ‘where’ type condition to get “Name1” from this node
1234

Thanks!

When working with “node” expressions I find it helpful to gradually refine the xpath expression I’m using for the search. Given the xml example we’re working with here, you’ll want to start one level higher, getting all of the TestCase nodes, then selecting the one with the particular TestCaseID that you want, then getting its Name. So the steps of progressively focusing in on the data you want might go something like this:

put every node "//TestCase" of xml
put every node "//TestCase[TestCaseId=1234]" of xml
put every node "//TestCase[TestCaseId=1234]/Name" of xml
put node "//TestCase[TestCaseId=1234]/Name" of xml
put the text of node "//TestCase[TestCaseId=1234]/Name" of xml

The real trick in this case is using square brackets to specify the condition to select just the particular TestCase that you want. See the “Predicates” section on this web page for some more examples: http://www.w3schools.com/xpath/xpath_syntax.asp

Thanks! That works great! Solution found.

When I run the code example above, I get the following error:
12/13/12 2:42:05 PM FAILURE Error 6 in XQueryErrorDomain XQueryError:6 - “invalid type”
Execution Time 0:00:00 scratch.script

set XMLSource to {{ 
<xml> 
<Report> 
<TestCaseList> 
<TestCaseData> 
<TestCase> 
<TestCaseId>1234</TestCaseId> 
<Name>Test1</Name> 
</TestCase> 
<TestCase> 
<TestCaseId>1235</TestCaseId> 
<Name>Test2</Name> 
</TestCase> 
</TestCaseData> 
</TestCaseList> 
</Report> 
</xml> 
}} 
set xml to XMLSource as tree 

put every node "//TestCaseId" of xml into TestCaseIdNodes 
put every node "//Name" of xml into NameNodes 
put the text of each item of TestCaseIdNodes 
put the text of each item of NameNodes 

This is because the sample provided isn’t actually valid XML and doesn’t properly define the root of the tree. This can be remedied by using the documentTreeFromXML() function:

set XMLSource to {{ 
<xml> 
<Report> 
<TestCaseList> 
<TestCaseData> 
<TestCase> 
<TestCaseId>1234</TestCaseId> 
<Name>Test1</Name> 
</TestCase> 
<TestCase> 
<TestCaseId>1235</TestCaseId> 
<Name>Test2</Name> 
</TestCase> 
</TestCaseData> 
</TestCaseList> 
</Report> 
</xml> 
}} 
set xml to documentTreeFromXML(XMLSource)

put every node "//TestCaseId" of xml into TestCaseIdNodes 
put every node "//Name" of xml into NameNodes 
put the text of each item of TestCaseIdNodes 
put the text of each item of NameNodes 

That works! Thanks for the clarification, Matt.

Please note that eggPlant v12.1 (which shipped today) includes some improvements to the handling of node expressions and a bug fix. When working with XML it is now recommended that you set the StandardNodeExpressions global property to true which will cause node expressions to be handled consistently across all platforms and into the future. See the v12.1 release notes for more details.

How to change the tag values if multiple tags present with same name??
Could someone please help me on this?