gpt4 book ai didi

使用 XQuery 将 XML 文档转换为 HTML 文档 - 需要帮助替换元素标签

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

我有一个 XML 文档,我想将其转换为 HTML。为此,我将 Xquery 与 Oxygen 解析器结合使用。

这是 xml:

<?xml version="1.0" encoding="UTF-8"?>
<?oxygen RNGSchema="file:textbook.rnc" type="compact"?>
<books xmlns="books">

<book ISBN="i0321165810" publishername="OReilly">
<title>XPath</title>
<author>
<name>
<fname>Priscilla</fname>
<lname>Warnley</lname>
</name>
<address>
<street_address>Hill Park<street_address>
<zip>90210</zip>
<state>california</state>
</address>
<phone>00000000</phone>
<e-mail>priscilla@oreilly.com</e-mail>
</author>
<year>2007</year>
<field>Databases</field>
<TOC>
<component>
<type>Part</type>
<title>Xpath</title>
<component>
<title>Chapter... A tour of xquery</title>
<pages>3</pages>
<component>
<title>Introductions</title>
</component>
<component>
<title>Getting started</title>
</component>
</component>
</component>
</TOC>
</book>

<publisher publishername="OReilly">
<web-site>www.oreilly.com</web-site>
<address>
<street_address>hill park</street_address>
<zip>90210</zip>
<state>california</state>
</address>
<phone>400400400</phone>
<e-mail>oreilly@oreilly.com</e-mail>
<contact>
<field>Databases</field>
<name>
<fname>Anna</fname>
<lname>Smith</lname>
</name>
</contact>
</publisher>
</books>

我首先执行此 Xquery 查询:

declare default element namespace "books";
<html>
<head>
<title>Table of contents</title>
</head>
<body>
<b>Table of contents</b>
<hr/>
{ for $i in //book[@ISBN='i0321165810']/TOC
return $i
}
</body>
</html>

基于我的 xml 文档得到这些结果:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="Books">
<head>
<title>Table of content</title>
</head>
<body>
<b>TOC</b>
<hr/>
<TOC>
<component>
<type>Part</type>
<title>Foundations</title>
<component>
<title>Chapter... A tour of xquery</title>
<pages>3</pages>
<component>
<title>Introductions</title>
</component>
<component>
<title>Getting started</title>
</component>
</component>
</component>
</TOC>
</body>
</html>

我现在想做的是用 pre 标签替换 component 标签(使用空格缩进),用斜体标签替换 title 标签,用粗体标签替换 pages 标签(基本上使用 HTML 标签而不是 XML 标签, 因此可以在 Web 浏览器中查看该文档)。我尝试使用替换功能,但无法正常工作。

有人可以帮忙吗?

最佳答案

正如 DevNull 所指出的,这是 XSLT 大放异彩的任务。这是我的 XQuery 解决方案:

declare default element namespace "http://www.w3.org/1999/xhtml";

declare function local:rename($node) {
let $old-name := local-name($node)
let $new-name :=
switch($old-name)
case 'component' return 'pre'
case 'title' return 'i'
case 'pages' return 'b'
default return $old-name
return element { $new-name } {
$node/@*,
for $nd in $node/child::node()
return if($nd instance of element())
then local:rename($nd)
else $nd
}
};

<html>
<head>
<title>Table of contents</title>
</head>
<body>
<b>Table of contents</b>
<hr/>
{ for $i in //*:book[@ISBN='i0321165810']/*:TOC/*
return local:rename($i)
}
</body>
</html>

函数 local:rename($node) 递归地进入 XML 片段,重建它并替换元素名称。它既不优雅也不高效,但它应该可以完成工作。

我还更改了 默认元素命名空间,因为您返回的 XHTML 文档位于 books 命名空间中。

关于使用 XQuery 将 XML 文档转换为 HTML 文档 - 需要帮助替换元素标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8300284/

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