1

In resources like this we see examples like this:

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

Where the type is given as "xs:string". Does this assume that the XSD namespace is prefixed as xs.

For example, is it legal to open our XSD document something like?

<foo:schema xmlns:foo="http://www.w3.org/2001/XMLSchema">

And if we did so, would the first example become?

<foo:element name="name" type="foo:string"/>
Matthew James Briggs
  • 2,085
  • 2
  • 28
  • 58

2 Answers2

1

Does this assume that the XSD Schema namespace is aliased (not sure if that's the right term) as xs.

Yes, namespace prefixes such as xs must be declared:

xmlns:xs="http://www.w3.org/2001/XMLSchema"

Yes, you could use foo rather than xs, but go with xs or xsd as they've become the convention and so will surprise readers the least.


If you're defining types directly in your XML, you'll also want to declare,

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

and use it:

xsi:type="xs:string"

See also How to restrict the value of an XML element using xsi:type in XSD?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

Yes, I found an easier way to answer this than searching and reading. Using OxygenXML, the following is valid.

<?xml version="1.0" encoding="UTF-8"?>
<foo:schema xmlns:foo="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <foo:simpleType name="newSimpleType">
        <foo:restriction base="foo:string"/>
    </foo:simpleType>
</foo:schema>
Matthew James Briggs
  • 2,085
  • 2
  • 28
  • 58
  • I'm curious as to why you'd want to use a `foo` namespace prefix for `http://www.w3.org/2001/XMLSchema` rather than the more conventional `xs` or `xsd`? When experienced devs see `base="foo:string"`, they'll tend initially to think that the type is a custom string rather than the built-in string. – kjhughes Jul 10 '20 at 16:42
  • i completely agree with @kjhughes. Here is a good link about predefined namespaces in MS SQL Server: https://learn.microsoft.com/en-us/sql/xquery/sequence-and-qnames-xquery?view=sql-server-ver15#qname – Yitzhak Khabinsky Jul 10 '20 at 16:51
  • I don't want to, but I'm writing code that parses XSD and generates code from it, so I need to be, at least, thinking about edge cases instead of 'just getting the job done' with hard-coded mistakes like "xs:string" – Matthew James Briggs Jul 10 '20 at 16:56
  • 1
    @MatthewJamesBriggs. Understood. – Yitzhak Khabinsky Jul 10 '20 at 16:59
  • If you're using an XML parser, and you absolutely should be, then you'd have to go out of your way to develop a dependency on the particulars of the namespace prefix used. Simply do don't do that. – kjhughes Jul 10 '20 at 17:05