Creating an attribute is required only when setting another attribute

Is it possible to create the required attribute if another attribute is set?

eg. In the following code, the viewId required attribute must be created if the iff action attribute is set.

XML:

 <node id="id1" destination="http://www.yahoo.com" /> <node id="id2" action="demo" viewId="demo.asp"/> 

If possible, could you show me how this is done. At the moment, I have the viewId set required in all cases, which is why the first node element does not perform validation.

 <xsd:attribute name="focusViewId" type="xsd:anyURI" use="required"/> 
+4
source share
2 answers

This is not possible with XSD 1.0. You should use an XML Schema language other than or instead of XSD, or go to XSD 1.1.

Another alternative might be restructuring your scheme. If @destination is mutually exclusive with {@action, @viewId}, perhaps you could use the elements and then allow xsd:choice to be used.

UPDATE: for XSD 1.1

 <?xml version="1.0" encoding="utf-8" ?> <!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> <xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="node"> <xsd:complexType> <xsd:attribute name="id" type="xsd:ID" use="required"/> <xsd:attribute name="destination" type="xsd:string"/> <xsd:attribute name="action" type="xsd:string"/> <xsd:attribute name="viewId" type="xsd:string"/> <xsd:assert test="(@viewId) or not(@action)" /> </xsd:complexType> </xsd:element> </xsd:schema> 
+3
source

You can create an abstract complex type for your element "node" (call it abstactNode) that contains the @id definition.

Then create a complex type "nodeWithDestination" that extends abstactNode, with the definition of @destination.

The second complex type is "nodeWithActionAndViewId", which also extends abstactNode, with @action and @viewId attribute definitions.

Your XML will look like this:

 <node id="id1" destination="http://www.yahoo.com" xsi:type="nodeWithDestination"/> <node id="id2" action="demo" viewId="demo.asp" xsi:type="nodeWithActionAndViewId"/> 

Will this fit your needs?

+1
source

Source: https://habr.com/ru/post/1437993/


All Articles