2014-06-18 1 views
2

xs:date 속성이있는 스키마가 있습니다. 스키마는 날짜를 포함하거나 비어있는 방식으로 정의됩니다. 나는이 요소를 조회하려고 할 때xs : date 목록이 비어있을 수있는 요소를 xQuery하는 방법?

는하지만 오류

"XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xs:date *'" 

어떤 제안을 얻을?


단계

create xml schema collection dbo.[test] AS 
N'<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="PACKAGE" > 
    <xs:complexType> 
     <xs:choice minOccurs="0" maxOccurs="unbounded"> 
     <xs:element name="CUSTOMER"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="BIRTHDAY" > 
       <xs:annotation> 
        <xs:documentation>Date of Birth</xs:documentation> 
       </xs:annotation> 
       <xs:simpleType> 
        <xs:restriction> 
        <xs:simpleType> 
         <xs:list itemType="xs:date" /> 
        </xs:simpleType> 
        <xs:minLength value="0" /> 
        <xs:maxLength value="1" /> 
        </xs:restriction> 
       </xs:simpleType> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:choice> 
    </xs:complexType> 
    </xs:element> 
</xs:schema>'; 

go 


declare @xml xml(dbo.[test]); 

set @xml = 
'<PACKAGE> 
    <CUSTOMER> 
     <BIRTHDAY></BIRTHDAY> 
    </CUSTOMER> 
    <CUSTOMER> 
     <BIRTHDAY>2010-01-01</BIRTHDAY> 
    </CUSTOMER> 
</PACKAGE>' 


select 
    BIRTHDAY = t.cust.value('(BIRTHDAY)[1]', 'date') 
FROM @xml.nodes('/PACKAGE/CUSTOMER') as t(cust) 

go 

drop xml schema collection dbo.[test] 

답변

1

당신은 SQL Server의 data Function (XQuery)

select 
    BIRTHDAY = t.cust.value('data(BIRTHDAY)[1]', 'date') 
FROM @xml.nodes('/PACKAGE/CUSTOMER') as t(cust) 

기능 value()를 사용할 수있는 단일 값을 필요로하고 BIRTHDAY는 목록으로 정의된다 날짜. (BIRTHDAY)[1]은 (는) 리스트를 제공합니다. data(BIRTHDAY)[1]BIRTHDAY에 저장된 날짜 목록의 첫 번째 날짜를 알려줍니다.

+1

네 글자 만 ... ~ 고맙습니다. –

0

이있어 재현! (다른 방법)

select 
    BIRTHDAY = nullif(t.cust.query('BIRTHDAY').value('(BIRTHDAY)[1]', 'date'), '1900-01-01') 
from @xml.nodes('/PACKAGE/CUSTOMER') as t(cust) 

결과 :

BIRTHDAY 
---------- 
NULL 
2010-01-01 
관련 문제