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

Schema Constraints and XPath Namespaces

Status
Not open for further replies.

fatmosh

Programmer
Jun 6, 2004
10
0
0
US
I have to (seemingly) simple questions:

1) I'd like to enfore a child node to take the value of it's parent in a given situation using an XML Schema. For example:

Assume the following ridiculous logic is true:
If my parent likes pizza, I MUST like pizza. Otherwise, I may like pizza.

So:

Code:
<parent>
  <name>Bob</name>
  <LikesPizza>Yes</LikesPizza>
  <child>
    <name>Lily></name>
    <LikesPizza>No</LikesPizza>
  </child>
</parent>

Would NOT be valid. All of the other possibilities {Yes,Yes},{No, Yes},{No, No} would be valid.

Any ideas on how to specify this in a Schema? In my "programming" sense I would want to do:

If (parent.likespizza = 'Yes') then
likespizza = 'Yes'
Else
likespizza = whatever it is in the XML doc


2) I know this has probably been asked and fixed a thousand times, but I can't get XPath to work with a schema. I know it has something to do with namespaces, but I can't get it to work. Frankly, I don't give a crap about them b/c I only use one, and only use that one so that it can find my Schema.

I have read around and saw that you need to add a namespace that XPath can use. But it also said that if you do this you need to reference the correct namespace everytime you use it, which just isn't feasible (especially since i'm only using one!)

Any ideas on a simple way to make this work?

I have this at the top of my XSD:

Code:
<xs:schema id="otms" targetNamespace="[URL unfurl="true"]http://tempuri.org"[/URL] elementFormDefault="qualified" xmlns="[URL unfurl="true"]http://tempuri.org"[/URL]
	xmlns:mstns="[URL unfurl="true"]http://tempuri.org"[/URL] xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]

This at the top of my XML doc:

Code:
<OTM xmlns="[URL unfurl="true"]http://tempuri.org"[/URL] xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL]
xsi:schemaLocation="[URL unfurl="true"]http://tempuri.org[/URL] OTMS.xsd">

And I'm using .Compile and .Select in XPath.

Everything works fine if I use my XML file without the schema info. Once I add the schema info, XPath stops working.

Any help would be greatly appreciated :)
 
Well I got part 2) working :)

But if anyone has any idea on Part 1, please let me know! Would I have to do some sort of "choose" statment in XSLT? Is there anyway I can just do it in an XSD file? Thanks!
 
I think the only way to do this would be to use an <xs:choice> and put all the valid choices as complex elements. I don't believe there's a way to enforce a value based on another, although schema's aren't my strong point.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top