XSD Verify Element has at least one of the attributes specified - validation

Using XSD schema validation 1.0 I want to verify an element has at least one attribute specified.
For example, a simple element like this:
<foo a="1" b="2" c="3" />
I want to verify that at least attribute b or c is specified. But note that both can also be specified--they're not mutually exclusive.
I tried using a key along the lines of:
<xs:key name="AttributeSpecified">
<xs:selector xpath="." />
<xs:field xpath="#b|#c" />
</xs:key>
but it fails when both attributes are specified (because multiple results are returned).
Can it be done?

This is not possible in XSD 1.0. It might be possible in XSD 1.1.
I am a fan of XML Schema, but I would not choose it for this type of validation. You might be able to make it work using XSD1.1 but if your requirements became just a little more complex you could end up with some horrible-looking constraints.
On the other hand, an XPath expression can elegantly express any constraint you can think of, and you would not need to bend the language to make it work.

Related

FHIR Profile/StructDef: How are children (sub-elements) of a named slice tied to the slice?

I'm trying to figure out how to fully specify a sliced element. If I'm reading the spec right, nameReference is the only place where a "sub element" of a slice can declare which slice it's "on".
So, if telecom is sliced by use and system and I want to specify a constraint on home phone, I have to fix use and system to those values and then add my constraints on that slice.
Consider:
Resource Example ElementDefinition attributes
================================ =====================================================================
<Patient> name="Patient"
... snip ...
<telecom> name="HomePhone"
<system value="phone" /> name="HomePhone.system", nameReference="HomePhone", fixedCode="phone"
<use value="home" /> name="HomePhone.use" , nameReference="HomePhone", fixedCode="home"
<value value="5551234567" /> name="HomePhone.value" , nameReference="HomePhone"
</telecom>
... snip ...
</Patient>
In most examples, it appears that a dotted notation of Name has been used (as I've placed in the example). But the specification doesn't require this and provides no format that could be reliably parsed.
The problem is: nameReference and fixed[x] are mutually exclusive. What's the correct way to handle this??
Repetitions in an instance don't "declare" what slice they're part of. They simply declare the appropriate value for what ever element(s) are the discriminator for the slicing process. nameReference isn't involved at all. On the definition side, association is simply handled by name. So HomePhone.system is associated with HomePhone simply by the name and by sequential proximity. The dot-notation is required. We could probably be a bit more explicit about that though, so feel free to submit a change request.

How do you specify multiple AppleScript child elements of the same class?

I'm adding Apple Event scripting to my application. I would like to be able to use both of the statements below on it:
-- Every instance of MyObject in the app
every MyObject
-- Only the instances of MyObject the user has selected
selected MyObjects
These are relevant excerpts from of my sdef file:
<dictionary>
<suite ...>
<class name="application" code="capp" description="Top-level scripting object" plural="applications" inherits="application">
<cocoa class="MyAppClass" />
<element type="MyObject" access="r">
<cocoa key="myobjects" />
</element>
<element name="selected MyObjects" code="ABCD" type="MyObject" access="rw">
<cocoa key="selectedMyObjects" />
</element>
</class>
<class name="MyObject" code="ABcd" inherits="item" plural="MyObjects">
...
</class>
</suite>
</dictionary>
When I call every MyObject, it returns a list of objects, as expected. But when I call selected MyObjects, the Script Editor selects the "MyObjects" portion and complains:
Syntax Error
Expected end of line, etc. but found plural class name.
How can I achieve what I'm looking to do?
Your design is wrong. See the Scripting Interface Guidelines for a somewhat thin but better-than-nothing outline of good UX practices.
The correct idiom is to define a selection property, typically on application and/or document. This may be read-write or read-only, depending on what's appropriate to your application.
The selection property's value is either:
A single bespoke object specifier that identifies all of the currently selected objects e.g.:
selection of application "Foo"
or
selection of document X of application "Foo"
Some of the better Carbon-based apps and the occasional Cocoa app use this approach, which allows users to perform powerful queries such as:
get (name of every MyObject of selection of document X)
delete (every job whose status is completed)
but requires more work for you to implement.
A list of single-object specifiers, each identifying one selected item, e.g.:
{thing B of document X of application "Foo",
thing E of document X of application "Foo",...}
This is less powerful, since users cannot manipulate all of the selected items in a single command but must instead iterate the list processing each item one at a time, but is cheaper for you to implement. Cocoa Scripting being somewhat lame and inflexible even at the best of times, and pretty damn hopeless at moving/duplicating/deleting more than one object at a time, this is the most common approach in Cocoa-based apps.
Update your sdef file with this property:
<property name="selection" code="ABCD">
<cocoa key="selectedMyObjects" />
<type type="MyObject" list="yes" />
</property>
For an example of the first (better) design approach, take a look at Script Editor's own dictionary. For an example of the second, see Mail's dictionary.
(Tip: To view a dictionary in Script Editor, choose File>Open Dictionary and select the appropriate item from the list. To export that dictionary as an SDEF file, just make sure the dictionary viewer window is frontmost and choose File>Save As.)

Is the use of logical comparisons quicker than axes in XPath?

I have the an XML document that will balloon in size as time goes on and I would like to ensure that my XPath choice for an XSL select will be as efficient as possible.
The document contains the following types of elements:
<simple_instance>
<name>Class0</name>
<type>Business_Capability</type>
<own_slot_value>
<slot_reference>contained_business_capabilities</slot_reference>
<value value_type="simple_instance">Class1</value>
<value value_type="simple_instance">Class3</value>
<value value_type="simple_instance">Class4</value>
<value value_type="simple_instance">Class5</value>
</own_slot_value>
<own_slot_value>
<slot_reference>business_capability_level</slot_reference>
<value value_type="string">1</value>
</own_slot_value>
<own_slot_value>
<slot_reference>name</slot_reference>
<value value_type="string">Planning</value>
</own_slot_value>
</simple_instance>
Which of these two selectors (which find elements like the one above) will be more efficient in the long run?
/node()/simple_instance[type='Business_Capability']/own_slot_value/slot_reference[text()='business_capability_level']/following-sibling::value[text()='1']
or
/node()/simple_instance[type='Business_Capability' and (own_slot_value/slot_reference='business_capability_level') and (own_slot_value/value='1')]
My guess is that, if the implementation of XML short-circuits the and, the latter will be quicker.
Note: I'm using Protege's XML/XSL capabilities.
The two XPath expressions have different results, so asking which is faster seems irrelevant (the first selects a value element, the second a simple_instance element).
In addition, XPath is a specification not an implementation. Implementations differ widely in their strategies for evaluating complex paths. An answer that is true for one implementation may well not be true for another. Measure it and see (and tell us the answer).

bpws:getVariableData() causes fault if no xpath match is found

I wanted to use "bpws:getVariableData()" to assign a value only if the xpath expression find a match. If not, nothing should happen. Unfortunately the bpel processing stops with a fault, if the xpath expression finds no match. Is there a way to achieve this behavior?
Thanks for your help.
I found that the oracle BPEL engine provides a feature to ignore missing from data. This Flag can be added to the copy element as follows:
<copy bpelx:ignoreMissingFromData="yes|no"/>
More info on how to set it in the JDeveloper: http://download.oracle.com/docs/cd/E17904_01/integration.1111/e10224/bp_manipdoc.htm#SOASE87087
This solves the problem with the fault message that is thrown. However it still does not show the wanted behavior. My intension was that no assignment is done, if the xpath expression cannot be evaluated. Using the bpelx:ignoreMissingFromData flag however assigns the empty string "" to the target.
In my use case I want to merge tow XML documents. I want to assign a new value to an element in document1 only if the element shows up in document2. If not, leave the element in document1 unchanged.
I solved the problem using a transformation instead of a BPEL assign. In the xsl I use the following statement. The transformation gets two XML documents a input. Document1 is referenced via the parameter $parameter_referenceDocument1.
<elementName>
<xsl:if test="xpathInDocument2">
<xsl:value-of select="xpathInDocument2"/>
</xsl:if>
<xsl:if test="not(xpathInDocument2)">
<xsl:value-of select="$parameter_referenceDocument1.xpathInDocument1"/>
</xsl:if>
</elementName>
I know its ugly, but solves the problem. If anyone has a better solution, please let me know.
No, the BPEL standard requires the engine to throw a selectionFailure in this case. To avoid such situations, make sure you have properly initialized variables and/or validate variable against a schema. Also you may guard an assign activity with an if/switch activity to check for the presence of the element before accessing it. You may also consider writing an custom XPath function that returns a default value in case the demanded element does not exist in the variable. However, I'm not sure if the Oracle BPEL engine supports that.
You can create a scope around the assign activity and using an exception handler on the scope catch the selectionFailure, the item which will then carry on processing.
In the exception handler you could then assign a default value if required.
To clarify Vanto's statement, the Oracle BPEL engine does support custom XPath functions which would allow you to do that.

Define keyref selector based on element type in XPath

Let's say I have an XML file that will look like this:
<a>
<b d="value1"/>
<c d="value2"/>
</a>
In the XSD file that defines the structure of this XML file I defined the elements by name 'b' and 'c' to be of the same type (and the type requires attribute 'd').
Let's say that I want to make a keyReference of all elements of the type that both 'b' and 'c' are, is there any way in XPath to do this?
At the definition of the type of 'a' I would expect something like this:
<xs:keyref name="myReferenceName" refer="keyToReferTo">
<xs:selector xpath="[#type='typenameof elements b and c?']"/>
<xs:field xpath="#d"/>
</xs:keyref>
Is something like this possible, or is XPath, even in the XSD, schema-unaware?
XPath 1.0 is certainly not aware of any schemas and the W3C XML schema specification in version 1.0 even only uses a subset of XPath 1.0.
I think there is work going on to define a new version of the W3C XML schema language that uses XPath 2.0 but I have no idea about its details and whether it allows then to select elements in a selector based on schema types.
The XPath would be element(*, NameOfTypeGoesHere) I think, see http://www.w3.org/TR/xpath20/#id-element-test

Resources