XML validation
Is it possible to write a XML schema to validate the XML similar to the following XML, but the field name could be any string value, and the target type has variable fields, min 1.
<Mapping>
<DataSource>Sample</DataSource>
<Target type="Sample AddressBook"
configurable="yes">
<FirstName
map="First" display="First
Name"/>
<LastName
map="Last" display="Last Name"/>
<EmailAddress
map="Email" display="Email Address"
/>
<PhoneNumber
map="Phone" display="Phone Number"
/>
</Target>
</Mapping>
James
[965 byte] By [
JamesZ] at [2007-12-23]
It's not clear what you are asking for. When you say field do you mean element, like the FirstName, LastName and so on?
Element names cannot be any string, they are somewhat limited, for example they may not contain spaces.
If you are asking if you can write an xml schema type for the child elements of Target such that any element name is allowed but that they always have the attributes of map and display, then it is possible, sort of... The schema type could be
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:stuff"
xmlns:tns="urn:stuff"
>
<xsd:element name="Mapping">
<xsd:complexType >
<xsd:sequence>
<xsd:element name="Target">
<xsd:complexType>
<xsd:sequence>
<xsd:any namespace="urn:someNamespace" minOccurs="1" maxOccurs="unbounded" processContents="strict"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
You would still need another schema to define what an "any" elements could be.
You could also validate by using a check contraint and XQuery instead of an xml schema.
What are you trying to achieve?
Dan
Dan
What i need to do is to write a schema to validate the input XML similar to the above sample, so any name (of course, valid name, no space...) of the child elements of Target is allowed but that they always have the attributes of map and display.
Could you give me an example about schema for "any" elements in this case?
Thank you for your great help!
James
I don't know of any ay to do this in SQL Server 2005. Actually there is no way in the XSD standard to define something like "element name may be anything butelement must contain attributes such and such".
You can however use a skip wildcard and then use a check constraint that verifies the presence of the proper attributes. Below is an example implementation.
First we create a schema collection
CREATE XML SCHEMA COLLECTION SC AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="Mapping">
<complexType>
<sequence>
<element name="DataSource" type="string"/>
<element name="Target">
<complexType>
<sequence>
<any processContents="skip" namespace="##local" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="type" type="string"/>
<attribute name="configurable" type="string"/>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
'
go
Then we create a function that checks for the presence of the two attributes in all the children of the 'Target' element (the function returns 0 if it finds at least one element under 'Target' that doesn't contain both the 'display' and the 'map' attributes)
CREATE FUNCTION dbo.checkAttrs(@x XML(SC))
RETURNS bit
AS
BEGIN
RETURN ~(@x.exist('/Mapping/Target/*[not( ./@display and ./@map )]'))
END
go
Finally we can create a table with a constraint on the typed XML column
CREATE TABLE T(xmlCol XML(SC)
CHECK (1 = dbo.checkAttrs(xmlCol))
)
go
I hope this helps.
Denis Ruckebusch
Actually you can do it in the following way:
You define an abstract element that contains the two attributes and use it as a head in a substitution group. Then every new element type will have to be added as belonging to the substitution group defined by the abstract element. See for example the W3C XML Schema Primer at http://www.w3.org/TR/2004/REC-xmlschema-0-20041028/#SubsGroups.
This works outside SQL Server 2005's XML Schema collection very well, but if you want to use the schema collection then you will have to provide all the members of the substitution group at the time of the initial schema collection definition. We currently do not allow you to extend the substitution group with additional element declarations to the same schema collection. you would have to copy the schema collection into a new schema, add the element, create a new schema collection and use ALTER TABLE ALTER COLUMN to retype the column with the new schema collection.
Best regards
Michael