Udostępnij za pośrednictwem


Ukrywanie elementów i atrybutów poprzez używanie sql:hide

Kiedy wykonywana jest kwerenda XPath schematu XSD, wynikowy dokument XML zawiera elementy i atrybuty, które są określone w schemacie.Można określić, że niektóre elementy i atrybuty ukryte w schemacie przy użyciu sql:hide adnotacji.Jest to przydatne, gdy kryteria kwerendy wymagają szczególnych elementów lub atrybutów w schemacie, ale nie chcesz, aby je zwrócone w dokumencie XML, który jest generowany.

sql:hide Adnotacji przyjmuje wartość logiczna (0 = false, 1 = true).Dopuszczalne wartości to 0, 1, true i false.

Przykłady

Aby utworzyć próbki pracy przy użyciu poniższych przykładach, musi spełniać określone wymagania.Aby uzyskać więcej informacji, zobacz Wymagania dotyczące uruchamianie przykładów SQLXML.

A.Określanie atrybut sql:hide

Schematu XSD w tym przykładzie składa się z <Person.Person> element z BusinessEntityID, Imię, i nazwisko atrybuty.

<Person.Person> element jest typu złożonego i dlatego mapuje tabela o tej samej nazwie (Mapowanie domyślne).All the attributes of <Person.Person> element are of simple type and map to columns with the same names in the Person.Persontable in the AdventureWorks2008R2 database.W schemacie sql:hide adnotacji jest określona na BusinessEntityID atrybut.Po określeniu kwerendę XPath schematem, BusinessEntityID nie jest zwracana w dokumencie XML.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xsd:element name="Person.Person" >
     <xsd:complexType>
       <xsd:attribute name="BusinessEntityID"  sql:hide="true" /> 
       <xsd:attribute name="FirstName"   type="xsd:string" /> 
       <xsd:attribute name="LastName"    type="xsd:string" /> 
     </xsd:complexType>
  </xsd:element>
</xsd:schema>

Aby przetestować przykładowe kwerendy XPath względem schematu

  1. Kod schematu skopiować i wkleić go do pliku tekstowego.Zapisz plik jako Hide.xml.

  2. Skopiuj następujący szablon i wkleić go do pliku tekstowego.Zapisz plik jako HideT.xml w tym samym katalogu, w którym zapisano Hide.xml.

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
        <sql:xpath-query mapping-schema="Hide.xml">
            /Person.Person[@BusinessEntityID="1"]
        </sql:xpath-query>
    </ROOT>
    

    Ścieżka katalogu określonego dla mapowania schematu (Hide.xml) jest katalog, w którym zapisany szablon.Ścieżka bezwzględna również można określić, na przykład:

    mapping-schema="C:\MyDir\Hide.xml"
    
  3. Tworzenie i używanie szablon wykonać skryptu testu 4.0 SQLXML (Sqlxml4test.vbs).

    Aby uzyskać więcej informacji, zobacz Przy użyciu ADO SQLXML wykonanie kwerendy 4.0.

Zestaw wyników jest następujący:

 <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
    <Person.Person FirstName="Gustavo" LastName="Achong" /> 
 </ROOT>

Gdy sql:hide określony dla elementu, element i jego atrybutów lub elementów podrzędność nie pojawiają się w dokumencie XML, który jest generowany.Oto innego schematu XSD, w którym sql:hide jest określona na <OD> element:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:dt="urn:schemas-microsoft-com:datatypes"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">

  <xsd:annotation>
    <xsd:documentation>
      Sales.Customer-Sales.SalesOrderHeader-Sales.SalesOrderDetail Schema
      Copyright 2004 Microsoft. All rights reserved.
    </xsd:documentation>
    <xsd:appinfo>
      <sql:relationship name="CustomerOrder"
         parent="Sales.Customer"
                  parent-key="CustomerID"
                  child-key="CustomerID"
                  child="Sales.SalesOrderHeader" />
       <sql:relationship name="OrderOrderDetails"
                  parent="Sales.SalesOrderHeader"
                  parent-key="SalesOrderID"
                  child-key="SalesOrderID"
                  child="Sales.SalesOrderDetail"/>
    </xsd:appinfo>
  </xsd:annotation>

  <xsd:element name="Customers" sql:relation="Sales.Customer">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Order" sql:relation="Sales.SalesOrderHeader" 
                     maxOccurs="unbounded" 
                     sql:relationship="CustomerOrder">
          <xsd:complexType>
            <xsd:sequence>
               <xsd:element name="OD" sql:relation="Sales.SalesOrderDetail" 
                                      maxOccurs="unbounded" 
                                      sql:relationship="OrderOrderDetails" 
                                      sql:hide="1">
                  <xsd:complexType>
                    <xsd:attribute name="SalesOrderID" type="xsd:string"/>
                    <xsd:attribute name="ProductID" type="xsd:string"/>
                  </xsd:complexType>
               </xsd:element>
            </xsd:sequence>
          <xsd:attribute name="CustomerID" type="xsd:string"/>
          <xsd:attribute name="OID" sql:field="SalesOrderID" 
                                    type="xsd:string"/>
          <xsd:attribute name="OrderDate" type="xsd:date"/> 
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="CID" sql:field="CustomerID" 
                                type="xsd:string"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Gdy kwerendę XPath (na przykład /Customers[@CID="1"]) określono schematem, dokumentu XML, który jest generowany nie obejmuje <OD> element i dzieci, jak pokazano na wynik częściowy:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <Customers CID="1">
    <Order CustomerID="1" OID="43860" OrderDate="2005-08-01" /> 
    <Order CustomerID="1" OID="44501" OrderDate="2005-11-01" /> 
    <Order CustomerID="1" OID="45283" OrderDate="2006-02-01" /> 
    <Order CustomerID="1" OID="46042" OrderDate="2006-05-01" /> 
  </Customers>
</ROOT>