2010-02-04 4 views
1

serveur SQL a un support pour XML, mais je ne peux pas comprendre comment le faire fonctionner avec le type de listeSQL Server XSD SimpleType xs: liste en tant que table?

<?xml version="1.0" encoding="utf-16"?> 
<xsd:schema id="XMLSchema1" 
    targetNamespace="http://tempuri.org/XMLSchema1.xsd" 
    elementFormDefault="qualified" 
    xmlns="http://tempuri.org/XMLSchema1.xsd" 
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:simpleType name="valuelist"> 
    <xsd:list itemType="xsd:integer"/> 
    </xsd:simpleType> 
    <xsd:element name="a" type="valuelist"/> 

Je ne peux pas comprendre comment faire ce travail:

DECLARE @p0 AS XML 
SET @p0 = '<a>123 124</a>' 
select ??? from @p0.??? 

celui-ci fonctionne très bien, mais il a une surcharge de 6 caractères supplémentaires par nombre:

DECLARE @p0 AS XML 
SET @p0 = '<b>123</b><b>124</b>' 
select T.c.value('.', 'int') as Id from @p0.nodes('/b') AS T(c) 

Répondre

1

Voici un exemple entièrement:

DECLARE @Schema XML 
SET @Schema = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:element name="a"> 
    <xs:simpleType> 
     <xs:list itemType="xs:int" /> 
    </xs:simpleType> 
</xs:element> 
</xs:schema>' 

CREATE XML SCHEMA COLLECTION exampleschema as @Schema; 
GO 

DECLARE @XmlDoc AS XML(exampleschema) 
SET @XmlDoc = '<a>123 456 789</a>' 
select T.ref.value('.', 'int') 
from 
(
     select [Xml][email protected](' 
      for $i in data(/a) return 
      element temp { $i } 
     ') 
) A 
CROSS APPLY A.Xml.nodes('/temp') T(ref) 

DROP XML SCHEMA COLLECTION exampleschema 
GO 

Ceci est à peu près pris de this le blog MSDN qui démontre l'approche.

Questions connexes