:rule context=" " subject=" "

Questions about XML that are not covered by the other forums should go here.
dinodc
Posts: 19

:rule context=" " subject=" "

Fri Apr 18, 2014 1:16 am

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: 77

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

Sat Apr 19, 2014 2:32 am

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

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

Sun Apr 20, 2014 8:47 pm

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: 77

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

Mon Apr 21, 2014 3:29 am

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

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

Mon Apr 21, 2014 5:43 am

Thanks....I will try later. But many thanks and I will keep you updated!
Jamil
Posts: 77

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

Mon Apr 21, 2014 1:32 pm

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>

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests