Udostępnij przez


Example: Specifying the ELEMENT Directive and Entity Encoding

Ten przykład przedstawia różnicę między ELEMENT and XML dyrektyw.The ELEMENT directive entitizes the data, but the XML directive does not.The <Summary> element is assigned XML, <Summary>This is summary description</Summary>, in the query.

Należy wziąć pod uwagę tę kwerendę:

SELECT  1 as Tag,
        0 as Parent,
        ProductModelID  as [ProductModel!1!ProdModelID],
        Name            as [ProductModel!1!Name],
        NULL            as [Summary!2!SummaryDescription!ELEMENT]
FROM    Production.ProductModel
WHERE   ProductModelID=19
UNION ALL
SELECT  2 as Tag,
        1 as Parent,
        ProductModelID,
        NULL,
       '<Summary>This is summary description</Summary>'
FROM   Production.ProductModel
WHERE  ProductModelID=19
FOR XML EXPLICIT

Jest to wynik.Skrócony opis jest entitized w wyniku.

<ProductModel ProdModelID="19" Name="Mountain-100">
  <Summary>
    <SummaryDescription>&lt;Summary&gt;This is summary description&lt;/Summary&gt;</SummaryDescription>
  </Summary>
</ProductModel>

Teraz, jeżeli określono XML dyrektywa w polu Nazwa kolumna Summary!2!SummaryDescription!XML, a nie ELEMENT dyrektywę, wyświetlony zostanie skrócony opis bez entitization.

<ProductModel ProdModelID="19" Name="Mountain-100">
  <Summary>
    <SummaryDescription>
      <Summary>This is summary description</Summary>
    </SummaryDescription>
  </Summary>
</ProductModel>

Zamiast przypisywania statycznych wartość XML, następujące kwerendy zastosowań Query() Metoda XML typu, aby pobrać wartość z kolumna CatalogDescription skrócony opis modelu produktuXML typu.Ponieważ wiadomo o się wynik XML typu entitization nie jest stosowana.

SELECT  1 as Tag,
        0 as Parent,
        ProductModelID  as [ProductModel!1!ProdModelID],
        Name            as [ProductModel!1!Name],
        NULL            as [Summary!2!SummaryDescription]
FROM    Production.ProductModel
WHERE   CatalogDescription is not null
UNION ALL
SELECT  2 as Tag,
        1 as Parent,
        ProductModelID,
        Name,
       (SELECT CatalogDescription.query('
            declare namespace pd="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
          /pd:ProductDescription/pd:Summary'))
FROM     Production.ProductModel
WHERE    CatalogDescription is not null
ORDER BY [ProductModel!1!ProdModelID],Tag
FOR XML EXPLICIT

See Also

Reference