Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Schema and XML

Status
Not open for further replies.

akreation

Programmer
Feb 21, 2005
2
US
This XML and Schema are for listing specific places in the world. Like this...
<Earth><North_America><United_States><North_Carolina><Site_Name>

I am trying to import my old xml data into a new schema and need some help with the schema.I almost have this working, but I need a little help with some details.

Question: what do i need to include in the schema to check for <Earth><North_America><United_States><North_Carolina><Site_Name>
or other possibilities like
<Earth><South_America><Brazil><Amazon_County???><Site_Name> along with what the schema is already checking for.

I am a little new to the syntax and nesting heirarchy details of xml schema.

Do these just need to be added as complex elements? if so how?

-------OR--------

I am opening to changing, but would prefer the previous structure.

Should I include <Earth><North_Carolina><United_States><North_Carolina> as an element or attribute under the <Site_Name>. If I do this, Site_Name would be the Root, which would be ok.



Any suggestions about the layout would also be appreciated, I dont want to have to change schemata again in the future :)
_________________________XML____________________________

<Earth>
<North_America>
<North_Carolina>
<Site_Name>
<ID/>
<Short_Name Long_Name=""/>
<Information>
<Main_Quote Quote_Source=""/>
<Info1 Info1_Source=""/>
<Info2 Info2_Source=""/>
<Info3 Info3_Source=""/>
</Information>
<Experience Rating=""/>
<Images>
<Map Map_Caption="" Map_Source=""/>
<Image1 Image1_Caption="" Image1_Source="">1.tif</Image1>
<Image2 Image2_Caption="" Image2_Source="">2.tif</Image2>
<Image3 Imag3_Caption="" Image3_Source="">3.tif</Image3>
<Image4 Image4_Caption="" Image4_Source="">4.tif</Image4>
<Image5 Image5_Caption="" Image5_Source="">5.tif</Image5>
<Image6 Image6_Caption="" Image6_Source="">6.tif</Image6>
<Image7 Image7_Caption="" Image7_Source="">7.tif</Image7>
</Images>
<Details Type="" Hours="" Fees="" Elevation="" Lattitude="" Longitude="">
<Accessibility/>
<Contact/>
<Directions/>
</Details>
</Site_Name>
</North_Carolina>
</North_America>
</Earth>



__________________________Schema________________________

<?xml version="1.0"?>
<xs:schema xmlns:xs=" elementFormDefault="qualified">
<!-- definition of simple elements -->
<xs:element name="ID" type="xs:ID"/>
<!-- definition of attibutes-->
<xs:attribute name="Long_Name" type="xs:string"/>
<xs:attribute name="Quote_Source" type="xs:string"/>
<xs:attribute name="Info1_Source" type="xs:string"/>
<xs:attribute name="Info2_Source" type="xs:string"/>
<xs:attribute name="Info3_Source" type="xs:string"/>
<xs:attribute name="Rating" type="xs:string"/>
<xs:attribute name="Type" type="xs:string"/>
<xs:attribute name="Hours" type="xs:string"/>
<xs:attribute name="Fees" type="xs:string"/>
<xs:attribute name="Elevation" type="xs:string"/>
<xs:attribute name="Longitude" type="xs:string"/>
<xs:attribute name="Lattitude" type="xs:string"/>
<xs:attribute name="Directions" type="xs:string"/>
<xs:attribute name="Contact" type="xs:string"/>
<xs:attribute name="Accessability" type="xs:string"/>
<xs:attribute name="Map_Caption" type="xs:string"/>
<xs:attribute name="Map_Source" type="xs:string"/>
<xs:attribute name="Image1_Source" type="xs:string"/>
<xs:attribute name="Image2_Source" type="xs:string"/>
<xs:attribute name="Image3_Source" type="xs:string"/>
<xs:attribute name="Image4_Source" type="xs:string"/>
<xs:attribute name="Image5_Source" type="xs:string"/>
<xs:attribute name="Image6_Source" type="xs:string"/>
<xs:attribute name="Image7_Source" type="xs:string"/>
<xs:attribute name="Image1_Caption" type="xs:string"/>
<xs:attribute name="Image2_Caption" type="xs:string"/>
<xs:attribute name="Image3_Caption" type="xs:string"/>
<xs:attribute name="Image4_Caption" type="xs:string"/>
<xs:attribute name="Image5_Caption" type="xs:string"/>
<xs:attribute name="Image6_Caption" type="xs:string"/>
<xs:attribute name="Image7_Caption" type="xs:string"/>
<!-- definition of complex elements -->
<xs:complexType name="Short_Name">
<xs:attribute ref="Long_Name" use="optional"/>
</xs:complexType>
<xs:complexType name="Main_Quote">
<xs:attribute ref="Quote_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Info1">
<xs:attribute ref="Info1_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Info2">
<xs:attribute ref="Info2_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Info3">
<xs:attribute ref="Info3_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Experience">
<xs:attribute ref="Rating" use="optional"/>
</xs:complexType>
<xs:complexType name="Map">
<xs:attribute ref="Map_Caption" use="optional"/>
<xs:attribute ref="Map_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Image1">
<xs:attribute ref="Image1_Caption" use="optional"/>
<xs:attribute ref="Image1_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Image2">
<xs:attribute ref="Image2_Caption" use="optional"/>
<xs:attribute ref="Image2_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Image3">
<xs:attribute ref="Image3_Caption" use="optional"/>
<xs:attribute ref="Image3_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Image4">
<xs:attribute ref="Image4_Caption" use="optional"/>
<xs:attribute ref="Image4_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Image5">
<xs:attribute ref="Image5_Caption" use="optional"/>
<xs:attribute ref="Image5_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Image6">
<xs:attribute ref="Image6_Caption" use="optional"/>
<xs:attribute ref="Image6_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Image7">
<xs:attribute ref="Image7_Caption" use="optional"/>
<xs:attribute ref="Image7_Source" use="optional"/>
</xs:complexType>
<xs:complexType name="Details">
<xs:attribute ref="Type" use="optional"/>
<xs:attribute ref="Hours" use="optional"/>
<xs:attribute ref="Fees" use="optional"/>
<xs:attribute ref="Elevation" use="optional"/>
<xs:attribute ref="Longitude" use="optional"/>
<xs:attribute ref="Lattitude" use="optional"/>
<xs:attribute ref="Directions" use="optional"/>
<xs:attribute ref="Contact" use="optional"/>
<xs:attribute ref="Accessability" use="optional"/>
</xs:complexType>
</xs:schema>
 
Your schema doesn't make any sense at the moment, you are using all attributes. I would suggest reading up on schemas:


To a large extent, choosing the structure is a personal preference. A couple of points though:

For <Info1 Info1_Source=""/>, why not have simply have <Info1 Source=""/>? The Source obviously belongs to Info1 as it is it's own attribute.

Also instead of Info1, Info2, Info3, etc. Why not just use Info, Info, Info? You can specify in your schema how many Info tags there should be.

For this:
<Details Type="" Hours="" Fees="" Elevation="" Lattitude="" Longitude="">
<Accessibility/>
<Contact/>
<Directions/>
</Details>

Why not have Type, Hours, Fees, etc as elements with Accesssibility? What makes them different? As a rule, I try to make everything elements because they are easier to read, process and extend. Attributes are good for data which describes the data in the element in a succinct fashion, such as an ID.

I would suggest the following structure:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Earth>
  <North_America>
    <United_States>
      <North_Carolina>
        <Site ID="">
          <Short_Name/>
          <Long_Name/>
          <Information Source="">
            <Main_Quote Source=""/>
          </Information>
          <Experience Rating=""/>
          <Images>
            <Map Caption="" Source=""/>
            <Image Caption="" Source=""/>
          </Images>
          <Details Type="">
            <Hours/>
            <Fees/>
            <Elevation/>
            <Lattitude/>
            <Longitude/>
            <Accessibility/>
            <Contact/>
            <Directions/>
          </Details>
        </Site>
      </North_Carolina>
    </United_States>
  </North_America>
</Earth>

As for Question: what do i need to include in the schema to check for....:

You need to specify each combination you want.

Here's an example schema you could use (this is the first schema I've ever made, so there's probably a better way to do it:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]

  <!-- Reusable stuff -->
  <xs:attribute name="Source" type="xs:string"/>
  <xs:attribute name="Caption" type="xs:string"/>
  
  <xs:complexType name="Sitetype">
    <xs:sequence>
      <xs:element name="Site">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Short_Name" type="xs:string"/>
            <xs:element name="Long_Name" type="xs:string"/>
            <xs:element name="Information">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Main_Quote">
                    <xs:complexType>
                      <xs:simpleContent>
                        <xs:extension base="xs:string">
                          <xs:attribute ref="Source"/>
                        </xs:extension>
                      </xs:simpleContent>
                    </xs:complexType>
                  </xs:element>
                  <xs:element name="Info" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:simpleContent>
                        <xs:extension base="xs:string">
                          <xs:attribute ref="Source"/>
                        </xs:extension>
                      </xs:simpleContent>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="Experience">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute name="Rating" type="xs:string"/>
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
            <xs:element name="Images">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Map">
                    <xs:complexType>
                      <xs:simpleContent>
                        <xs:extension base="xs:string">
                          <xs:attribute ref="Caption"/>
                          <xs:attribute ref="Source"/>
                        </xs:extension>
                      </xs:simpleContent>
                    </xs:complexType>
                  </xs:element>
                  <xs:element name="Image" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:simpleContent>
                        <xs:extension base="xs:string">
                          <xs:attribute ref="Caption"/>
                          <xs:attribute ref="Source"/>
                        </xs:extension>
                      </xs:simpleContent>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="Details">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Hours" type="xs:string"/>
                  <xs:element name="Fees" type="xs:string"/>
                  <xs:element name="Elevation" type="xs:string"/>
                  <xs:element name="Lattitude" type="xs:string"/>
                  <xs:element name="Longitude" type="xs:string"/>
                  <xs:element name="Accessibility" type="xs:string"/>
                  <xs:element name="Contact" type="xs:string"/>
                  <xs:element name="Directions" type="xs:string"/>
                </xs:sequence>
                <xs:attribute name="Type" type="xs:string"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attribute name="ID" type="xs:integer"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  
  <!-- Main bit -->
  <xs:element name="Earth">
    <xs:complexType mixed="true">
      <xs:choice>
        <xs:element name="North_America">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="United_States">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="North_Carolina" type="Sitetype"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="South_America">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Brazil">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Amazon_County" type="Sitetype"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  
</xs:schema>
 
If you want to get fancy, you could make sure the longitude/lattitude values are valid:

Code:
<xs:simpleType name="longitude">
  <xs:restriction base="xs:string">
    <xs:pattern value="\d{2}&#176;\d{2}&#39;\d{2}&#34;(N|E|S|W)"/>
  </xs:restriction>
</xs:simpleType>

<xs:element name="Longitude" type="longitude"/>
 
Thanks for the suggestions guys! XML is very cool when you get the hang of it...Two more questions,
1)is there a way to change data types (from attribute to element) on a mass level, instead of having to change them individually?
2)what is the best way to import old data from an xml file into a new xml file with a different schema and data types?
 
1) Not really, although you could use XSL it's probably more hassle than changing each one yourself.

2) Use XSL:


For instance this stylesheet would change XML from your original structure to the structure I suggested:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Site_Name">
    <Site ID="{ID}">
      <Short_Name>
        <xsl:value-of select="Short_Name"/>
      </Short_Name>
      <Long_Name>
        <xsl:value-of select="Short_Name/@Long_Name"/>
      </Long_Name>
      <Information>
        <Main_Quote Source="{Information/Main_Quote/@Quote_Source}">
          <xsl:value-of select="Information/Main_Quote"/>
        </Main_Quote>
        <xsl:for-each select="Information/node()[position() &gt; 1]">
          <Info Source="{@*}">
            <xsl:value-of select="."/>
          </Info>
        </xsl:for-each>
      </Information>
      <Experience Rating="{Experience/@Rating}">
        <xsl:value-of select="Experience"/>
      </Experience>
      <Images>
        <Map Caption="{Images/Map/@Map_Caption}" Source="{Images/Map/@Map_Source}">
          <xsl:value-of select="Images/Map"/>
        </Map>
        <xsl:for-each select="Images/node()[position() &gt; 1]">
          <Image Caption="{@*[1]}" Source="{@*[2]}">
            <xsl:value-of select="."/>
          </Image>
        </xsl:for-each>
      </Images>
      <Details Type="{Details/@Type}">
        <Hours>
          <xsl:value-of select="Details/@Hours"/>
        </Hours>
        <Fees>
          <xsl:value-of select="Details/@Fees"/>
        </Fees>
        <Elevation>
          <xsl:value-of select="Details/@Elevation"/>
        </Elevation>
        <Lattitude>
          <xsl:value-of select="Details/@Lattitude"/>
        </Lattitude>
        <Longitude>
          <xsl:value-of select="Details/@Longitude"/>
        </Longitude>
        <Accessibility>
          <xsl:value-of select="Details/Accessibility"/>
        </Accessibility>
        <Contact>
          <xsl:value-of select="Details/Contact"/>
        </Contact>
        <Directions>
          <xsl:value-of select="Details/Directions"/>
        </Directions>
      </Details>
    </Site>
  </xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top