简体   繁体   中英

T-SQL XQuery Node Value Path not Matching

While this works for retrieving local-name(.) node names as demonstrated here I can't get a table with resume first name and resume last name for each job candidate.

    USE AdventureWorks2012;

    WITH XMLNAMESPACES(
     'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume'
      AS ns)

      SELECT 
        T.rows.value('(ns:Name.First)[1]', 'nvarchar(100)') AS firstName, 
        T.rows.value('(ns:Name.Last)[1]', 'nvarchar(100)') AS lastName
      FROM HumanResources.JobCandidate
      CROSS APPLY
        Resume.nodes('//ns:Name/*') AS T(rows);

The error message lists the nodes that are in context, including what appears to be the targeted node.

XQuery [HumanResources.JobCandidate.Resume.value()]: There is no element named "{ http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume }: Name.First " in the type "element(ns{ http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume }:Name.Prefix,xs:string) | element(ns{ http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume }: Name.First ,xs:string)

...

element(ns{ http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume }:Name.Last,xs:string) | element(ns{ http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume }:Name.Suffix,xs:string)".

Is the problem that the element is a level down?

Problem was that context was a level too deep. Fixed by removing /* from nodes path.

WITH XMLNAMESPACES(
 'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume'
  AS ns)

  SELECT 
    T.rows.value('(ns:Name.First)[1]', 'nvarchar(100)') AS firstName, 
    T.rows.value('(ns:Name.Last)[1]', 'nvarchar(100)') AS lastName
  FROM HumanResources.JobCandidate
  CROSS APPLY
    Resume.nodes('//ns:Name/*') AS T(rows);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM