gpt4 book ai didi

sql - 将映射的 XPath 表达式转换为包含关系数据的 XML

转载 作者:数据小太阳 更新时间:2023-10-29 02:09:08 24 4
gpt4 key购买 nike

关闭。这个问题是 off-topic 。它目前不接受答案。












想改善这个问题吗? Update the question 所以它是堆栈溢出的 on-topic

8年前关闭。




Improve this question




背景

使用 XML 和 XSL 创建网站。 PostgreSQL 数据库的结构是为了表示必需的 XML 文档。 PostgreSQL 提供了许多与 XML 相关的函数,列在:

http://www.postgresql.org/docs/current/static/functions-xml.html

这是一个不同于从 XML 到数据库的问题,其中有很多解决方案。

更新: 澄清一下, 这个问题不需要使用给定 URL 中列出的 XML 函数 。它涉及基于将数据库表(和 JOIN 条件)映射到 XPath 表达式来创建 XML 文档的通用解决方案。

更新: 进一步说明,XQuery 用于搜索 XML 文档,就好像它们是一个数据库一样——就像 XPath 和 SQL 之间的交叉。我有一个数据库,我想生成一个 XML 文档。 XML 文档的结构应该根据映射到表和列的 XPath 表达式给出,而不是根据 XML 函数。

问题

我想解决的问题是使用 PostgreSQL 将表行中的值映射到 XPath 值。下面的例子用来说明这个问题。

例子

这个问题所涉及的 XPath 映射类似于:

root               > people
person > person
person.first_name -> name/first
person.last_name -> name/last
person.age -> [@age]
account.person_id => person.person_id
account > person/account
account.number -> [@id]
PERSON 表可能类似于:
person_id | first_name | last_name | age
123 | Peter | Parker | 18
456 | James | Jameson | 42
ACCOUNT 表可能类似于:
account_id | person_id | number
1 | 123 | 123456789

使用 XPath 映射查询数据库将生成以下 XML 文档:
<people>
<person age="18">
<name>
<first>Peter</first>
<last>Parker</last>
</name>
<account id="123456789" />
</person>
<person age="42">
<name>
<first>James</first>
<last>Jameson</last>
</name>
</person>
</people>

在这种情况下,James Jameson 没有帐户,因此最终文档中不包含相应的 XML 元素 ( account)。

这是一个难题,不需要完整的解决方案。处理映射到简单 XML 元素和属性的 80% 的简单表的解决方案就足够了。



您将如何创建基于数据库表(和一些 JOIN 操作)到 XPath(或类似操作)的映射返回 XML 文档的 SQL 语句(或过程)以执行数据转换?

是否有任何技术或开源实现已经执行此类任务?

谢谢!

相关链接

文章和白皮书
  • http://www.ibm.com/developerworks/library/x-query2xml/
  • http://www.thinkmind.org/download.php?articleid=dbkda_2011_6_20_30152
  • http://www.informatica.si/PDF/33-3/06_Naser%20-%20Two-Way%20Mapping%20between%20Object-Oriented%20Databases%20and%20XML.pdf
  • http://www.stylusstudio.com/sqlxml_tutorial.html

  • 商业软件
  • http://www.stylusstudio.com/db_to_xml_mapper.html
  • http://www.altova.com/mapforce.html
  • 最佳答案

    它应该使用一个xml生成函数

    postgres=# select xmlelement(name "people", xmlagg(xmlelement(name "person", xmlattributes(age), xmlforest(first_name as first, last_name as last)))) from person;
    xmlelement
    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    <people><person age="18"><first>Peter</first><last>Parker</last></person><person age="42"><first>James</first><last>Jameson</last></person></people>

    (1 行)

    还有其他可能的用途
    postgres=# select table_to_xml('person', true, false, '');
    table_to_xml
    ────────────────────────────────────────────────────────────────
    <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">↵

    <row> ↵
    <first_name>Peter</first_name> ↵
    <last_name>Parker</last_name> ↵
    <age>18</age> ↵
    </row> ↵

    <row> ↵
    <first_name>James</first_name> ↵
    <last_name>Jameson</last_name> ↵
    <age>42</age> ↵
    </row> ↵

    </person> ↵

    (1 row)

    下一步应该是一些 xslt 转换到您的目标格式 - 这里将有更好的 xslt 专家,应该会有所帮助

    关于sql - 将映射的 XPath 表达式转换为包含关系数据的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8641602/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com