Re: XML , schema

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Graeme Malcolm (graemem_cm_at_hotmail.com)
Date: 07/29/04


Date: Thu, 29 Jul 2004 16:07:21 +0100

Install SQLXML 3.0
(http://www.microsoft.com/downloads/details.aspx?FamilyID=4C8033A9-CF10-4E22
-8004-477098A407AC&displaylang=en) and use an annotated XSD schema. You can
extract the data in the appropriate XML format through an IIS virtual root,
ADO, or ADO.NET.

Here's an example that uses ADO:

Schema:
<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xsd:element name="Catalog" sql:is-constant="1">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Category" sql:relation="Categories">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="CategoryName" type="xsd:string"
sql:field="CategoryName" />
              <xsd:element name="ProductList" sql:is-constant="1">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="Product" sql:relation="Products">
                      <xsd:annotation>
                        <xsd:appinfo>
                          <sql:relationship parent="Categories"
                                            parent-key="CategoryID"
                                            child="Products"
                                            child-key="CategoryID" />
                        </xsd:appinfo>
                      </xsd:annotation>
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="ProductName" type="xsd:string"
sql:field="ProductName" />
                          <xsd:element name="Price" type="xsd:decimal"
sql:field="UnitPrice" />
                        </xsd:sequence>
                        <xsd:attribute name="ProductCode" type="xsd:integer"
sql:field="ProductID" />
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="CategoryID" type="xsd:integer"
sql:field="CategoryID" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

ADO code to extract data:
Const DBGUID_XPATH = "{ec2a4293-e898-11d2-b1b7-00c04f680c56}"
Const adExecuteStream = 1024
Const adReadAll = -1
Dim conn
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "SQLXMLOLEDB.3.0"
conn.ConnectionString = "DATA PROVIDER=SQLOLEDB;" & _
    "SERVER=(local);DATABASE=northwind;INTEGRATED SECURITY=sspi;"
conn.Open
Dim cmd
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
'Set the dialect
cmd.Dialect = DBGUID_XPATH
'Create Stream object for results.
Dim stmXMLout
Set stmXMLout = CreateObject("ADODB.Stream")
'Assign the result stream.
stmXMLout.Open
cmd.Properties("Output Stream") = stmXMLout
'Specify the schema
cmd.Properties("mapping schema") = "Catalog.xsd"
'Specify the XPath
cmd.CommandText = "Catalog"
'Execute the command returning a stream
cmd.Execute, , adExecuteStream
'Move to the beginning of the stream
stmXMLout.Position = 0
WScript.Echo stmXMLout.ReadText(adReadAll)

Hope that helps!
Graeme

-- 
----
Graeme Malcolm
Principal Technologist
Content Master Ltd.
www.contentmaster.com
www.microsoft.com/mspress/books/6137.asp
"Rena" <Rena@mail.hongkong.com> wrote in message
news:e5vM0qXdEHA.2520@TK2MSFTNGP12.phx.gbl...
Hi all expert,
    I would like to know if there are any way for SQL Server to output
required XML format (attribute / element)  base on an schema / mapping file?
    I know there is auto xml syntax for XML but could there be some way to
auto feed a XML
schema and export the expected XML  as output?
is SQL eligable to do it? or any tools could help?
Thanks  alot for help
Rena.


Relevant Pages

  • Re: Importing XML data into Access and exporting Access table to XML form
    ... through so many hoops to transform between the XML schema that ADO produced ... used by ADO or by the new XML features in Access 2003. ...
    (microsoft.public.access.externaldata)
  • Re: 8192 character limit in Query Analyser when returning data, how to save result without newline e
    ... You can write a small VB Script using ADO that will use the ADO result ... stream to write the FOR XML result directly to a file. ... The Query Analyzer used ODBC, that had no streaming result support ...
    (microsoft.public.sqlserver.xml)
  • Re: XML aus XSD erstellen
    ... wie man mit CodeDOM eine Klasse aus einem XML Schema erzeugt. ... XmlDocument aus dem Stream instanziieren: ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • RE: ADO XML question
    ... are saying but with one exception, how would I use the XSL ... in ADO to extract the correct data? ... the XSL file to obtain the desired XML formatted file. ... >> (to a specified schema). ...
    (microsoft.public.vb.database.ado)
  • Re: Recordset load XML
    ... file, if you are using a version of ADO that supports the Stream object, you ... new Recordset ... Recordset return so I save the RecordSet to an XML string: ...
    (microsoft.public.dotnet.languages.csharp)