Reputation: 2063
I have to define first of all a sequence of anytype elements. My situation is the following:
<staticAction name="Jump" >
<"anyElementName" reqPoints="" gainedPoints="" />
<"anyElementName" reqPoints="" gainedPoints="" />
<"anyElementName" reqPoints="" gainedPoints="" />
...
</staticAction>
So, my question is: how can I define an unbounded sequence of elements with "dynamic" names but with fixed attributes (reqPoints and gainedPoints) ? Both attributes are xs:integer. I thought about adding the attributes through assertations but still, I don't know exactly how to do it. Thanks in advance.
Upvotes: 0
Views: 402
Reputation: 6016
Usually "dynamic" names can be replaced so they can be modelated using XSD. Example:
From
<places>
<country value="x"/>
<city value="y"/>
<town value="z"/>
</places>
To
<places>
<place type="country" value="x"/>
<place type="city" value="y"/>
<place type="town" value="z"/>
</places>
In this new case only attribute values are "dynamic". This is suitable for being modeled using XSD, which is easier and more expressive than XPath assertions.
However, if you really need to validate that "dynamic" names with known attributes and content type you can see this sample schema with assertions (explained inside the schema):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
vc:minVersion="1.1" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning">
<xs:element name="staticAction">
<xs:complexType>
<xs:complexContent>
<!-- Base is anyType -->
<xs:extension base="xs:anyType">
<xs:attribute name="name" type="xs:string"/>
<!-- Check that every "dynamic" child has the two integer attributes and no other attributes -->
<xs:assert test="every $element in ./* satisfies
($element[
matches(@reqPoints, '^[+-]?\d+$') and
matches(@gainedPoints, '^[+-]?\d+$') and
count(@*=2)])"/>
<!-- Test that there is no content in staticAction nor in the "dynamic" nodes -->
<xs:assert test="matches(string(.), '^\s*$')"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
Note that by extending xs:anyType and using that assertions you are getting an unbounded sequence of elements with any name and those two attributes.
Upvotes: 1