【问题标题】:Writing an XSD Schema to put XML data into SQL tables编写 XSD 架构以将 XML 数据放入 SQL 表中
【发布时间】:2010-11-18 18:02:41
【问题描述】:

我希望有人可以帮助我。我对此很陌生。我想知道是否有可能获得一个 XSD 架构来将 XML 数据转储到 多个 SQL 表中(使用 sql:relation attribute 等)。

一个表证明没有问题,所以我只是想知道是否可以将数据一分为二转储。能够使用一个 XSD Schema 做到这一点会很好,但是我是否必须对第二个表的 XML 进行两次传递?

感谢您的帮助。


这是架构本身:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<!-- Skimlinks/Everyfeed schema --> 

<!-- definition of simple elements --> 
<xs:element name="title" type="xs:string" sql:field="ProductName"/> 
<xs:element name="url" type="xs:string" sql:field="ProductURL"/> 
<xs:element name="image_url" type="xs:string" sql:field="ImageURL"/> 
<xs:element name="currency" type="xs:string" sql:field="currency"/>
<xs:element name="price" type="xs:string" sql:field="Price"/>
<xs:element name="merchant" type="xs:string" sql:field="Brand" default=" " /> 
<xs:element name="description" type="xs:string" sql:field="Description" default=" "/>
<xs:element name="item" type="xs:string" sql:field="Category" />

<!-- definition of attributes -->
<xs:attribute name="id" type="xs:string" sql:field="SKU" /> 

<!-- definition of complex elements -->
<xs:element name="category" sql:relation="ProductDataCategory">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="item" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="product" sql:relation="ProductData">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="title"/>
      <xs:element ref="url"/>
      <xs:element ref="image_url"/>
      <xs:element ref="currency"/>
      <xs:element ref="price"/>
      <xs:element ref="merchant"/>
      <xs:element ref="description"/>
      <xs:element ref="category"/>
    </xs:sequence>
    <xs:attribute ref="id" use="required"/>
  </xs:complexType>
</xs:element>

<xs:element name="products" sql:is-constant="1">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="product" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="everyFeed" sql:is-constant="1">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="products" maxOccurs="1" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

这是我们正在导入的 XML 的示例:

<?xml version='1.0' encoding='utf-8'?>
<feed version="2">
<numFound>7985</numFound>
<products>
    <product id="18639|216623247">
      <title>Trouser</title>
      <url>http://www.products.com/trousers/trouser/</url>
      <image_url>http://www.images.co.uk/images/big/4d624426.jpg</image_url>
      <verified_image>True</verified_image>
      <currency>GBP</currency>
      <price>1000</price>
      <prev_price>1000</prev_price>
      <firstseen>2010-10-27T00:00:00Z</firstseen>
      <lastseen>2010-10-27T00:00:00Z</lastseen>
      <merchant id="20748">Yours Clothing</merchant>
      <by>Yours Clothing</by>
      <description></description>
      <category>
        <item id="9">Lounge &amp; nightwear</item>
        <item id="3">Women</item>
        <item id="2">Clothing</item>
        <item id="1">Clothing, shoes &amp; accessories</item>
      </category>
    </product>
</products>
</feed>

如您所见,它尝试转储到两个表中:ProductDataProductDataCategory。只有存储在&lt;item&gt; 元素中的内容才应该放在后一个表中(在字段类别中)。

错误信息内容如下:

错误:“类别”上应有关系

我不知道为什么:(

感谢您为这项工作提供的任何帮助!

【问题讨论】:

  • @Django - 我已重新标记,因为这是 Microsoft 特定于 XML 架构的扩展
  • 你会想要提供更多关于你想要做什么的细节。您如何使用此架构将数据批量加载到单个表中?
  • 嗨,约翰,这里有更多详细信息:stackoverflow.com/questions/3373104/…
  • @John,我还在原始问题中添加了更多细节。
  • 您能展示一下 ProductData 和 ProductDataCategories 表的样子吗?只需钥匙就足够了。我怀疑您没有在两者之间定义外键约束。

标签: sql-server xml xsd sqlxml


【解决方案1】:

如果您尝试进行批量导入,您可能需要在 XSD 文件中定义 SQL 关系。我遇到了类似的问题(这只是一个示例,并非特定于您的 XSD):

  <xs:annotation>
    <xs:appinfo>
      <sql:relationship name="Detail"
                        parent="Product"
                        parent-key="ProductID"
                        child="Details"
                        child-key="ProductID" 
                        />
      <sql:relationship name="ProductFiles"
                       parent="Product"
                       parent-key="ProductID"
                       child="Files"
                       child-key="ProductID" 
                        />
      <sql:relationship name="ProductToList"
                        parent="Product"
                        parent-key="ProductID"
                        child="ProductList"
                        child-key="ProductID"
                        />
      <sql:relationship name="ListToProduct"
                        parent="ProductList"
                        parent-key="ID"
                        child="ProductListProduct"
                        child-key="ProductListID"
                        />
    </xs:appinfo>
  </xs:annotation>

【讨论】:

  • 感谢您的回答。我的 XSD 模式文件已经定义了 SQL 关系......虽然显然不够好。您是否有任何进一步的具体信息如何解决?
  • 啊,我在您发布的文档中没有看到它。不确定还有什么可能导致它作为我在定义关系后纠正的问题。 GL!
【解决方案2】:

姜戈,

为什么不尝试像 DBVis - http://www.dbvis.com/ 这样的东西来创建数据库中所有关系的可视化表示,并努力使 xsd 与生成的图表相匹配。这将确保您获得所有这些关系。

请参阅此处以获取一个非常简单的数据库的示例截图 - http://www.dbvis.com/products/dbvis/doc/main/doc/ug/databaseExplorer/images/genericschemaview.png

我可以保证它也适用于更大的数据库。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多