Note that there are some explanatory texts on larger screens.

plurals
  1. POXSD: Adding attributes to strongly-typed "simple" elements
    primarykey
    data
    text
    <p>Is there some sensible way to have elements with strongly-typed simple-types and also attributes?</p> <p>Okay, I have an XSD schema which has a million (er, hundred) elements that might look like this:</p> <pre><code>&lt;xsd:element name="DocumentDescription" type="xsd:string" /&gt; &lt;xsd:element name="DocumentDateTime" type="xsd:dateTime" /&gt; &lt;xsd:element name="DocumentSize" type="xsd:int" /&gt; </code></pre> <p>That's dandy. However, I really want all of these elements to also have some common attributes on them like, let's say, "format" and "isVisible". i.e. have a schema like:</p> <pre><code>&lt;DocumentDescription isVisible="true"&gt;doc description&lt;/DocumentDescription&gt; &lt;DocumentDateTime format="dd/mm/yyyy" isVisible="true"&gt;1/1/2008&lt;/DocumentDescription&gt; &lt;DocumentSize format="0.00 KB" isVisible="false"&gt;5403&lt;/DocumentSize&gt; </code></pre> <p>I could do it manually, and horribly, by adding all such attributes to the XSD when I generate it, something like this:</p> <pre><code>&lt;xsd:element name="DocumentDescription" /&gt; &lt;xsd:complexType&gt; &lt;xsd:simpleContent&gt; &lt;xsd:extension base="xsd:string"&gt; &lt;xsd:attribute name="format" type="xsd:string" /&gt; &lt;xsd:attribute name="isVisible" type="xsd:boolean" /&gt; &lt;/xsd:extension&gt; &lt;/xsd:simpleContent&gt; &lt;/xsd:complexType&gt; &lt;xsd:element name="DocumentDateTime" /&gt; ... etc </code></pre> <p>...but in an ideal world I'd rather define it as a complexType:</p> <pre><code>&lt;xsd:complexType name="customType"&gt; &lt;xsd:complexContent&gt; &lt;xsd:extension base="???"&gt; &lt;xsd:attribute name="format" type="xsd:string" /&gt; &lt;xsd:attribute name="isVisible" type="xsd:boolean" /&gt; </code></pre> <p>...which means I could just do:</p> <pre><code>&lt;xsd:element name="DocumentDescription" type="customType" baseType="xsd:string" /&gt; &lt;xsd:element name="DocumentDateTime" type="customType" baseType="xsd:dateTime" /&gt; &lt;xsd:element name="DocumentSize" type="customType" baseType="xsd:int" /&gt; </code></pre> <p>The problem with my "ideal world" code is that:</p> <p><strong>a)</strong> I've no valid <code>&lt;xsd:extension base-"???"</code>>, since really I don't care what I'm extending; I want to extend all types. Seems like the "xsd:anyType" is appropriate, yet then the element becomes a weakly typed container does it not?</p> <p><strong>b)</strong> I can no longer specify the simple type on the <code>&lt;xsd:element</code>>, since now the type is the complex "customType" I defined. Hence the imaginary "baseType" attribute I put there...</p> <p>So can I add attributes to simple types in a non-clunky way? Or do I need to define a dozen complexTypes that are all identical except for the simple type that they extend?</p> <p>Strongly-typed elements not only describe the data more sensibly, but when I use them for XML mapping in Excel (and this is the whole purpose behind these things), the strong-typing means that Excel sets the cell formatting correctly based on the type.</p> <p>I'm probably looking at it all the wrong way! Any advice appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload