:rule context=" " subject=" "

Questions about XML that are not covered by the other forums should go here.
dinodc
Posts: 19
Joined: Fri Apr 18, 2014 1:07 am
Contact:

:rule context=" " subject=" "

Post by dinodc »

Are there more example to use the "subject within rule context? (<sch:rule context="blah/blah/blah" subject=""> )

See, I was task to be more specific..have a way to check if a value is there, if so, then run "assert" within the <pattern>.

We know <rule context=" "> is where the context attribute is. But we want to have like a IF-Statement or if value is there then go ahead and run the <pattern>, If not, don't proceed on.
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

Re: :rule context=" " subject=" "

Post by Jamil »

I don't have an example where subject is used; however, can't you do what you are looking for with XPath criteria within the context attribute?

Something like this:

Code: Select all


    <pattern>
<rule context="/node/subNode[@atttibute='TARGET'][count(@attributeThatMustExist)>0]">
<assert test=".!='whatever'">Error condition</assert>
</rule>
</pattern>
dinodc
Posts: 19
Joined: Fri Apr 18, 2014 1:07 am
Contact:

Re: :rule context=" " subject=" "

Post by dinodc »

Thanks Jamil.

I like how you have the "[count(@attributeThatMustExist)>0]". But my problem is this. I want to check that the attribute is located in another node. If it does, then in a different node I want to run the assert to test that node/subnode's Properties exist. The reason is that to run the assert test, those properties are only for those that particular 1st node.

Here is a more generic term without going into my work's elements.

Node
Car->Toyota->Prius
Car->Ford->Mustang
Suv->Ford->Explorer

Node
Gas--attributes: 1)Mileage on gas, 2)Total Mileage distance
Hybrid--attribute: 1) Mile on electric, 2)Mileage on city, 3)mileage on highway.

See, due to Prius is a Hybrid I just want to test the hybrid part and the Prius should not run through the gas element. In the Node with gas...I need check in "sch:rule context" to look at the Node Car. I guess its checking for its reference.

Is that a little help?
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

Re: :rule context=" " subject=" "

Post by Jamil »

Okay. Try this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
<pattern>
<rule context="//*[count(@MileageOnElectric)>0]">
<assert test="./@MileageOnCity > 30 or ./@MileageOnHighway > 50">Poor mileage for a hybrid</assert>
</rule>
<rule context="//*[count(@MileageOnGas)>0]">
<assert test="./@TotalMileageDistance > 60">Poor mileage for a gasolene vehicle</assert>
</rule>
</pattern>
</schema>
This worked against a test document I created below:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="schematron_question2.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<auto>
<Car>
<Toyota>
<Prius MileageOnElectric="30" MileageOnCity="35" MileageOnHighway="10"/>
</Toyota>
<Ford>
<Mustang MileageOnGas="15" TotalMileageDistance="80"/>
</Ford>
</Car>
<Suv>
<Ford>
<Explorer MileageOnGas="10" TotalMileageDistance="61"/>
</Ford>
</Suv>
</auto>
You'll want to run some performance tests with this against large XML documents due to using //*
dinodc
Posts: 19
Joined: Fri Apr 18, 2014 1:07 am
Contact:

Re: :rule context=" " subject=" "

Post by dinodc »

Thanks....I will try later. But many thanks and I will keep you updated!
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

Re: :rule context=" " subject=" "

Post by Jamil »

No problem. Taking a second look at this, the or in my first assert test should really be an and. This revised schema resolves the issue:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
<pattern>
<rule context="//*[count(@MileageOnElectric)>0]">
<assert test="./@MileageOnCity > 30 and ./@MileageOnHighway > 50">Poor mileage for a hybrid</assert>
</rule>
<rule context="//*[count(@MileageOnGas)>0]">
<assert test="./@TotalMileageDistance > 60">Poor mileage for a gasolene vehicle</assert>
</rule>
</pattern>
</schema>
Post Reply