Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Problem selecting XML data Expand / Collapse
Author
Message
Posted Monday, May 20, 2013 10:55 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, October 25, 2013 3:50 PM
Points: 2, Visits: 15
Hello community,

I have a problem when selecting data from an XML column.

I have imported the data using the following statement:

CREATE TABLE XmlImport(XmlResult XML);
GO

INSERT INTO XmlImport(XmlResult)
SELECT * FROM OPENROWSET(
--BULK 'c:\SampleFolder\SampleData3.txt',
BULK 'c:\SampleFolder\k5Entleerung_Export_Beispiel1.xml',
SINGLE_BLOB) AS x;

Then I select the imported data with the following statement:

WITH XMLNAMESPACES(DEFAULT 'http://www.k-5.at/Schema/Entleerung:001')
SELECT
Export.Gemeindekennziffer.value('.', 'nchar(5)') as GemeindeKennZiffer
,substring(Expo.Erstellungsdatum.value('.','nchar(10)'),1,4) as Jahr
,substring(Expo.Erstellungsdatum.value('.','nchar(10)'),6,2) as Monat
,'D' as Recordtype
,KueDet.KueGr.value('.','nchar(30)') as Tarifbezeichnung
,Kuebel.tag.value('.','nchar(16)') as TAG
FROM (SELECT XmlResult FROM XmlImport) AS b(x)
cross apply x.nodes('EntleerungExports/ÖStatNummer') as Export(GemeindeKennZiffer)
cross apply x.nodes('EntleerungExports/Erstellungsdatum') as Expo(Erstellungsdatum)
cross apply x.nodes('EntleerungExports/Kübel/Tagnummer') as Kuebel(TAG)
cross apply x.nodes('EntleerungExports/Kübel/Kübeldetails/Kübelgröße') as KueDet(KueGr)

which leads to the following (undesired) results:

GemeindeKennZiffer Jahr Monat Recordtype Tarifbezeichnung TAG
------------------ ---- ----- ---------- ------------------------------ ----------------
70367 1967 08 D 120.55 CBC9580900000000
70367 1967 08 D 80.00 CBC9580900000000
70367 1967 08 D 125.00 CBC9580900000000
70367 1967 08 D 120.55 DBC5682500000000
70367 1967 08 D 80.00 DBC5682500000000
70367 1967 08 D 125.00 DBC5682500000000
70367 1967 08 D 120.55 DBC9876500000000
70367 1967 08 D 80.00 DBC9876500000000
70367 1967 08 D 125.00 DBC9876500000000

(9 row(s) affected)

which should be, of course:

GemeindeKennZiffer Jahr Monat Recordtype Tarifbezeichnung TAG
------------------ ---- ----- ---------- ------------------------------ ----------------
70367 1967 08 D 120.55 CBC9580900000000
70367 1967 08 D 80.00 DBC5682500000000
70367 1967 08 D 125.00 DBC9876500000000


Please, can anybody help me to retrieve the data correctly.

I've attached the xml file as textfile.

Many thanx in advance,

Helmut
Post #1454636
Posted Tuesday, May 21, 2013 12:30 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, October 25, 2013 3:50 PM
Points: 2, Visits: 15
Meanwhile I have solved the problem myself:

;WITH XMLNAMESPACES(DEFAULT 'http://www.k-5.at/Schema/Entleerung:001')

SELECT
e.z.query('data(./ÖStatNummer)').value('.', 'nchar(5)') as GemeindeKennZiffer
,substring(e.z.query('data(./Erstellungsdatum)').value('.','nchar(10)'),1,4) as Jahr
,substring(e.z.query('data(./Erstellungsdatum)').value('.','nchar(10)'),6,2) as Monat
,'D' as Recordtype
,k.y.query('data(./Tagnummer)').value('.','nchar(16)') as TAG
,k.y.query('data(./Kunde/EDVNummer)').value('.','nchar(16)') as Personennummer
,ku.v.value('.','nchar(20)') as Vorname
,k.y.query('data(./Kunde/Adresse)').value('.','nchar(20)') as Adresse
,s.o.query('data(./Objektnummer)').value('.','nchar(16)') as Objektnummer
FROM (SELECT XmlResult FROM XmlImport) AS b(x)
cross apply x.nodes('EntleerungExports') as E(z)
cross apply e.z.nodes('Kübel') as K(y)
cross apply k.y.nodes('Kunde') as Ku(v)
cross apply k.y.nodes('Steuerobjekt') as S(o)
cross apply k.y.nodes('Abgabe') as a(g)

but I came across a new question:

how can I get the data from a group (complex type), e.g. 'AdresseGroup' or 'PersonnamenGroup', which is defined in the following way?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.k-5.at/Schema/Common/BaseTypes:001" targetNamespace="http://www.k-5.at/Schema/Common/BaseTypes:001" elementFormDefault="qualified" attributeFormDefault="qualified">
<xs:simpleType name="ZählernummerType">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="12"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="JahrType">
<xs:restriction base="xs:int">
<xs:minInclusive value="1900"/>
<xs:totalDigits value="4"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="AnzahlStellenType">
<xs:restriction base="xs:int">
<xs:minInclusive value="3"/>
<xs:maxInclusive value="8"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="EichintervallType">
<xs:restriction base="xs:int">
<xs:totalDigits value="1"/>
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="FabrikatType">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="EDVNummerType">
<xs:restriction base="xs:int">
<xs:minExclusive value="0"/>
<xs:maxInclusive value="99999999"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="DurchflussgrößeType">
<xs:restriction base="xs:decimal">
<xs:minExclusive value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="EMailType">
<xs:restriction base="xs:string">
<xs:pattern value="[^@]+@[^\.]+\..+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ObjektnummerType">
<xs:restriction base="xs:int">
<xs:minExclusive value="0"/>
<xs:maxInclusive value="99999"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="BetriebsnummerType">
<xs:restriction base="xs:int">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="99999"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="AbgabeNummerType">
<xs:restriction base="xs:int">
<xs:minExclusive value="0"/>
<xs:maxInclusive value="999"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="AbgabeLfdNrType">
<xs:restriction base="xs:int">
<xs:minExclusive value="0"/>
<xs:maxInclusive value="99999"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ÖStatNummerType">
<xs:restriction base="xs:int">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="99999"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="TagnummerType">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="20"/>
</xs:restriction>
</xs:simpleType>
<xs:group name="AdresseGroup">
<xs:sequence>
<xs:element name="Straße">
<xs:annotation>
<xs:documentation>Straße</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="150"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Hausnummer">
<xs:annotation>
<xs:documentation>Hausnummer (von)</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minExclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Restadresse" minOccurs="0">
<xs:annotation>
<xs:documentation>Restadresse</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Nutzungseinheit" minOccurs="0">
<xs:annotation>
<xs:documentation>Nutzungseinheit</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="PLZ">
<xs:annotation>
<xs:documentation>PLZ</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Ort">
<xs:annotation>
<xs:documentation>Ort</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Staat" minOccurs="0">
<xs:annotation>
<xs:documentation>Staat</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="40"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:group>
<xs:complexType name="AdresseType">
<xs:sequence>
<xs:group ref="AdresseGroup"/>
</xs:sequence>
</xs:complexType>
<xs:group name="PersonnamenGroup">
<xs:sequence>
<xs:element name="Nachname">
<xs:annotation>
<xs:documentation>Nachname</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="60"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Vorname">
<xs:annotation>
<xs:documentation>Vorname</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="20"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Titel" minOccurs="0">
<xs:annotation>
<xs:documentation>Titel / akad. Grad</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="TitelNachgestellt" minOccurs="0">
<xs:annotation>
<xs:documentation>Titel nachgestellt</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:group>
<xs:complexType name="PersonnamenType">
<xs:sequence>
<xs:group ref="PersonnamenGroup"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ZustellbevollmächtigterType">
<xs:sequence>
<xs:element name="EDVNummer" type="EDVNummerType" minOccurs="0">
<xs:annotation>
<xs:documentation>EDV-Nr des Zustellbevollmächtigten</xs:documentation>
</xs:annotation>
</xs:element>
<xs:group ref="PersonnamenGroup"/>
<xs:group ref="AdresseGroup"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

Does anybody have an idea how to solve this problem? Please reply.
Post #1454826
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse