Aracılığıyla paylaş


XQuery içindeki namespaces işleme

Bu konu ad alanlarında sorguları işleme örnekleri sağlar.

Örnekler

A.Bir isim alanı bildirimi

Belirli bir ürün modeline üretim adımları aşağıdaki sorgu alır.

SELECT Instructions.query('
     declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
        /AWMI:root/AWMI:Location[1]/AWMI:step
    ') as x
FROM Production.ProductModel
WHERE ProductModelID=7

SELECT Instructions.query('
     declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
        /AWMI:root/AWMI:Location[1]/AWMI:step
    ') as x
FROM Production.ProductModel
WHERE ProductModelID=7

Bu kısmi bir sonucudur:

<AWMI:step xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">Insert <AWMI:material>aluminum sheet MS-2341</AWMI:material> into the <AWMI:tool>T-85A framing tool</AWMI:tool>. </AWMI:step>
…

<AWMI:step xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">Insert <AWMI:material>aluminum sheet MS-2341</AWMI:material> into the <AWMI:tool>T-85A framing tool</AWMI:tool>. </AWMI:step>
…

Not ad anahtar yeni bir ad alanı önekini tanımlamak için kullanılan "AWMI:". Bu önek, o ad kapsamı içinde kalan tüm öğeler için sorguda kullanılmalıdır.

B.Varsayılan bir ad alanı bildirme

Önceki sorgu, yeni bir ad alanı öneki tanımlanmıştır. Bu önek, sonra istenen xml yapıları seçmek için sorguda kullanılacak vardı. Alternatif olarak, aşağıdaki değiştirilmiş sorgu gösterildiği gibi varsayılan bir ad alanı, ad alanı bildirebilirsiniz:

SELECT Instructions.query('
     declare default element namespace "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
        /root/Location[1]/step
    ') as x
FROM Production.ProductModel
where ProductModelID=7

SELECT Instructions.query('
     declare default element namespace "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
        /root/Location[1]/step
    ') as x
FROM Production.ProductModel
where ProductModelID=7

Sonuç budur

<step xmlns="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">Insert <material>aluminum sheet MS-2341</material> into the <tool>T-85A framing tool</tool>. </step>
…

<step xmlns="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">Insert <material>aluminum sheet MS-2341</material> into the <tool>T-85A framing tool</tool>. </step>
…

Not Bu örnekte tanımlanan ad, "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions", varsayılan, ya da boş, ad alanı geçersiz kılmak için yapılmış. Bu nedenle, artık bir ad alanı öneki kullanılan yol ifadesi yok sorgulanacak. Ayrıca artık sonuçlarda görüntülenen öğe adlarında ad alanı önekini yok. Ayrıca, varsayılan ad alanını tüm öğeleri, ancak onların öznitelikler uygulanır.

C.xml yapım ad alanlarını kullanma

Yeni ad tanımladığınızda, kapsamı sadece sorgu için ama inşaat halinde toplanırlar. Örneğin, xml oluştururken, yeni bir ad alanı kullanarak tanımlayabileceğiniz "declare namespace ...'' beyanı ve tüm öğeleri ve öznitelikleri sorgu sonuçları içinde görünmesini inşa ile o ad kullanın.

SELECT CatalogDescription.query('
     declare default element namespace "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
     declare namespace myNS="uri:SomeNamespace";
       <myNS:Result>
          { /ProductDescription/Summary }
       </myNS:Result>

    ') as Result
FROM Production.ProductModel
where ProductModelID=19

SELECT CatalogDescription.query('
     declare default element namespace "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
     declare namespace myNS="uri:SomeNamespace";
       <myNS:Result>
          { /ProductDescription/Summary }
       </myNS:Result>

    ') as Result
FROM Production.ProductModel
where ProductModelID=19

Sonuç şudur:

<myNS:Result xmlns:myNS="uri:SomeNamespace">
  <Summary xmlns="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">
   <p1:p xmlns:p1="http://www.w3.org/1999/xhtml">
     Our top-of-the-line competition mountain bike. Performance-enhancing 
     options include the innovative HL Frame, super-smooth front 
     suspension, and traction for all terrain.</p1:p>
  </Summary>
</myNS:Result>

<myNS:Result xmlns:myNS="uri:SomeNamespace">
  <Summary xmlns="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">
   <p1:p xmlns:p1="http://www.w3.org/1999/xhtml">
     Our top-of-the-line competition mountain bike. Performance-enhancing 
     options include the innovative HL Frame, super-smooth front 
     suspension, and traction for all terrain.</p1:p>
  </Summary>
</myNS:Result>

Alternatif olarak, Ayrıca açıkça xml yapım bir parçası olarak kullanıldığı her noktada ad alanı aşağıdaki sorgu gösterildiği gibi tanımlayabilirsiniz:

SELECT CatalogDescription.query('
     declare default element namespace "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
       <myNS:Result xmlns:myNS="uri:SomeNamespace">
          { /ProductDescription/Summary }
       </myNS:Result>
    ') as Result
FROM Production.ProductModel
where ProductModelID=19

SELECT CatalogDescription.query('
     declare default element namespace "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
       <myNS:Result xmlns:myNS="uri:SomeNamespace">
          { /ProductDescription/Summary }
       </myNS:Result>
    ') as Result
FROM Production.ProductModel
where ProductModelID=19

D.Varsayılan ad alanları kullanılarak inşaat

Varsayılan bir ad alanı kullanmak için yapılandırılmış xml içinde de tanımlayabilirsiniz. Örneğin, varsayılan bir ad alanı, "uri: SomeNamespace" nasıl belirleyebileceğiniz aşağıdaki sorgu gösterir \ oluşturulur, gibi yerel olarak adlandırılmış öğeleri için varsayılan olarak kullanmak için <Result>öğesi.

SELECT CatalogDescription.query('
      declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
      declare default element namespace "uri:SomeNamespace";
       <Result>
          { /PD:ProductDescription/PD:Summary }
       </Result>

    ') as Result
FROM Production.ProductModel
where ProductModelID=19

SELECT CatalogDescription.query('
      declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
      declare default element namespace "uri:SomeNamespace";
       <Result>
          { /PD:ProductDescription/PD:Summary }
       </Result>

    ') as Result
FROM Production.ProductModel
where ProductModelID=19

Sonuç şudur:

<Result xmlns="uri:SomeNamespace">
  <PD:Summary xmlns:PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">
   <p1:p xmlns:p1="http://www.w3.org/1999/xhtml">
         Our top-of-the-line competition mountain bike. Performance-
         enhancing options include the innovative HL Frame, super-smooth 
         front suspension, and traction for all terrain.</p1:p>
  </PD:Summary>
</Result>

<Result xmlns="uri:SomeNamespace">
  <PD:Summary xmlns:PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">
   <p1:p xmlns:p1="http://www.w3.org/1999/xhtml">
         Our top-of-the-line competition mountain bike. Performance-
         enhancing options include the innovative HL Frame, super-smooth 
         front suspension, and traction for all terrain.</p1:p>
  </PD:Summary>
</Result>

Varsayılan öğe ad alanı veya boş ad alanı geçersiz kılarak, yapılandırılmış xml tüm yerel adlandırılmış öğeleri daha sonra baskın varsayılan ad alanına bağlı olduğunu unutmayın. Bu nedenle, boş ad yararlanmak için xml oluşturma esnekliği gerektiriyorsa, varsayılan öğe ad alanı geçersiz kılmaz.

Ayrıca bkz.

Kavramlar

İle sorguları WITH xmlnamespaces NameSpaces eklemek

DEL - Implementing XML in SQL Server

Diğer Kaynaklar

XQuery dil başvurusu (SQL Server)