使用T-SQL和XQUERY解析XML - 使用条件语句

时间:2014-09-11 16:35:42

标签: xml tsql xquery

名为AdvantureWorks2012的Microsoft实践数据库有一个名为HumanResources.JobCandidate的表。该表有几列。其中一列是uri数据类型。这就是该列中一个单元格内的内容:

<ns:Resume xmlns:ns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume">
  <ns:Name>
    <ns:Name.Prefix></ns:Name.Prefix>
    <ns:Name.First>Shai</ns:Name.First>
    <ns:Name.Middle></ns:Name.Middle>
    <ns:Name.Last>Bassli</ns:Name.Last>
    <ns:Name.Suffix></ns:Name.Suffix>
  </ns:Name>
  <ns:Skills>
I am an experienced and versatile machinist who can operate a range of machinery personally as well as supervise the work of other machinists. I specialize in diagnostics and precision inspection, have expertise in reading blueprints, and am able to call on strong interpersonal and communication skills to guide the work of other production machinists whose work I am called upon to inspect. 
My degree in mechanical engineering affords me a better theoretical understanding and mathematical background than many other candidates in the machinist trade.
    </ns:Skills>
  <ns:Employment>
    <ns:Emp.StartDate>2000-06-01Z</ns:Emp.StartDate>
    <ns:Emp.EndDate>2002-09-30Z</ns:Emp.EndDate>
    <ns:Emp.OrgName>Wingtip Toys</ns:Emp.OrgName>
    <ns:Emp.JobTitle>Lead Machinist</ns:Emp.JobTitle>
    <ns:Emp.Responsibility> Supervised work of staff of four machinists. Coordinated all complex assembly and tooling activities, including production of tricycles and wagons.
Developed parts fabrication from sample parts, drawings and verbal orders.Worked with ISO9000 implementation.
        </ns:Emp.Responsibility>
    <ns:Emp.FunctionCategory>Production</ns:Emp.FunctionCategory>
    <ns:Emp.IndustryCategory>Manufacturing</ns:Emp.IndustryCategory>
    <ns:Emp.Location>
      <ns:Location>
        <ns:Loc.CountryRegion>US </ns:Loc.CountryRegion>
        <ns:Loc.State>MI </ns:Loc.State>
        <ns:Loc.City>Saginaw</ns:Loc.City>
      </ns:Location>
    </ns:Emp.Location>
  </ns:Employment>
  <ns:Employment>
    <ns:Emp.StartDate>1996-11-15Z</ns:Emp.StartDate>
    <ns:Emp.EndDate>2000-05-01Z</ns:Emp.EndDate>
    <ns:Emp.OrgName>Blue Yonder Airlines</ns:Emp.OrgName>
    <ns:Emp.JobTitle>Machinist</ns:Emp.JobTitle>
    <ns:Emp.Responsibility>Repaired and maintained a variety of production and fabrication machine tools.
Set up and operated machines to close tolerances. Used and wrote CNC machine programs. Trained extensively in computer-aided manufacturing.
        </ns:Emp.Responsibility>
    <ns:Emp.FunctionCategory>Production</ns:Emp.FunctionCategory>
    <ns:Emp.IndustryCategory>Manufacturing</ns:Emp.IndustryCategory>
    <ns:Emp.Location>
      <ns:Location>
        <ns:Loc.CountryRegion>US </ns:Loc.CountryRegion>
        <ns:Loc.State>IL </ns:Loc.State>
        <ns:Loc.City>Chicago</ns:Loc.City>
      </ns:Location>
    </ns:Emp.Location>
  </ns:Employment>
  <ns:Employment>
    <ns:Emp.StartDate>1994-06-10Z</ns:Emp.StartDate>
    <ns:Emp.EndDate>1996-07-22Z</ns:Emp.EndDate>
    <ns:Emp.OrgName>City Power and Light</ns:Emp.OrgName>
    <ns:Emp.JobTitle>Assistant Machinist</ns:Emp.JobTitle>
    <ns:Emp.Responsibility>Performed centerless grinding. Received training in manual mill and lathe machines, as well as micrometers and calipers.
Owned complete toolset.Worked extensive overtime on request. </ns:Emp.Responsibility>
    <ns:Emp.FunctionCategory>Production</ns:Emp.FunctionCategory>
    <ns:Emp.IndustryCategory>Manufacturing</ns:Emp.IndustryCategory>
    <ns:Emp.Location>
      <ns:Location>
        <ns:Loc.CountryRegion>US </ns:Loc.CountryRegion>
        <ns:Loc.State>IA </ns:Loc.State>
        <ns:Loc.City>Des Moines</ns:Loc.City>
      </ns:Location>
    </ns:Emp.Location>
  </ns:Employment>
  <ns:Education>
    <ns:Edu.Level>Bachelor</ns:Edu.Level>
    <ns:Edu.StartDate>1990-09-15Z</ns:Edu.StartDate>
    <ns:Edu.EndDate>1994-05-10Z</ns:Edu.EndDate>
    <ns:Edu.Degree>Bachelor of Science</ns:Edu.Degree>
    <ns:Edu.Major>Mechanical Engineering</ns:Edu.Major>
    <ns:Edu.Minor></ns:Edu.Minor>
    <ns:Edu.GPA>3.2</ns:Edu.GPA>
    <ns:Edu.GPAScale>4</ns:Edu.GPAScale>
    <ns:Edu.School>Midwest State University</ns:Edu.School>
    <ns:Edu.Location>
      <ns:Location>
        <ns:Loc.CountryRegion>US </ns:Loc.CountryRegion>
        <ns:Loc.State>IA </ns:Loc.State>
        <ns:Loc.City>Ames</ns:Loc.City>
      </ns:Location>
    </ns:Edu.Location>
  </ns:Education>
  <ns:Address>
    <ns:Addr.Type>Home</ns:Addr.Type>
    <ns:Addr.Street>567 3rd Ave</ns:Addr.Street>
    <ns:Addr.Location>
      <ns:Location>
        <ns:Loc.CountryRegion>US </ns:Loc.CountryRegion>
        <ns:Loc.State>MI </ns:Loc.State>
        <ns:Loc.City>Saginaw</ns:Loc.City>
      </ns:Location>
    </ns:Addr.Location>
    <ns:Addr.PostalCode>53900</ns:Addr.PostalCode>
    <ns:Addr.Telephone>
      <ns:Telephone>
        <ns:Tel.Type>Voice</ns:Tel.Type>
        <ns:Tel.IntlCode>1</ns:Tel.IntlCode>
        <ns:Tel.AreaCode>276</ns:Tel.AreaCode>
        <ns:Tel.Number>555-0114</ns:Tel.Number>
      </ns:Telephone>
      <ns:Telephone>
        <ns:Tel.Type>Fax</ns:Tel.Type>
        <ns:Tel.IntlCode>1</ns:Tel.IntlCode>
        <ns:Tel.AreaCode>276</ns:Tel.AreaCode>
        <ns:Tel.Number>555-0132</ns:Tel.Number>
      </ns:Telephone>
    </ns:Addr.Telephone>
  </ns:Address>
  <ns:EMail>Shai@Example.com</ns:EMail>
  <ns:WebSite></ns:WebSite>
</ns:Resume>

我想使用tsql提取芝加哥的那些行(城市的路径是/Resume/Employment/Emp.Location/Location/Loc.City)。我只是一个初学者,所以精心解释会有很长的路要走。感谢。

1 个答案:

答案 0 :(得分:1)

要检查XML中是否存在节点,您应该使用exist() Method (xml Data Type)exist()的参数是一个XQuery,它指定您希望存在于XML中的节点,如果存在至少一个节点则返回1,如果没有匹配的节点则返回0

要检查XML中的值,请在括号[]中编写谓词。

这将为您提供所有位置节点/Resume/Employment/Emp.Location/Location,并且只获取Loc.City为Chicago的节点,您添加谓词[Loc.City = "Chicago"]。然后XQuery变为/Resume/Employment/Emp.Location/Location[Loc.City = "Chicago"]

此查询将为您提供曾在芝加哥就业的所有候选人。

with xmlnamespaces(default 'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume')
select *
from HumanResources.JobCandidate as C
where C.Resume.exist('/Resume/Employment/Emp.Location/Location[Loc.City = "Chicago"]') = 1