Click to See Complete Forum and Search --> : XML Restriction Problem


gold2040
05-06-2009, 07:38 AM
Hello

I'm working on an assignment and i'm trying to place a restriction on one of my XML values

here is my XML code


<cameras
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = 'cameras.xsd' >

<camera>
<model> Fujifilm Finepix J10 </model>
<price> 97.89 </price>
<megapixel> 3 </megapixel>
<image> camera.gif </image>
<logo> fujifilm.jpg </logo>
<blurb> Aluminium body. Movie capture with sound. 21 scene modes. Smile shutter. Previous catalogue price 199.99 </blurb>
<features>
<zoom> 10 </zoom>
<lcd> 2.5 </lcd>
<antiblur> Digital </antiblur>
<face> 10 </face>
<battery> Li-Ion </battery>
<format> SD/SDHC/xD </format>
<maxiso> 1600 </maxiso>
<depth> 1.9 </depth>
<catno> 559/1019 </catno>
</features>

</camera>

<camera>
<model> Sanyo E10 </model>
<price> 97.989 </price>
<megapixel> 10 </megapixel>
<image> camera2.gif </image>
<logo> sanyo.gif </logo>
<blurb> Aluminium body </blurb>
<features>
<zoom> 5 </zoom>
<lcd> 2.5 </lcd>
<antiblur> Digital </antiblur>
<face> 12 </face>
<battery> AA </battery>
<format> SD </format>
<maxiso> 2500 </maxiso>
<depth> 2.3 </depth>
<catno> 559/1325 </catno>
</features>

</camera>
<camera>
<model> Kodak M1063 </model>
<price> 87.99 </price>
<megapixel> 10 </megapixel>
<image> camera3.gif </image>
<logo> kodak.jpg </logo>
<blurb> Aluminium body </blurb>
<features>
<zoom> 3 </zoom>
<lcd> 2.7 </lcd>
<antiblur> Digital </antiblur>
<face> 3 </face>
<battery> Li-ion </battery>
<format> SD/SDHC </format>
<maxiso> 1000 </maxiso>
<depth> 2.1 </depth>
<catno> 580/1903 </catno>
</features>
</camera>

<camera>
<model> Sony 4GHDH </model>
<price> 87.99 </price>
<megapixel> 10 </megapixel>
<image> camera4.gif </image>
<logo> canon.gif </logo>
<blurb> Aluminium body </blurb>
<features>
<zoom> 3.3 </zoom>
<lcd> 2.5 </lcd>
<antiblur> Digital </antiblur>
<face> 9 </face>
<battery> AA </battery>
<format> SD/SDHC/MMC/MMC+</format>
<maxiso> 3200 </maxiso>
<depth> 15 </depth>
<catno> 580/0625 </catno>
</features>
</camera>

<camera>
<model> Sony 4GHDH </model>
<price> 136.99 </price>
<megapixel> 10 </megapixel>
<image> camera5.gif </image>
<logo> sony.gif </logo>
<blurb> Aluminium body </blurb>
<features>
<zoom> 4 </zoom>
<lcd> 2.7 </lcd>
<antiblur> Digital </antiblur>
<face> 0 </face>
<battery> Li-ion </battery>
<format> MSPD </format>
<maxiso> 0 </maxiso>
<depth> 2.9 </depth>
<catno> 580/1930 </catno>
</features>
</camera>

</cameras>


and here is my XSD code


<?xml version= "1.0" encoding= "UTF-8"?>

<xs:schema xmlns:xs = 'http://www.w3.org/2001/XMLSchema'>

<xs:element name= "cameras">

<xs:complexType>
<xs:sequence>
<xs:element ref = "camera" maxOccurs = 'unbounded' />
</xs:sequence>
</xs:complexType>

</xs:element>

<xs:element name= "camera">

<xs:complexType>
<xs:sequence>
<xs:element name = "model" type = "xs:string" />
<xs:element name = "price" type = "xs:decimal" />
<xs:element name = "megapixel" type = "xs:integer" />
<xs:element name = "image" type = "xs:string" />
<xs:element name = "logo" type = "xs:string" />
<xs:element name = "blurb" type = "xs:string" />
<xs:element ref = "features" />
</xs:sequence>
</xs:complexType>

</xs:element>

<xs:element name="megapixel">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="5"/>
<xs:maxInclusive value="15"/>
</xs:restriction>
</xs:simpleType>
</xs:element>


<xs:element name= "features">
<xs:complexType>
<xs:sequence>
<xs:element name = "zoom" type="xs:decimal" />
<xs:element name = "lcd" type="xs:decimal" />
<xs:element name = "antiblur" type="xs:string" />
<xs:element name = "face" type="xs:string" />
<xs:element name = "battery" type="xs:string" />
<xs:element name = "format" type="xs:string" />
<xs:element name = "maxiso" type="xs:decimal" />
<xs:element name = "depth" type="xs:decimal" />
<xs:element name = "catno" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>


the value I am using is "megapixel"
I am using a version of JEdit which is the software used on my course which has been customized
when I build and validate the code with a value 5 or over it validates fine
when I build and validate the code with a value less then 5 it still validates fine

am I doing something wrong?

dmboyd
05-06-2009, 01:09 PM
<!-- The stuff in red is your code, the stuff in blue is what should replace it. -->
<xs:element name="megapixel" type="xs:integer" />
<xs:element name="megapixel" type="megapixel.type" />
<!-- Skipping irrelevant code. -->
<xs:element name="megapixel">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="5"/>
<xs:maxInclusive value="15"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:simpleType name="megapixel.type">
<xs:restriction base="xs:integer">
<xs:minInclusive value="5" />
<xs:maxInclusive value="15" />
</xs:restriction>
</xs:simpleType>

The difference? I gave the simpleType that is properly restricted a name. That way, you can use it to declare "megapixel" with that type's name rather than xs:integer, which is how it is currently declared (as set of red-blue code I posted). Rather than making another "megapixel" element that is global (meaning that it can be used as the root of the XML document, just as your "camera" and "cameras" elements both can be), I simply removed the element declaration by making xs:simpleType stand on its own. In fact, you can also do this with xs:complexType, if you wanted to make sure nobody can make an XML document with only the following:
<camera>
<!-- The rest of your code is here. Notice how the beginning of the
XML document SHOULD be "cameras" rather than "camera"?
According to your schema, this use of "camera" is valid. -->
</camera>

You're on the right track, but I think you're still confused about how to wrap your head around some of the stuff. It can take a bit of time for some people. If you have any more issues or questions, you're always welcome to post again. ^_^

gold2040
05-06-2009, 01:55 PM
<camera>
<!-- The rest of your code is here. Notice how the beginning of the
XML document SHOULD be "cameras" rather than "camera"?
According to your schema, this use of "camera" is valid. -->
</camera>

You're on the right track, but I think you're still confused about how to wrap your head around some of the stuff. It can take a bit of time for some people. If you have any more issues or questions, you're always welcome to post again. ^_^

I'm actually interested in web design hence why I chose this XML module, now I just want to pass it
about the <cameras>, that came on a template our tutor posted as an example if I remember, pretty much changed the values, the Ant file we were supplied with validates it correctly anyway

kudos for the advice :)

I have one more query if you could help me :D

I have a snippet of code in a JSP file that validates the length of some data being input into a form


<c:set var = "name" value = "${ fn:trim( param.zoom ) }" />

<c:if test = "${ fn:length( name ) lt 6 }" >

<c:set var = "errorMessage" > please enter a numeric value </c:set>

</c:if>


obviously Zoom isn't being validated for length, so how would I get that to check if the data is numeric?, I think fn:number has something to do with it, no idea what comes after though :(, been looking at **** but no luck

and one more incy wincy question

i'm using that code you gave me regarding megapixels on the node "battery" with a set of set values
this is what I have


<xs:element name= "features">
<xs:complexType>
<xs:sequence>
<xs:element name = "zoom" type="xs:decimal" />
<xs:element name = "lcd" type="xs:decimal" />
<xs:element name = "antiblur" type="xs:string" />
<xs:element name = "face" type="xs:string" />
<xs:element name="catno" type="battery.type" />
<xs:element name = "format" type="xs:string" />
<xs:element name = "maxiso" type="xs:decimal" />
<xs:element name = "depth" type="xs:decimal" />
<xs:element name="catno" type="catno.type" />
</xs:sequence>
</xs:complexType>
</xs:element>


and


<xs:simpleType name="battery.type">
<xs:restriction base="xs:string">
<xs:enumeration value="Li-ion"/>
<xs:enumeration value="AA"/>
</xs:restriction>
</xs:simpleType


sorry for busting your balls lol, the 1st query is more important however

you can tell how useless I am with this <__<
______________