2

I'm used to create XML file but quite new to create my own XSD file for validating the XML content. So i want to ask few questions about XSD file.

First, i have an XML file. Now i need to create an XSD file to strictly validate the XML content. But i also want to use the same XSD file to generate some UI in my website for user to input the value. Like for each tag or each attribute within each tag, we will have 1 input, select box, toggle button, etc. After that, using the inputted value and the XSD file structure to generate the XML file.

But now i have a problem with these two case:

  1. I will need a label to show our user what this input field for. I can put an attribute with fixed value in each tag to use it for the label. But the out XML tag doesn't need this attribute. So how can i add something in the XSD file to store the label for each tag without affecting the XML file?

  2. I want to create a dropdown list, choose one of its value and then when generate, the XML file will have the selected value as one of its attribate value. Well to do this, i can use restriction using the enumeration constraint. But sometime the value is hard to understand so i need something else to show instead, like the value is 1 but the text show as "Running" for example. This will be kind of like the above question, but need for the restriction part.

So can anyone help me with these 2 case. What tag, attribute should i use in the XSD file to store these unrelated information to XML file?

Thanks you.

====================================================

Update : To be more specific about my question. So for example i have this in my xsd file

<xs:element name="car">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Audi"/>
      <xs:enumeration value="Golf"/>
      <xs:enumeration value="BMW"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element> 

This one only have a restricted value to validate the xml tag. But i want to add another value to each enumeration tag to store a label or something like that:

<xs:element name="car">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Audi" label="Option 1"/>
      <xs:enumeration value="Golf" label="Option 2"/>
      <xs:enumeration value="BMW" label="Option 3"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element> 

And this added thing won't make the xsd file invalid nor effect the validation of the xml file.

============================================================

Update 2:

I would need to use the xsd file to generate UI like this selectbox

With the value as value of each option and label as the display text of each option. When user choose for example option 2 which is Golf, the generate xml will be like this:

<car>Golf</car>

So these labels purpose purely to generate the UI, nothing to do with the xml.

mameo
  • 639
  • 8
  • 25
  • Please, read [how to ask useful questions](https://stackoverflow.com/help/mcve). You have a t least 2 different problems: XSD + XML and XML + (a specific OS usage). – LMC Mar 23 '18 at 00:44
  • @LuisMuñoz : Why there a problem with XSD + XML and XML + (a specific OS usage)? What i want here is to add some value to XSD file which not relate to XML validation so that i can use them for other purpose – mameo Mar 23 '18 at 19:17
  • what I meant is that you are asking a too brad question, how to build xml with xsd and how to apply that xml to an specific usage. That's why I pointed you to the MCVE help page. – LMC Mar 23 '18 at 19:24
  • @LuisMuñoz : Nope i didn't ask to build xml with xsd but ask to put something into xsd and use the xsd file for other thing. You can look at the updated question. – mameo Mar 23 '18 at 19:27
  • Please add the expected xml output. You probably need an [](https://stackoverflow.com/questions/10025419/how-can-i-add-a-restriction-to-a-complextype-in-xml-xsd-schema) – LMC Mar 23 '18 at 19:39
  • @LuisMuñoz: Updated the question – mameo Mar 23 '18 at 19:50

1 Answers1

2

1. You can use <xsd:annotation> for it.

<xs:annotation>
 <xs:appinfo>
    <!-- Add arbitrary xml here e.g. -->
    <label>Audi</label>
 </xs:appinfo>
</xs:annotation>

Just put the annotation next to your element.

2. Look how others used xsd:annotation for similar purposes.

- example on how to use annotations on an enumeration

- a form generation tool on basis of xsd xsd-forms

3. Similar questions:

Toolkits or Applications That Build UI From Xsd

jschnasse
  • 8,526
  • 6
  • 32
  • 72
  • Yes this is what i'm looking for. Annotation work like a charm. For the link to the tool, our customer don't allow any third party or open source so we will have to make the generation code our self. Anyway, thank you very much. – mameo Mar 23 '18 at 20:05
  • 1
    The xsd-forms readme has a section on "how to generate a form". It includes a small snipped on how they use annotations. – jschnasse Mar 23 '18 at 20:11