XSD for auto closing tag in complexType - ruby

I have this in my XML file:
<rooms>
<room id="1" beds="1" windows="0"/>
<room id="2" beds="2" windows="0"/>
</rooms>
And this in my XSD file:
<xs:complexType name="Rooms">
<xs:sequence>
<xs:element name="room" type="Room"/>
</xs:sequence>
<xs:attribute name="count" type="xs:integer"/>
</xs:complexType>
<xs:complexType name="Room">
<xs:attribute name="id" type="xs:integer"/>
<xs:attribute name="beds" type="xs:integer"/>
<xs:attribute name="windows" type="xs:integer"/>
</xs:complexType>
But I got this error : Element 'room': This element is not expected.
When I put <room id="1" beds="1" windows="0"></room> I don't have the error anymore and if I have only one room I don't have the error, so it's basically because of the auto closing tag.
How can I fix this?

Try specifying values for minOccurs and maxOccurs to your room element inside the sequence. I think the default expected number of occurrences is 1. Something like:
<xs:sequence>
<xs:element name="room" type="Room" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>

Related

Where is the schema of `child-resources` specified?

I'm trying to find where the schema of the result of a child-resources query is specified in the xsd. It's clearly not a primitiveContent (specified in responsePrimitive) because that doesn't contain the named child-resources arrays.
I think this is in the CDT-responsePrimitive-v.xsd file. In Resourcewrapper you have "m2m:sg_regularResource":
<xs:complexType name="resourceWrapper">
<xs:sequence>
<xs:choice minOccurs="1" maxOccurs="1">
...
<xs:element ref="m2m:sg_regularResource" />
...
</xs:choice>
<xs:element name="URI" type="xs:anyURI" />
</xs:sequence>
</xs:complexType>
In any of the resources that is a "m2m:sg_regularResource" you can find a section like this that allows for "included" child resources:
...
<!-- Child Resources -->
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="childResource" type="m2m:childResourceRef" minOccurs="1" maxOccurs="unbounded" />
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element ref="m2m:aResourceType" />
<xs:element ref="m2m:anotherResourceType" />
</xs:choice>
</xs:choice>
...

How to use XPath to find nodes in the XML attribute with ref?

I am trying to access the attribute/element nodes with ref in the below xsd
<xs:attributeGroup name="arcAttrs">
<xs:attribute ref="xlink:type" use="required" fixed="arc"/>
<xs:attribute ref="xlink:arcrole"/>
<xs:attribute ref="xlink:title"/>
<xs:attribute ref="xlink:show"/>
<xs:attribute ref="xlink:actuate"/>
<xs:attribute ref="xlink:from"/>
<xs:attribute ref="xlink:to">
<xs:annotation>
<xs:documentation>
from and to have default behavior when values are missing
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:attributeGroup>
<xs:group name="arcModel">
<xs:sequence>
<xs:element ref="xlink:title" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:group>
<xs:complexType name="arcType">
<xs:group ref="xlink:arcModel"/>
<xs:attributeGroup ref="xlink:arcAttrs"/>
</xs:complexType>
I try to access it by
<bindings node="xs:complexType[#name='arcType']">
<bindings node="xs:group/xs:element[#ref='xlink:title']">
<property name="arcModelTitle"/>
</bindings>
</bindings>
but it doesn't work…… what should I do about this?
What I want to do is : generate JAVA package from xsd by xjc. However,there’re some name conflict in the xsd, like the error below:
[ERROR] Property "Title" is already defined. Use <jaxb:property> to resolve this conflict.
line 197 of file:/D:/0Ubiloc/TEST/xlink.xsd
[ERROR] The following location is relevant to the above error
line 183 of file:/D:/0Ubiloc/TEST/xlink.xsd
so I want to redefined the name of the attribute or the element. But it failed to get the node(attribute/element) with ref, just like the code I stick out on the floor 1
Thanks in advance

Fully Qualified Xpath returns null

<Trial1Response xmlns="http://schemas.microsoft.com/Sql/2008/05/Procedures/dbo">
<Trial1Result>
<DataSet xmlns="http://schemas.datacontract.org/2004/07/System.Data">
<xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element msdata:IsDataSet="true" name="NewDataSet">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="NewTable">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="cust_id" type="xs:int">
</xs:element>
<xs:element minOccurs="0" name="fname" type="xs:string">
</xs:element>
<xs:element minOccurs="0" name="lname" type="xs:string">
</xs:element>
<xs:element minOccurs="0" name="addr" type="xs:string">
</xs:element>
<xs:element minOccurs="0" name="city" type="xs:string">
</xs:element>
<xs:element minOccurs="0" name="order_id" type="xs:int">
</xs:element>
<xs:element minOccurs="0" name="amount" type="xs:int">
</xs:element>
<xs:element minOccurs="0" name="particulars" type="xs:string">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<NewTable><cust_id>1</cust_id><fname>Akshay</fname><lname>Jain</lname><addr>Borivali</addr><city>Mumbai</city><order_id>221</order_id><amount>41</amount><particulars>Item B</particulars>
</NewTable>
<NewTable><id>223</id><Akshay1id>682</Akshay1id><amount>345</amount><particulars>Item A</particulars>
</NewTable>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
</Trial1Result>
<ReturnValue>0</ReturnValue>
</Trial1Response>
I want to extract the data between the tags NewDataSet. For this, I am using the following Xpath:
/*[local-name="Trial1Response" and namespace-uri()="http://schemas.microsoft.com/Sql/2008/05/Procedures/dbo"]/*[local-name="Trial1Result" and namespace-uri()="http://schemas.microsoft.com/Sql/2008/05/Procedures/dbo"]/*[local-name="DataSet" and namespace-uri()="http://schemas.datacontract.org/2004/07/System.Data"]/*[local-name="diffgr:diffgram" and namespace-uri()="urn:schemas-microsoft-com:xml-diffgram-v1"]/*[local-name="NewDataSet" and namespace-uri()=""]
I get null as my result. What am I doing wrong?
I am expected to use the fully qualified Xpath because Logic apps do not work without it
Edit: I have edited the XML after comments
Correct your xml.
And change local-name to local-name()
like this
/*[local-name()="Trial1Response" and namespace-uri()="http://schemas.microsoft.com/Sql/2008/05/Procedures/dbo"]/*[local-name()="Trial1Result" and namespace-uri()="http://schemas.microsoft.com/Sql/2008/05/Procedures/dbo"]/*[local-name()="DataSet" and namespace-uri()="http://schemas.datacontract.org/2004/07/System.Data"]/*[local-name()="diffgram" and namespace-uri()="urn:schemas-microsoft-com:xml-diffgram-v1"]/*[local-name()="NewDataSet" and namespace-uri()=""]
note: no need of using namespace-uri() if you have single/unique elements in the xml.

XSD context-dependent types; wrapper types

I am writing a schema for XML documents like this:
<workbook>
<worksheet>
<column/>
</worksheet>
</workbook>
Where each node can be wrapped into the env element (e.g.)
<workbook>
<env>
<worksheet>
<column/>
</worksheet>
</env>
</workbook>
or
<workbook>
<worksheet>
<env>
<column/>
</env>
</worksheet>
</workbook>
So, depending on the location of the env element, it can have different children (env-child of workbook must have worksheet children and env-child of worksheet must have column children)
When I am declaring env elements as nested, I get multiply-defined element errors.
How could I write xsd for such document, or is it at all possible?
Thanks in advance!
Yes, it is possible, because you can have elements with the same name env but with different types (structure) in different places - something linke this:
<xs:element name="workbook">
<xs:complexType>
<xs:choice>
<xs:element name="env">
<xs:complexType>
<xs:sequence>
<xs:element ref="worksheet"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element ref="worksheet"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="worksheet">
<xs:complexType>
<xs:choice>
<xs:element name="env">
<xs:complexType>
<xs:sequence>
<xs:element ref="column"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element ref="column"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="column" type="xs:string">
</xs:element>

case-insensitive key in xsd

I have an XSD embedded into an XML like this:
<Replacements>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Replacements">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Replace" />
</xs:sequence>
</xs:complexType>
<xs:key name="ReplaceKey">
<xs:selector xpath="./Replace"/>
<xs:field xpath="#old"/>
</xs:key>
</xs:element>
<xs:element name="Replace">
<xs:complexType>
<xs:attribute name="old" type="AnythingButLowerCase" use="required" />
<xs:attribute name="new" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:simpleType name="AnythingButLowerCase">
<xs:restriction base="xs:string">
<xs:pattern value="[^a-z]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
<Replace old="A1020____9" new="A1020"/>
<Replace old="a1020____9" new="A1020"/>
</Replacements>
I've used xs:key to define a unique-key on "old" attribute of Replace elements.
my problem is I want this key to be CASE-INSENSITIVE.
I've read so many documents indicating I can use xsd functions like upper-case or translate to solve this, but if I write something like
<xs:field xpath="upper-case(#old)"/>
VS2010 gives me a warning like this:
'upper-case(#old)' is an invalid XPath for selector or field.
What is it I'm doing wrong?
Thanks :)
The correct XPath function is "upper-case", not "upper case". Just add in the hyphen.
OK!
I got that!
Using functions in an xpath for a xs:field is not allowed.
The workaround for what I was seeking is to define a simpleType and put a restriction there not allowing lower-case letters.
Something like this:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Replacements>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Replacements">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Replace" />
</xs:sequence>
</xs:complexType>
<xs:key name="ReplaceKey">
<xs:selector xpath="./Replace"/>
<xs:field xpath="#old"/>
</xs:key>
</xs:element>
<xs:element name="Replace">
<xs:complexType>
<xs:attribute name="old" type="AnythingButLowerCase" use="required" />
<xs:attribute name="new" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:simpleType name="AnythingButLowerCase">
<xs:restriction base="xs:string">
<xs:pattern value="[^a-z]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
<Replace old="A1020____9" new="A1020"/>
<Replace old="a1020____9" new="A1020"/>
</Replacements>
Now this serves as what I want it to.

Resources