Pattern matching on xsd:element name

regex sqlbulkcopy xml xml-parsing xsd

Question

I am using SQL XML bulk load to inject the data direct to the database. My XML and xsd are as the following:

XML:

<?xml version="1.0"?>
<Data> 
  <USEFUL>
        <Value>3.1</Value>
        <Date>12/20/2001</Date>
  </USEFUL>

  <Something>
        <Value>3.1</Value>
        <Date>12/20/2001</Date>
  </Something>

</Data>

XSD:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:sql="urn:schemas-microsoft-com:mapping-schema">

    <xsd:simpleType name="anyElement">
        <xs:restriction base="xs:string">
            <xs:pattern value="[A-Za-z]+" />   
        </xs:restriction> 
    </xsd:simpleType>

    <xsd:element name="USEFUL" sql:relation="DATA" >

        <xsd:complexType>
           <xsd:sequence>
                <xsd:element name="Value" type="xsd:string" />
                <xsd:element name="Date" type="xsd:date" />
           </xsd:sequence>
        </xsd:complexType>

    </xsd:element>

    <xsd:element type="anyElement" sql:relation="NON-DATA" >

        <xsd:complexType>
           <xsd:sequence>
                <xsd:element name="Value" type="xsd:string" />
                <xsd:element name="Date" type="xsd:date" />
           </xsd:sequence>
        </xsd:complexType>

    </xsd:element>

</xsd:schema> 

I want to insert the "USEFUL" elements into the same table, and everything else into a different table. Is there a way to use the wildchar * for the name or a regular expression to filter the unwanted stuff into another table?

Thank you.

Popular Answer

No, it is not possible - there is no way to define in an XSD a complex type that applies to any element with a name different from a specified one. You'll have to define the type:

 <xsd:complexType name="anyElement"> 
    <xsd:sequence> 
        <xsd:element name="Value" type="xsd:string" /> 
        <xsd:element name="Date" type="xsd:date" /> 
    </xsd:sequence> 
 </xsd:complexType> 

and then list explicitely all the elements that go to the 'other' table:

 <xsd:element name="Something" type="anyElement" sql:relation="NON-DATA"/>
 <xsd:element name="SomethingElse" type="anyElement" sql:relation="NON-DATA"/>
 <xsd:element name="YetAnotherOne" type="anyElement" sql:relation="NON-DATA"/>
 . . . 



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why