Roadmap
Part 5: Revisiting @rulesContext and more XPath features
Introduction
In this Part, we are going to revisit the use of the rulesContext attribute, why it should be avoided, and rewrite some real-world examples that use it. We also touch upon additional capabilities of XPath and why you should be mindful of the use of “//” in your expressions.
A Reminder Why rulesContext Attribute is Bad
Part 2 of Building a Better BREX discusses the reasons why the use of the rulesContext attribute should be avoided:
A data module's schema URI must match exactly the schema URI specified in the
rulesContextattribute for the rules to be applied. If you have been a good IETP author and use the standard schema URIs, this is not a problem. However, there are environments where schema URIs refer to local pathnames (e.g. "file://C:/data/...") instead of utilizing XML Catalogs.Only a single URI can be specified in the
rulesContextattribute. This can be very limiting if you have rules that may be applicable to more than one schema type, or applicable to different issues of a given schema type (e.g. Issue 4.0, 4.1, 5.0, etc). When usingrulesContext, you will have to replicate the the rules for each context, which leads to sustainment headaches when rule updates are required.
Rewriting Rules to Not Use rulesContext
Disallowing Specific Module Types
It is common for projects to decide that certain module types are not relevant and should be disallowed. For example, if the project decides that learning modules should not be used, they may write something like the following:
<contextRules
rulesContext="http://www.s1000d.org/S1000D_4-2/xml_schema_flat/learning.xsd">
<structureObjectRuleGroup>
<structureObjectRule>
<objectPath allowedObjectFlag="0">/dmodule</objectPath>
<objectUse>The learning module shall not be used.</objectUse>
</structureObjectRule>
</structureObjectRuleGroup>
</contextRules>In this example, the rules are specific Issue 4.2 data. To avoid the known problems of using rulesContext, the rule can be changed to the following:
<contextRules>
<structureObjectRuleGroup>
<structureObjectRule>
<objectPath allowedObjectFlag="0">
/dmodule/content/learning
</objectPath>
<objectUse>The learning module shall not be used.</objectUse>
</structureObjectRule>
</structureObjectRuleGroup>
</contextRules>When it comes to data module schema types, the schemas are consistent in their structure, where you have a meta-data section, identified by /dmodule/identAndStatusSection, and the main content section, /dmodule/content. And for the main content section:
Each schema type defines children of /dmodule/content that allows us to identify module type without relying on the schema URI.
The following is a list of data module types and their identifying XPath expression:
ACT: /dmodule/content/applicCrossRefTable
brDoc: /dmodule/content/brDoc
BREX: /dmodule/content/brex
Checklist: /dmodule/content/checkList
Common Repository: /dmodule/content/commonRepository
CCT: /dmodule/content/condCrossRefTable
Container: /dmodule/content/container
Crew: /dmodule/content/crew
Description: /dmodule/content/description
Fault (Reporting): /dmodule/content/faultReporting
Fault (Isolation): /dmodule/content/faultIsolation
Front Matter: /dmodule/content/frontMatter
IPD: /dmodule/content/illustratedPartsCatalog
Learning: /dmodule/content/learning
PCT: /dmodule/content/productCrossRefTable
Procedure: /dmodule/content/procedure
Process: /dmodule/content/process
Service Bulletin: /dmodule/content/sb
Schedule: /dmodule/content/maintPlanning
SCO Content: /dmodule/content/scoContent
Wiring Data: /dmodule/content/wiringData
Wiring Fields: /dmodule/content/wiringFieldsA "Must" Rule
The following rule not only uses rulesContext to restrict its scope, but also uses the allowedObjectFlag="1" construct:
<contextRules
rulesContext="http://www.s1000d.org/S1000D_4-2/xml_schema_flat/container.xsd">
<structureObjectRuleGroup>
<structureObjectRule>
<objectPath allowedObjectFlag="1">
(/dmodule/identAndStatusSection/dmAddress/dmIdent/
dmCode[@disassyCodeVariant="Z"]) |
(/dmodule/identAndStatusSection/dmAddress/dmIdent/
dmCode[@disassyCodeVariant="ZZ"]) |
(/dmodule/identAndStatusSection/dmAddress/dmIdent/
dmCode[@disassyCodeVariant="ZZZ"])
</objectPath>
<objectUse>The attribute "disassemblyCodeVariant" must be
a Z, ZZ or ZZZ character string for the container
module.</objectUse>
</structureObjectRule>
</structureObjectRuleGroup>
</contextRules>Applying schema awareness, matching what is not allowed, and some XPath expression improvements, we get the following:
<contextRules>
<structureObjectRuleGroup>
<structureObjectRule>
<objectPath allowedObjectFlag="0">
/dmodule[content/container]/
identAndStatusSection/dmAddress/dmIdent/
dmCode[not(@disassyCodeVariant = ("Z", "ZZ", "ZZZ"))]
</objectPath>
<objectUse>The attribute "disassemblyCodeVariant" must be
a Z, ZZ or ZZZ character string for the container
module.</objectUse>
</structureObjectRule>
</structureObjectRuleGroup>
</contextRules>Explanation of rewritten version:
/dmodule[content/container]part of the expression restricts the scope to container modules, avoiding false failures for other module types. In more technical terms, thedmoduleis not matched unless the conditions of the predicate (the expression in the[]s) is true. Since only container modules allow thecontainerelement as a child ofcontent, we know the expression will never match non-container modules.@disassyCodeVariant = ("Z", "ZZ", "ZZZ")takes advantage of how the=operator actually functions:=, along with!=,<,>, <=, >=are general comparison operators, where either, or both, sides of the operation can be a sequence (i.e. a list of values). As long as there is a pair of items—comprised of one item from each side of the operation—that satisfies the comparison, the result istrue.We wrap the comparison in
not()to negate the comparison since we want to match cases wheredisassyCodeVariantis not in the allowed sequence of values.
To verify our logic, let's do a thought exercises, where we use an actual value in the expression to see if we get the expected result. For example, let's say disassyCodeVariant is "Z" (an allowed value), we will have the following:
not("Z" = ("Z","ZZ","ZZZ"))Since there is at least one pairing, ("Z" = "Z"), that satisfies the comparison, we get:
not(true)And finally,
falseWhich is what we want as we are trying to match what is not allowed (we declared allowsObjectValue="0"): Something that is allowed should return false.
Now, let's say disassyCodeVariant equals "A" (not an allowed value), we then have the following:
not("A" = ("Z","ZZ","ZZZ"))We cannot find a single pairing that satisfies the comparison since "A" does not match any of the values in the sequence. Therefore, we get the following:
not(false)And finally,
trueThe behavior of the general comparison operators is often overlooked by those familiar with other programming languages, where = (et.al.) only support a single item on each side of the operator. In XPath, to get this behavior, you use the value comparison operators eq, ne, lt, le, gt, and ge.
“!=” is Not the Inverse of “=”
Since you may be thinking why use not() instead of !=, some further explanation is required.
In XPath, the != operator is NOT the inverse of =.
In our example, the the behavior of != will cause valid values of disassyCodeVariant to fail. For example, let's say we used the following expression instead:
@disassyCodeVariant != ("Z","ZZ","ZZZ")And revisit our thought exercise where we assign the value "Z" to disassyCodeVariant:
"Z" != ("Z","ZZ","ZZZ")Recall the explanation of how general comparison operators behave:
As long as there is a pair of items--comprised of one item from each side of the operation--that satisfies the comparison, the result is
true.
The comparison to satisfy is "not equals". As long as there is a pairing that satisfies this, the expression will return true. In this thought exercise, we have two pairings that satisfy the comparison: ("Z != "ZZ") and ("Z" != "ZZZ"). Therefore, a container module that has a valid value for disassyCodeVariant will fail validation.
In practice, avoid using != when sequences are involved.
If you believe you have a need to use it, make sure to test for expected successes and failures to ensure your rule provides the results you expect.
Converting a Boolean Rule to Closest Node of Interest
<structureObjectRule>
<objectPath allowedObjectFlag="0">
not(//firstVerification and //secondVerification)
</objectPath>
<objectUse>
Published modules must have gone through 1st and 2nd verification.
</objectUse>
</structureObjectRule>Although the specification is ambiguous in allowing XPath expression to return a Boolean value (true or false), the specification includes examples that do, and most, if not all, BREX checkers support it. Regardless, the problem with Boolean return values is when the rule fails: the BREX checker cannot provide the relevant location in the document for the author to make corrective action. In this example, knowing where to go may not be a challenge, but for more complex expressions, the author can struggle on how to correct the failure.
To help the checker provide document location information, we will rewrite the rule so the expression returns a node-set instead, while still enforcing the BR:
<structureObjectRule>
<objectPath allowedObjectFlag="0">
/dmodule/identAndStatusSection/dmStatus/qualityAssurance
[not(firstVerification and secondVerification)]
</objectPath>
<objectUse>
Published modules must have gone through 1st and 2nd verification.
</objectUse>
</structureObjectRule>In our rewrite, we return the element containing the markup indicating the 1st and 2nd verification status of the module, where the Boolean logic is specified in the predicate expression of qualityAssurance. If this rule fails, the BREX checker can now provide useful location information, for example:
Status: FAILED: HIGH
URI: DMC-IRISCHECKDEMO-AAA-D00-00-00-00AA-022A-D_010-00_EN-US.XML
Message: Node is not allowed
Line: 94:25
XPath: /dmodule[1]/identAndStatusSection[1]/dmStatus[1]/qualityAssurance[1]
BREX: DMC-IRISCHECKDEMO-AAA-D00-00-00-00AA-022A-D_010-00_EN-US
Usage:
Published modules must have gone through 1st and 2nd verification.Overuse of “//”
There are valid, and sometimes necessary, uses of “//”, but it is frequently overused, which can lead to inefficiencies when expressions are evaluated by the BREX checker. For example, let's revisit an original expression we rewrote previously:
not(//firstVerification and //secondVerification)“//” is shorthand for any descendant node. For example, the expression //firstVerification will try to match all firstVerification elements, regardless of where it occurs in the document. This implies that XPath engine will have to scan the entire document to to see if there are any firstVerification elements.
A complete scan of the module is not needed here since since we know (based on the S1000D schemas), that firstVerification can only occur as a child of qualityAssurance, which itself, can only occur in a specific location of a module. Add to that, the existence of //secondVerification in the original expression, and we have two passes over the entire module. With the following expression rewrite:
/dmodule/identAndStatusSection/dmStatus/qualityAssurance
[not(firstVerification and secondVerification)]We limit the scope of search to where firstVerification and secondVerification can occur.
Initial observations may indicate that the use of “//” has no real performance impact, but if you are running validation on thousands of modules (which is a common scenario for large systems) and/or large data modules, that extra processing time can add up. In additions, if the use of “//” occurs in many of your BREX rules, multiple passes over each module will occur for each rule evaluated.
In Summary
In this article, we did the following:
Reminded ourselves why the
rulesContextattribute should be avoided and rewrote a couple of real-world examples.Explored the power of XPath’s general comparison operation “=”, and why “!=” should be avoided when working with sequences.
Revisited the closest-node-of-interest principle by rewriting a Boolean expression to return a node set.
And, be mindful when using “
//” by trying to write expression that limits search scope of a module.
