Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: support for xs:Alternative? #511

Open
irku0409 opened this issue May 28, 2024 · 3 comments
Open

Question: support for xs:Alternative? #511

irku0409 opened this issue May 28, 2024 · 3 comments

Comments

@irku0409
Copy link

I also apologize for posting a questions here, but I cannot find any answers elsewhere.

Is it correctly understood that xcsgen does not support xs:alternative?

I need to switch the attributes depending on the value of another attribute. E.g. if the value of attribute "type" is 1 then attributes UpLimit and LowLimit should be present, otherwise only attribute Limits should be present.
The solution I found was based on use of the xs:alternative, but when I use xscgen on my xsd file, no model file is generated.

Thanks for the help in advance!

@mganss
Copy link
Owner

mganss commented May 28, 2024

There is no specific support that would create code to enforce the attribute presence/absence. But I think the classes with properties for the attributes should be generated regardless. You would have to set the attribute values from user code according to the rules in the schema. Do you have an example schema file?

@irku0409
Copy link
Author

Thanks so much for reply!

Our customer provided us only with .doc-specification stating that different attributes should be used based on the value of another attribute. An xml sample could look like this

<?xml version="1.0" encoding="utf-8"?>
<root> 
  <node mode="B" LowLimit="string" UpLimit="string" />
  <node mode="A" Limits="string" />
</root>

They were unable to provide an xsd-schema, so I was trying to create a schema file and so far I've come up with this

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified" attributeFormDefault="unqualified">

  <xs:element name="root" type="root"/>

  <xs:element name="node" type="node">
        <xs:alternative type="node-type-A" test="@mode = 'A'"/>  
        <xs:alternative type="node-type-B" test="@mode = 'B'"/>  
  </xs:element>


  <xs:complexType name="root">
    <xs:sequence>
      <xs:element ref="node" minOccurs = "2" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <!-- Base type -->
  <xs:complexType name="node">
    <xs:attribute name="mode" type="allowed-node-types" use="required"/>
  </xs:complexType>

  <xs:simpleType name="allowed-node-types">
    <xs:restriction base="xs:string">
      <xs:enumeration value="A"/>
      <xs:enumeration value="B"/>
    </xs:restriction>
  </xs:simpleType>      

  <!-- Type A -->
  <xs:complexType name="node-type-A">
    <xs:complexContent>
        <xs:extension base="node">
            <xs:attribute name="Limits" type="xs:string" use="required"/>
      </xs:extension>  
    </xs:complexContent>
  </xs:complexType>

  <!-- Type B -->
  <xs:complexType name="node-type-B">
    <xs:complexContent>
      <xs:extension base="node">
        <xs:attribute name="LowLimit" type="xs:string" use="required"/>
		<xs:attribute name="UpLimit" type="xs:string" use="required"/>
      </xs:extension>  
    </xs:complexContent>
  </xs:complexType>  
</xs:schema>

Since no model file is generated for this schema file by usning xscgen, my other option is to declare all three attributes (Limits, LowLimit and UpLimit) as optional on Node-element and then set them according to the business rules in our code. That was also what your answer suggested, if I understood it correctly?

@mganss
Copy link
Owner

mganss commented May 31, 2024

I'm getting element is not supported in this context in Visual Studio's XML Schema editor. This is also the message in the exception that occurs when parsing the schema in XmlSchemaClassGenerator. I suspect either xs:alternative is not supported or the syntax is not correct.

Yes, what you describe in the last paragraph is exactly what I meant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants