Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2006, 07:27 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to richb
Default Simple or Complex

I have a question that seems simple but there is a group of us struggling to find an adequate answer.

We want to declare a simple type with attributes. Seems simple enough, right?

Our XML should be able to have optional attributes or value...
<AmountType A="45" B="50" >100</AmountType>
<AmountType A="45" B="50" ></AmountType>
<AmountType>100</AmountType>

I would think the schema could look something like this...
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="Field">
    <xs:simpleContent>
      <xs:extension base="field_type">
        <xs:attribute name="A" type="xs:string" use="optional"/>
        <xs:attribute name="B" type="xs:string" Use="optional" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:simpleType name="field_type">
    <xs:restriction base="xs:string">
      <xs:pattern value="^[0-9]$" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

The problem is that the schema fails to validate if the element value is not present. We cannot get this to work unless we use a union (but that is not widely supported among all the parsers we are using). Is there a simple way to accomplish this otherwise? The basic problem is that we have a distinct xpath to a simple type that now needs more of a structure with it. We don't want to change the existing xpath to amount... just add more fields to optionally go along with it regardless of whether it exists or not.

Thanks

 
Old December 8th, 2006, 07:46 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>We want to declare a simple type with attributes.

By definition, a simple type has no attributes.

What you want is a "complex type with simple content", represented by the structure

<xs:complexType>
  <xs:simpleContent>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 8th, 2006, 07:53 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I shouldn't be answering posts without reading them to the end.

Firstly

 <xs:pattern value="^[0-9]$" />

is not a valid pattern in XML Schema (I think some schema processors are very lax in the regular expression syntax that they accept however). Patterns are implicitly anchored so you don't need the "^" and "$".

Secondly, if you fix it, then it is a pattern that matches a single digit. It looks to me from your examples as if any sequence of zero-or-more digits is valid, that's value="[0-9]*".

But are you sure this is really a string of zero or more digits? Isn't it better modelled as an optional integer? There are two ways of permitting an optional integer: you can define the union of (integer, ""), or you can define a list of integers with maxLength=1.

I'm afraid if you're using a buggy schema processor then I've no idea what will work and what won't. My advice would be to get rid of it.




Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 11th, 2006, 03:05 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to richb
Default

Thank you for the replies. You are correct regarding the pattern. To be honest, I just cut and paste that sample from an email i had from a vendor.

I probably confused the issue by trying to put the schema that failed. You can ignore the restriction put on the simple type. The core of the problem is different. Basically, I am looking to represent this possible combination in a schema.

<Amount A="45" B="50" >100</Amount>
<Amount A="45" B="50" ></Amount>
<Amount>100</Amount>

Is there a way to do this in XML schema such that the AmountType element value is optional as well as the attributes?

 
Old December 11th, 2006, 03:15 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I thought I already answered that. You know how to make the attributes optional, and I have explained several ways to make the content optional:

(a) leave it as a string with a pattern that matches an empty string, e.g. [0-9]*

(b) define it as a union of an integer or a zero-length string

(c) define it as a list of integers with maxLength="1".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 11th, 2006, 07:58 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to richb
Default

Ha! Looks like I am the one who did not read the complete post last time. I failed to read your complete post. Thanks for the input. This should work (making the string pattern allow for an empty string). Thanks

 
Old December 11th, 2006, 08:20 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to richb
Default

Okay... let's assume that I am a complete idiot and did not know that AmountType was defined as a decimal...

    <xsd:simpleType name="AmountType">
        <xsd:annotation>
            <xsd:documentation>Type for a U.S. amount field with 2 decimal places</xsd:documentation>
        </xsd:annotation>
        <xsd:restriction base="xsd:decimal">
            <xsd:totalDigits value="18"/>
            <xsd:fractionDigits value="2"/>
        </xsd:restriction>
    </xsd:simpleType>

I want to use the same general concept (and allow this simple type to be empty). Can this be done? Or can I somehow declare this as a string and put a regular expression that would mimic XXX.XX with 18 total digits and 2 decimal places as well as allow an empty string?

 
Old December 11th, 2006, 08:30 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

xsd:decimal doesn't allow an empty string, so no type that restricts xsd:decimal will allow an empty string either. To allow an empty string you need (as I said before) either to allow a union of decimals and (empty) strings, or allow a list of decimals containing 0 or 1 members.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 12th, 2006, 01:40 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to richb
Default

Well (as I said before), a union wont work for us. I liked the empty string idea so I was hoping for a solution down that route (reason for my question). I already heard your list idea, but thanks for typing it again anyway. Looks like you are saying that this is the only possible solution if I don't want to use a union. I guess I will go down that route.

(By the way... no reason to repeat yourself and tell me to upgrade parsers that may have issues with the union. The situation is much more complicated than that and there is no reason to go into it with this forum)

Thanks for the replies.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Expression Too Complex JCML Excel VBA 6 April 11th, 2007 08:36 AM
complex query g_vamsi_krish SQL Language 3 February 27th, 2006 10:48 AM
Complex sorting kjohnstone XSLT 1 November 26th, 2004 08:11 AM
Complex Dataviews Alaric ADO.NET 0 October 16th, 2003 07:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.