Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XML Schema - 1 Element, Different Attributes, Can One Create Multiple Types?

0 views
Skip to first unread message

MaggotChild

unread,
Jun 30, 2009, 4:28:46 PM6/30/09
to
I have an instance document where the same element has a wide variety
of Name and Value attributes:

<Data>
<Info Product="XXX" Type=""/>
<Detail Name="Added" Value="2009-01-01"/>
<Detail Name="Cost" Value="25.99"/>
</Data>

There are 50-80 different Nave/Value pairs, all having very specific
validation needs.
I'd like to be able to do this for each pair:

<xsd:element name="Detail" id="Added">
<xsd:complexType>
<xsd:attribute name="Name">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Cost"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>

<xsd:attribute name="Value">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="^\d{2}\.\d{2}$"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>

But, since each rule is based on the Detail element, it is illegal.

Is it possible to create a XML Schema for this, without having to use
XSI in the instance doc?

Maybe RNG (or something else) would be better suited.


Neil Smith [MVP Digital Media]

unread,
Jul 1, 2009, 10:57:34 AM7/1/09
to
Hm I've had a think about this, and it's not obvious that XSD contains
the feature you want (referencing the content of an attribute to
determine the validation rule required)

So it seems you may have to use a 2-pass approach.

A way to consider it with XSL, would be to validate your primary
document structure first. Then transforming the doc to include
xsi:type based on each matching Detail node's Name attribute; and
finally validating using the xsi:type specified

Or transforming the doc to a more element based structure
<Detail><Added>{value</Added></Detail> then validating the result of
that transformation using the Element name as usual.

Probably there's not much to choose in processing time, though I
consider transforming to Element based validation makes for easier
readability during testing.

In either case, it seems your input XML document is designed to
frustrate validation even though its a simple structure - if you have
any influence on the input document, I'd consider restructuring it.

HTH
Cheers - Neil

------------------------------------------------
Digital Media MVP : 2004-2009
http://mvp.support.microsoft.com/mvpfaqs

MaggotChild

unread,
Jul 1, 2009, 6:35:54 PM7/1/09
to
On Jul 1, 7:57 am, "Neil Smith [MVP Digital Media]" <n...@nospam.com>
wrote:

> Hm I've had a think about this, and it's not obvious that XSD contains
> the feature you want (referencing the content of an attribute to
> determine the validation rule required)
>
> So it seems you may have to use a 2-pass approach.
>
> A way to consider it with XSL, would be to validate your primary
> document structure first. Then transforming the doc to include
> xsi:type based on each matching Detail node's Name attribute; and
> finally validating using the xsi:type specified

The problem is that cost is a required element.

<Detail Name="Cost" Value="25.99"/>

becomes, say

<Detail Name="Cost" Value="25.99" xsi:type="Cost"/>

How do I say a certain XSI type is required? I don't think I can.

> Or transforming the doc to a more element based structure
> <Detail><Added>{value</Added></Detail> then validating the result of
> that transformation using the Element name as usual.

That might be the only way to say that a certain element is required.
Creating a new structure that wont replacing the existing one sucks,
but it seems to be the only option.

> In either case, it seems your input XML document is designed to
> frustrate validation even though its a simple structure - if you have
> any influence on the input document, I'd consider restructuring it.

Yes, the design is horrible. I change it.

Thanks

MaggotChild

unread,
Jul 1, 2009, 6:37:22 PM7/1/09
to
On Jul 1, 3:35 pm, MaggotChild <hsomob1...@yahoo.com> wrote:
> On Jul 1, 7:57 am, "Neil Smith [MVP Digital Media]" <n...@nospam.com>
> > In either case, it seems your input XML document is designed to
> > frustrate validation even though its a simple structure - if you have
> > any influence on the input document, I'd consider restructuring it.
>
> Yes, the design is horrible. I change it.


I meant to say that I can't change it.


Neil Smith [MVP Digital Media]

unread,
Jul 2, 2009, 5:47:06 AM7/2/09
to
On Wed, 1 Jul 2009 15:35:54 -0700 (PDT), MaggotChild
<hsomo...@yahoo.com> wrote:

>On Jul 1, 7:57�am, "Neil Smith [MVP Digital Media]" <n...@nospam.com>
>wrote:
>> Hm I've had a think about this, and it's not obvious that XSD contains
>> the feature you want (referencing the content of an attribute to
>> determine the validation rule required)
>>
>> So it seems you may have to use a 2-pass approach.
>>
>> A way to consider it with XSL, would be to validate your primary
>> document structure first. Then transforming the doc to include
>> xsi:type based on each matching Detail node's Name attribute; and
>> finally validating using the xsi:type specified
>
>The problem is that cost is a required element.
>
><Detail Name="Cost" Value="25.99"/>
>
>becomes, say
>
><Detail Name="Cost" Value="25.99" xsi:type="Cost"/>
>
>How do I say a certain XSI type is required? I don't think I can.


A guy in a comment right at the bottom of this page
http://norman.walsh.name/2004/01/29/trainwreck
referred to a preceding comment stating XML schema or DTD can't do
this (a step matching your proposal) but RelaxNG can.

I don't know much about RelaxNG though.

It seems from the little I know about Schematron, that you may just be
able to embed the schematron rules in the XSD, then post-process
(again a second step) using a Schematron processor - I know there's a
class to do this I've seen written in PHP4 but not (so far) ported to
PHP5 or 6.

HTH
Cheers - Neil

MaggotChild

unread,
Jul 2, 2009, 3:07:13 PM7/2/09
to
On Jul 2, 2:47 am, "Neil Smith [MVP Digital Media]" <n...@nospam.com>
wrote:
> A guy in a comment right at the bottom of this pagehttp://norman.walsh.name/2004/01/29/trainwreck

> referred to a preceding comment stating XML schema or DTD can't do
> this (a step matching your proposal) but RelaxNG can.

I'll have to have a look at Relax NG.

> It seems from the little I know about Schematron, that you may just be
> able to embed the schematron rules in the XSD, then post-process
> (again a second step) using a Schematron processor

If I can do this with Relax NG then the added complexity in not
necessary.

Do you know if Visual Studio has a plugin that will provide auto sense
for Relax NG?
I think it would make it easier to learn.

Thanks Again

0 new messages