SQLXML批量将属性加载到单个表中

时间:2015-09-08 19:09:47

标签: xml vbscript sql-server-2008-r2 xml-parsing sqlxml

TLDR;

我正在尝试使用SQLXML Bulk Loader(4.0)来加载看起来像这样的XML;

<?xml version = "1.0" encoding = "UTF-8"?>
<CarSales>
 <Client>
  <ID >3</ID>
  <ClientName>John Smith3</ClientName>
  <Country name="Colombia"/>
 </Client>
 <Client>
  <ID>7</ID>
  <ClientName>Slow Sid</ClientName>
  <Country name="Bolivia"/>
 </Client>
 <Client>
  <ID>10</ID>
  <ClientName>Missing Town</ClientName>
  <Country name="Argentina"/>
 </Client>
</CarSales>

我希望在我正在使用的唯一表的列中捕获国家/地区名称属性(“Client_XMLBulkLoad”,它还包含客户端名称和ID)。这可能吗?

更多详情

这是我到目前为止对应的XSD(“Country”已注释掉)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql = "urn:schemas-microsoft-com:mapping-schema">
 <xsd:element name = "CarSales" sql:is-constant = "1" >
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name = "Client" sql:relation="Client_XMLBulkLoad"
          maxOccurs = "unbounded">
      <xsd:complexType>
       <xsd:sequence>
        <xsd:element name = "ID" type = "xsd:integer" 
                     sql:field = "ID" />
        <xsd:element name = "ClientName" type = "xsd:string"
                     sql:field = "ClientName" />
        <!-- <xsd:element name="Country" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:attribute name="name" type="xsd:string" sql:field = "Country" />
                </xsd:complexType> 
        </xsd:element> -->
       </xsd:sequence>
      </xsd:complexType>
     </xsd:element>
    </xsd:sequence>
   </xsd:complexType>
  </xsd:element>
</xsd:schema>

如果我尝试使用Country as-is进行批量加载,则会收到一条错误消息,指出“Country”上存在架构关系。但是,在我正在阅读的所有内容中,关系是在不同的表之间建立的,而不是与所有其他数据在同一个表上导入属性。

或者我尝试使用“is-constant =”1“”标记标记“Country”元素,但这会导致错误,指出“常量/固定元素不能具有属性”。

那么当它与客户名称和ID属于同一个表时,如何捕获国家/地区名称?

这是我正在使用的实际架构(它是嵌套架构)

架构根:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" targetNamespace="http://scap.nist.gov/schema/feed/vulnerability/2.0" xmlns:mstns="http://scap.nist.gov/schema/feed/vulnerability/2.0" xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:app1="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xs:import namespace="http://scap.nist.gov/schema/vulnerability/0.4" schemaLocation="DELTE_app1.xsd" />
  <xs:element name="nvd" sql:is-constant = "1">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="entry" minOccurs="0" maxOccurs="unbounded" sql:relation="NVD.CVE">
          <xs:complexType>
            <xs:sequence>
              <xs:element ref="app1:cwe" minOccurs="0"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

架构孩子:

<?xml version="1.0" standalone="yes"?>
<xs:schema targetNamespace="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:mstns="http://scap.nist.gov/schema/feed/vulnerability/2.0" xmlns="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:app2="http://cpe.mitre.org/language/2.0" xmlns:app3="http://scap.nist.gov/schema/cvss-v2/0.2" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xs:element name="cwe" msdata:Prefix="vuln" sql:field="CWE">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="id" form="unqualified" type="xs:string" sql:field="CWE"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

我正在加载的XML文件:

<?xml version='1.0' encoding='UTF-8'?>
<nvd xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.1" xmlns:cvss="http://scap.nist.gov/schema/cvss-v2/0.2" xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:patch="http://scap.nist.gov/schema/patch/0.1" xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0" xmlns:cpe-lang="http://cpe.mitre.org/language/2.0" nvd_xml_version="2.0" pub_date="2015-07-10T03:00:00" xsi:schemaLocation="http://scap.nist.gov/schema/patch/0.1 http://nvd.nist.gov/schema/patch_0.1.xsd http://scap.nist.gov/schema/feed/vulnerability/2.0 http://nvd.nist.gov/schema/nvd-cve-feed_2.0.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd">
<entry id="CVE-2015-0016">
    <vuln:cwe id="CWE-22"/>
  </entry>
</nvd>

注意这是可行的但是我需要在元素级别和属性级别都有[sql:field =“CWE”]。如果我不添加额外的“sql:field”,我会收到一条错误,说“列'cwe'已在架构中定义,但在数据库中不存在”。如果我在该元素上添加'is constant'标签,那么我得到的常量元素不能包含属性错误。所以我找到的唯一解决方案是冗余地为它分配sql:field =“CWE”。

1 个答案:

答案 0 :(得分:4)

通过寻址sql字段,您需要具有简单内容的复杂类型定义。
请查看<xsd:simpleContent> Element<xsd:extension> Element (complexContent) 我没有测试,但以下架构应该工作 更新:我刚刚测试过,它按预期工作。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xsd:element name="CarSales" sql:is-constant="1">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Client" sql:relation="Client_XMLBulkLoad" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="ID" type="xsd:integer" sql:field="ID"/>
              <xsd:element name="ClientName" type="xsd:string" sql:field="ClientName"/>
              <xsd:element name="Country" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:simpleContent>
                    <xsd:extension base="xsd:string">
                      <xsd:attribute name="name" type="xsd:string" sql:field="Country"/>
                    </xsd:extension>
                  </xsd:simpleContent>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>