2010-11-18 46 views
0

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

一个表没有问题,所以我只是想知道是否有可能将数据转储为两个。能够用一个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> 

正如你所看到的,它试图转储到两个表: ProductData and ProductDataCategory。只有存储在<item>元素中的内容应该放在后面的表中(在字段类别中)。

错误消息写着:

Error: Relationship expected on 'category'

我不知道为什么:(

感谢您得到这个工作的任何援助

+1

@Django - 我已经被重新绑定,因为这是对XML模式的特定于Microsoft的扩展 – Anon 2010-11-18 18:22:18

+0

您需要提供有关您要执行的操作的更多详细信息。你如何使用这种模式将数据批量加载到单个表中? – 2010-11-18 20:08:26

+0

嗨,约翰,这里有更详细的信息:http://stackoverflow.com/questions/3373104/problem-during-sql-bulk-load – 2010-11-19 09:16:26

回答

1

您可能需要定义一个SQL!关系在XSD文件中,如果您尝试进行批量导入,我遇到了类似的问题(这仅仅是一个示例,并非针对您的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> 
+0

感谢您的回答。我的XSD模式文件已经定义了SQL关系...虽然显然不够好。你有没有进一步的具体信息如何解决? – 2010-11-29 18:32:48

+0

啊,我没有在你发布的文档中看到它。不知道还有什么可能导致它作为我在定义关系后纠正的问题。 GL! – 2010-11-29 20:00:40

1

的Django,

为什么不尝试像DBVis - http://www.dbvis.com/ - 创建数据库中的所有关系的可视化表示,对具有所产生的图中的XSD匹配工作。这将确保您获得所有这些关系。

在这里看到一个例子screeshot一个非常简单的数据库 - http://www.dbvis.com/products/dbvis/doc/main/doc/ug/databaseExplorer/images/genericschemaview.png

我可以保证它的工作以及对更大的数据库了。

希望这会有所帮助。