0

I want to introduce a new namespace and prefix "extending" hod:string from xsd:string. Then in my schema I could define a string element:

<xsd:element name="Label" type="hod:string"/>

This way I can change the maxLength of all elements of type hod:string, for example, to 80. If that requirement changes later to 50, I could simply change hod:string's definition in a single place. This is what I'm trying--my IDE doesn't like line 15 ("cannot resolve symbol hod:string"):

<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:hod="http://hod.com/2019/XMLSchema/hod"
       attributeFormDefault="unqualified" elementFormDefault="qualified">

  <xsd:simpleType name="hod:string">
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="80"/>
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:complexType name="Object">
    <xsd:sequence>
      <xsd:element name="Label" type="hod:string"/>
      <xsd:element name="MateriaID" type="GUID"/>
  ...

I changed all 'hod:string' with 'mystring' to verify I was defining things right and it seems happy, but I want to use the prefix if possible. (also tried to change this: attributeFormDefault="qualified" but it didn't seem to make a difference)

3
  • 2
    w3.org/TR/xmlschema11-1/#declare-datatype shows that the name attribute of the simpleType element needs to be an NCName so name="hod:string" is not allowed. If you want to declare a type in a particular namespace set up a schema with the namespace as the targetNamespace and xs:import the schema. Commented Aug 16, 2019 at 16:49
  • @MartinHonnen I tried what you said and the IDE is happy, but when I run it this exception is thrown: System.Xml.Schema.XmlSchemaValidationException : Type 'hod.com/2019/XMLSchema/hod:string' is not declared. Commented Aug 16, 2019 at 18:30
  • @MartinHonnen I got it working. I had figured the import would cause XDocument.Load to automatically load the .xsd file referenced in <xsd:import>. That is apparently not the case; I looked for an option to auto-load it but didn't find one. Calling XmlSchemaSet::add for the new file fixed it. Commented Aug 16, 2019 at 21:30

1 Answer 1

1

Simple types get their namespace from the targetNamespace attribute on the xs:schema element. Since your xs:schema element has no targetNamespace your current elements/types, including your custom string type, have no namespace.

If you want to keep your current elements in no namespace and only have your string type be namespaced, you need to define the type in a new schema that has a different targetNamepsace, and import that into this schema. For example:

hodtypes.xsd

<xsd:schema
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:hod="http://hod.com/2019/XMLSchema/hod"
       targetNamespace="http://hod.com/2019/XMLSchema/hod">

  <xsd:simpleType name="string">
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="80"/>
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>

main.xsd

<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:hod="http://hod.com/2019/XMLSchema/hod"
       attributeFormDefault="unqualified" elementFormDefault="qualified">

  <xsd:import namespace="http://hod.com/2019/XMLSchema/hod" schemaLocation="hodtypes.xsd" />

  <xsd:complexType name="Object">
    <xsd:sequence>
      <xsd:element name="Label" type="hod:string"/>
      <xsd:element name="MateriaID" type="GUID"/>
      ...
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, that worked. I just had to add the new schema to the XmlSchemaSet before validating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.