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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML XPath Subquery 1

Status
Not open for further replies.

duggied

Programmer
Oct 7, 2005
4
CA
Hello folks. I'm fairly new to xml, so I'm not sure if what I am try to do is even possible.

Basicallly I have the following XML (see below) and I'm trying to filter all the fields where the attribute dependatforms = the forms id attribute.
Attempted XPath query: autoforms/fields/*[autoforms/forms/form/(@id=@dependantforms)]
Code:
<?xml version="1.0" encoding="utf-8" ?> 
<autoforms>
	<forms>
		<form text="Server Name Request Form" id="ServerName" showdependantfields="No" showduplicatefieldsacrossforms="No" columns="2" />
		<form text="Server Checklist Request Form" id="ServerChecklist" showdependantfields="No" showduplicatefieldsacrossforms="No" columns="2" />
	<fields>
		<textbox id="sname" text="Server Name: " required="Yes" requirederrormessage="* Required"  dependantforms="ServerName" />
		<textbox id="testfield" text="Test Field: " required="Yes" requirederrormessage="* Required"  dependantforms="ServerName" />
	</fields>
</autoforms>
 
It's not clear what nodeset you are looking for. Do you want to select all <textbox> that have @dependantforms = any of the form/@id ? If so, this will do it:
Code:
autoforms/fields/textbox[@dependantforms = ../../forms/@id]

Jon

"I don't regret this, but I both rue and lament it.
 
Thanks Jon,

Would it work to do a contains in the filter expression?

Code:
autoforms/fields/textbox[contains(@dependantforms,../../forms/@id)]

The reason being, although not clear in my posted xml, the dependant forms attribute could be
Code:
dependantforms="ServerName,ServerChecklist"
 
I tested contains with an XPath query analyzer and it works! Great!

Ok, last question how do I combine two filter expressions?

Code:
autoforms/fields/*[contains(@dependantforms,../../forms/@id)]and[contains(@memberforms,'ServerName')]
 
Two ways:
Code:
autoforms/fields/*[contains(@dependantforms,../../forms/@id) and contains(@memberforms,'ServerName')]
Or:
Code:
autoforms/fields/*[contains(@dependantforms,../../forms/@id)][contains(@memberforms,'ServerName')]

Jon

"I don't regret this, but I both rue and lament it.
 
Why isn't this query working? It's returning all three nodes, but should only be returning 1.

Code:
autoforms/fields/*[not(contains(../../forms/@id,@dependantforms)) and contains(@memberforms,'JoinDomain')]

It should return the fields nodes where the memberforms attribute contains JoinDomain, but it's dependantforms attribute does not match any ids in the forms node.

Code:
<?xml version="1.0" encoding="utf-8" ?> 
<autoforms>
	<forms>
		<form text="Server Name Request Form" id="ServerName" />
		<form text="Join Domain Request Form" id="JoinDomain" />
	</forms>
	<fields>
		<textbox id="sname" text="Server Name: " memberforms="JoinDomain" dependantforms="ServerName" />
		<dropdownlist id="OU" text="OU: " memberforms="JoinDomain" dependantforms="---" />
		<textbox id="password" text="Domain Password: " memberforms="JoinDomain" dependantforms="ServerName" />
	</fields>
</autoforms>
 
Your XPath is incorrect:
Code:
autoforms/fields/*[not(contains(../../forms/[COLOR=#ff0000]form/[/color]@id,@dependantforms)) and contains(@memberforms,'JoinDomain')]

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top