title: How to define a 'literal' in XSD
body: <p>I'm a newbie with XSD and pretty much at wits end.</p>
  
  <p>What I need to do is somehow define a 'literal' element within an XSD file so that users can use it in an XML document.</p>
  
  <p>For example:
  In the XML document, I want to let a user be able to add an element like this:</p>
  
  <pre><code>  &lt;WordBoundary/&gt;
  </code></pre>
  
  <p>but let the XSD file define not only the name 'WordBoundary' but also a list of Elements <em>within</em> which are of types defined elsewhere within the XSD.</p>
  
  <h2>Is this possible?</h2>
  
  <p>Updated to provide more information:</p>
  
  <p>I am trying to write an XSD (or set of XSDs) which provide a library of pre-defined elements for an end-user to use.
  The elements will ultimately be used to generate a Regular Expression but I want to hide the complexities of regular expressions from users as far as possible.
  The XSD will use .NETs XSD.Exe to generate C# classes.</p>
  
  <p>Currently I have defined elements like  and  and  etc. which work fine but rely on their ultimate regex pattern being defined in code. Some of the elements have a Pattern attribute to allow fine tuning but this clutters up the XML document for users somewhat.
  Examples of the built-up existing C# definitions:</p>
  
  <pre><code>const string BidMatch = @"(?&lt;" + BidGroupName + "&gt;" + DecimalNumberFragment + ")";
  const string OfferMatch = @"(?&lt;" + OfferGroupName + "&gt;" + DecimalNumberFragment + ")";
  
  const string BidOfferSpreadMatch = BidMatch + OptionalGap + RangeSeparatorFragment + OptionalGap + OfferMatch;
  </code></pre>
  
  <p>I therefore wanted to be able to refactor this so that all the regex patterns are moved from code  into XSD definitions to form a library (or if it turns out not to be possible an XML file looked up by name) whereby there are primitives such as  and  and commonly-used but more complex structures such as  etc.</p>
  
  <p>So the use can use predefined elements like  directly within their XML document but also be able to build up their own like this:</p>
  
  <pre><code>&lt;Group captureName="MyCustomMatch"&gt;
    &lt;WordBoundary/&gt;
          &lt;Digit/&gt;
          &lt;Literal pattern="[xyz]" /&gt;
          &lt;AnyOf/&gt;
              &lt;AnyOfChoice&gt;
                  &lt;DecimalNumber /&gt;
                  &lt;Gap /&gt;
                  &lt;DecimalNumber /&gt;
              &lt;/AnyOfChoice&gt;
              &lt;AnyOfChoice&gt;
                  &lt;LiteralText text="(" /&gt;
                  &lt;DecimalNumber /&gt;
                  &lt;LiteralText text=")" /&gt;
              &lt;/AnyOfChoice&gt;
    &lt;WordBoundary/&gt;
  &lt;/Group&gt;
  </code></pre>
  
  <p>(In fact the prefined elements in the library would be build up the same way)</p>
  
  <p>From the code point of view, I currently have AnyOf/Group etc. working by looking at the element type and calling a method to generate the pattern. For this to work, all these new Elements need to have a common ancestor, e.g. RegexLiteral, from which I can just read the Pattern attribute or whatever and add it into the full pattern.</p>
  
  <p>I have tried extending a common type and trying to override its Pattern attribute to use a fixed attribute but XSD does not apparently allow this.</p>
  
  <p>I am hoping this is just a limitation of my XSD knowledge rather than of XSD itself and hoping you clever guys know a way of achieving this.</p>
  
  <p>Update2:
  Thought I had it with this XSD fragment</p>
  
  <pre><code>&lt;xs:complexType name="LiteralFragment" abstract="true"&gt;
      &lt;xs:attribute name="Pattern" type="xs:string" /&gt;
  &lt;/xs:complexType&gt;
  
  &lt;xs:complexType name="Fred"&gt;
      &lt;xs:complexContent &gt;
          &lt;xs:extension base="LiteralFragment" &gt;
              &lt;xs:attribute name="Pattern" type="xs:string" fixed="BBB" /&gt;
          &lt;/xs:extension&gt;
      &lt;/xs:complexContent&gt;
  &lt;/xs:complexType&gt;
  </code></pre>
  
  <p>but XSD (and Xsd2Code for that matter) generate this rubbish code:</p>
  
  <pre><code>public partial class Fred : LiteralFragment
  {
  
      private string pattern1Field;
  
      public Fred()
      {
          this.pattern1Field = "BBB";
      }
  
      [System.Xml.Serialization.XmlAttributeAttribute("Pattern")]
      public string Pattern1
      {
          get { return this.pattern1Field; }
          set { this.pattern1Field = value; }
      }
  }
  </code></pre>
  
  <p>which bombs because there are two XmlAttributeAttribute with "Pattern" being used.
  What I need is an XSD generator that is intelligent enough to realise this and generate this code instead:</p>
  
  <pre><code>public partial class Fred : LiteralFragment
  {
      public Fred()
      {
          Pattern = "BBB";
      }
  }
  </code></pre>