What to create XPath(s) that I can can use in a FOR loop to retrieve each of the <app> and <coapp> fname & mname. Want my result to be "1 - Danny L", "2 - Wendy R", "3 - Brenda", "4 - Tim G". I have played with this XPath and just cannot get it right.

<?xml version="1.0"?>
<root>
 <data>
   <app>
     <curr_fname>Danny</curr_fname>
     <curr_mname>L</curr_mname>
     <prev_fname>Wendy</prev_fname>
     <prev_mname>R</prev_mname>
   </app>
   <coapp>
     <curr_fname>Brenda</curr_fname>
   </coapp>
   <coapp>
     <curr_fname>Tim</curr_fname>
     <curr_mname>G</curr_mname>
   </coapp>
 </data>
</root>

Recommended Answers

All 6 Replies

XPath 1.0 or 2.0? I'm working on something that might meet your needs, but IU don't understand what kind of FOR loop you mean. Are you implementing something in Java/C# that is using this XPath to get a certain node set? Or is this a pure XSLT problem to generate this output?

Le me know and I'll look at it in the morning.

XPath 1.0 or 2.0? I'm working on something that might meet your needs, but IU don't understand what kind of FOR loop you mean. Are you implementing something in Java/C# that is using this XPath to get a certain node set? Or is this a pure XSLT problem to generate this output?

Le me know and I'll look at it in the morning.

I am writing VB.Net code. It is complicated but I have no flexbility. It will be VB.Net code that uses XPath only to retrieve data from the XML document.

A bit more information - the FOR loop will be written in vb.net. Something like the following. The retrieval of data from the xml doc must be in an XPath. There is no flexibility.

Dim node As XmlNode

For i = 1 to 10

   xpath = "(\root\data\app |\root\data\coapp)[" & i  & "] (curr_fname | prev_fname)"
   node1 = xml.SelectSingleNode(xpath)
   xpath = "(\root\data\app |\root\data\coapp)[" & i  & "] (curr_mname | prev_mname)"
   node2 = xml.SelectSingleNode(xpath)
     Field = Field + ", " & node.innertext & " " & node.innertext   
Next i

This problem is that XPath isn't quite enough to sort and group the way you need it too. When you have multiple names (both curr and prev) inside the same "app", you can't separate which you're looking at just in XPath 1.0.

You're going to have to break your loop into 2 basically. You're going to have to say, For i, look at the "currnames" inside a particular "app", then for that look at the "prevnames", then increment and go to the next one. Once you do that you can do something like this easily. (Just change the [1] for your position to next "app")

/root/data/*[local-name()[ends-with(.,'app')]][1]/concat(curr_fname,' ',curr_mname)
/root/data/*[local-name()[ends-with(.,'app')]][1]/concat(prev_fname,' ',prev_mname)

Another solution that would work is if the input XML tree can be changed to put each name pair in it's own app element. THEN you could easily get exactly what you want, because each name pair is grouped together to begin with. For example in your input document looked like this:

<root>
	<data>
		<app>
			<curr_fname>Danny</curr_fname>
			<curr_mname>L</curr_mname>
			<subapp>
				<prev_fname>Wendy</prev_fname>
				<prev_mname>R</prev_mname>
			</subapp>
		</app>
		<coapp>
			<curr_fname>Brenda</curr_fname>
		</coapp>
		<coapp>
			<curr_fname>Tim</curr_fname>
			<curr_mname>G</curr_mname>
		</coapp>
	</data>
</root>

Then you could use this XPath to get what you want. (Once again the [2] is the position you would increment)

/descendant::*[local-name()[ends-with(.,'app')]][2]/concat(*[local-name()[ends-with(.,'fname')]],' ',*[local-name()[ends-with(.,'mname')]])

Sorry I couldn't be more helpful, but maybe I provided some kind of insight.

"FOR loop will be written in vb.net. "
"1 - Danny L", "2 - Wendy R", "3 - Brenda", "4 - Tim G".

I found it much easier to make xml file like a excel sheet. just columb & rows.
the more child nodes you create the more complicated xslt and parsers become to handle them.
fileA.xml
<root>
<data>
<id></id><aa>0001</aa><bb>loanapp</bb><cc>Danny</cc><dd>L</dd>
<id></id><aa>0001</aa><bb>subapp</bb><cc>Wendy</cc><dd>R</dd>
<id></id><aa>0001</aa><bb>coapp1</bb><cc>Brenda</cc><dd></dd>
<id></id><aa>0001</aa><bb>coapp2</bb><cc>Tim</cc><dd>G</dd>
</data>
</root>
fileB.xml
<root>
<data>
<id>0001</id><aa></aa><bb>Regional Mng</bb><cc>Dan</cc><dd>D</dd>
<id>0002</id><aa></aa><bb>Secrtary</bb><cc>Nina</cc><dd>T</dd>
</data>
</root>

You use asp to xquery for ID=0001
then xpath to MATCH id=0001
then trans xslt it to html via xslt if bb="loanapp" then display =cc
if bb="subapp" then display =cc
if bb="coapp1" then display =cc
if bb="coapp2" then display =cc


or just xslt match id or aa for 0001 then list names(cc) and thier rolls as (bb)

Your could get your output to say display the

loan agent = Danny L
sub applicant Wendy R
co app#1 Brenda
co app#2 Tim G


Or if you are looking at say file B, and make your xslt display links via xquery can link "0001" then click and open filter match for fileA.xml as a display.
--------------------
I don't explain well but can show you what I mean with pure xml db and asp pages.
Just stuff I have been involed developing when I get free time.
http://www.stranskyfamilytree.net/gen%20project/admin-login.asp
also I am custom doing a xml db for http://www.wartimepress.com/
that site has 10-20k records, images and articles. I am just getting around to a pure xml user portal to validate users and admin/operator dashboards.

Yeah that works perfectly as well. Once again you've got all the data you need in a single element, already grouped.

That it worked out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.