Syntax for xquery with namespace in the node

2019-08-26 01:23发布

We are trying to get IdValue from sql query. We are using sql server 2005.

DECLARE @MyXML XML
SET @MyXML = '<Candidate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ns.hr-xml.org/2007-04-15">
  <CandidateProfile>
    <ProfileId>
      <IdValue>9499063</IdValue>
    </ProfileId>
  </CandidateProfile>
  </Candidate>'

SELECT @MyXML.value('Candidate[1]/CandidateProfile[1]/ProfileId[1]','varchar(10)') AS Id

This is not working because of the name space in the Candidate tag.

Please let me know how to xquery with the namespace.

1条回答
淡お忘
2楼-- · 2019-08-26 02:00
    SELECT @MyXML.value(
    'declare namespace hr="http://ns.hr-xml.org/2007-04-15";
    hr:Candidate[1]/hr:CandidateProfile[1]/hr:ProfileId[1]','varchar(10)'
    ) 
    AS Id 

http://msdn.microsoft.com/en-US/library/ms189075(v=SQL.90).aspx

查看更多
登录 后发表回答