编写 XSD 架构以将 XML 数据放入 SQL 表中
我希望有人能帮助我。我对此很陌生。我想知道是否可以使用 XSD 架构将 XML 数据转储到多个 SQL 表中(使用 sql:relation 属性
等)。
一张表证明没有问题,所以我只是想知道是否可以将数据转储为两张表。如果能够使用一个 XSD 架构来完成此操作,那就太好了,但是我是否必须对第二个表的 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 & nightwear</item>
<item id="3">Women</item>
<item id="2">Clothing</item>
<item id="1">Clothing, shoes & accessories</item>
</category>
</product>
</products>
</feed>
如您所见,它尝试转储到两个表中:ProductData 和 ProductDataCategory。只有存储在
元素中的内容才应放入后一个表中(在字段类别中)。
错误消息内容如下:
错误:“类别”上预期存在关系
我不知道为什么:(
感谢您为实现此工作提供的任何帮助!
I hope someone can help me. I'm very new at this. I was wondering if it's possible to get an XSD Schema to dump XML data into multiple SQL tables (using the sql:relation attribute
, etc.).
One table is proving no problems, so I just wondered if it's possible to dump data in two. It'd be nice to be able to do this with one XSD Schema, but do I have to make two passes on the XML for the second table?
Thanks for any assistance.
Here's the schema itself:
<?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>
Here's an example of the XML we're importing:
<?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 & nightwear</item>
<item id="3">Women</item>
<item id="2">Clothing</item>
<item id="1">Clothing, shoes & accessories</item>
</category>
</product>
</products>
</feed>
As you can see, it attempts to dump into two tables: ProductData and ProductDataCategory. Only the stuff stored in the <item>
elements should go in the latter table (in the field category).
The error message reads:
Error: Relationship expected on 'category'
I don't know why :(
Thanks for any assistance in getting this working!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试执行批量导入,则可能需要在 XSD 文件中定义 SQL 关系。我遇到了类似的问题(这只是一个示例,并非特定于您的 XSD):
You may need to define a SQL relationship in the XSD file if you are trying to do a bulk import. I ran into a similar problem (this is just an example, not specific to your XSD):
Django,
为什么不尝试像 DBVis 这样的东西 - http://www.dbvis.com/ - 来创建视觉效果数据库中所有关系的表示,并努力使 xsd 与生成的图表相匹配。这将确保您获得所有这些关系。
请参阅此处,获取非常简单的数据库的示例屏幕截图 - http://www.dbvis.com/products/dbvis/doc/main/doc/ug/databaseExplorer/images/genericschemaview.png
我可以保证它也适用于更大的数据库。
希望这会有所帮助。
Django,
Why not try something like DBVis - http://www.dbvis.com/ - to create a visual representation of all the relationships in your database and work towards having the xsd match the generated diagram. This'll ensure you get all of these relationships.
See here for an example screeshot for a very simple database - http://www.dbvis.com/products/dbvis/doc/main/doc/ug/databaseExplorer/images/genericschemaview.png
I can vouch for it working well for much larger databases too.
Hopefully this'll help.