how to combine element pattern with attributes?

M

MichaelD

Hi!

How do I combine an element pattern with attributes?

I can validate

<phone>111-111-1111</phone> using

pattern

I can validate presence of attributes

<phone name="xyx" value="123">111-111-1111</phone>

but now I want to validate the content of phone (111-111-1111) using
pattern
and the presence of the attributes name and value.

I am seem to be unable to find the right notation to combine the two

This is one description I think should work but doesn't (tried many
alternatives but haven't found the right one yet...)

<xsd:complexType name="Phone">
<xsd:simpleContent>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{3}-[0-9]{3}-[0-9]{4}"/>
</xsd:restriction>
</xsd:simpleContent>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="value" type="xsd:string" use="optional"/>
</xsd:complexType>

the content of name and value need not be validated, just the presence
as described in the 'use'

This should be simple i guess, but I it click yet in my mind...

any help appreciated!!!

Michael
 
P

Priscilla Walmsley

Hi Michael,

You need to do it in two steps:

<xsd:simpleType name="PhoneNumber">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{3}-[0-9]{3}-[0-9]{4}"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="Phone">
<xsd:simpleContent>
<xsd:extension base="PhoneNumber">
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="value" type="xsd:string" use="optional"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>

Hope that helps,
Priscilla
 
M

Michael

Priscilla,
thanks I got to that structure as well, but Word (Office 2003) does not like
that and complains:
xsd:phoneNumber not valid ... on the xsd:extensio base= part.

Interesting book on your site, does it cover this sort of stuff?

We are trying to use Word built in XML to validate document based on a
schema.
 
M

Martin Honnen

Michael wrote:

I got to that structure as well, but Word (Office 2003) does not like
that and complains:
xsd:phoneNumber not valid ... on the xsd:extensio base= part.

I have tried the following examples with MSXML 5 (which Office 2003
uses) and I get no problems validating with script:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:simpleType name="PhoneNumber">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{3}-[0-9]{3}-[0-9]{4}"/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="Phone">
<xs:simpleContent>
<xs:extension base="PhoneNumber">
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="value" type="xs:string" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>

<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="phone" type="Phone" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

XML instance:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2004102501Xsd.xml">
<phone name="Lance">111-111-1111</phone>
</root>

Maybe someone here can help when you post the complete example causing
the error.
 
P

Priscilla Walmsley

Hi Michael,

If you're using a target namespace (which I assume you are if you're
using Word 2003), you need to reference the PhoneNumber simple type
using its _qualified_ name.

For example, if you map your target namespace to the prefix xyz, you
would use base="xyz:phoneNumber", as in:

<xs:schema targetNamespace="http://xyz"
xmlns:xyz="http://xyz"
.....

<xsd:complexType name="Phone">
<xsd:simpleContent>
<xsd:extension base="xyz:phoneNumber">
....
Interesting book on your site, does it cover this sort
of stuff?

If you're talking about my schema book (Definitive XML Schema), yes it
covers all this. My other book (XML in Office 2003) covers using Word
with XML in detail, and has a one-chapter overview of XML Schema.

Thanks,
Priscilla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top