如何从xquery正确生成xml

如何从xquery正确生成xml,xquery,Xquery,我是xquery新手,正在尝试阅读关于使用该工具的不同参考资料。我一直在尝试测试和生成一些xml格式的消息,但这一点让我感到困惑。这是我的xQuery代码: 示例XQuery declare variable $requestBody as element() external; declare function VerifyOrderDetailTransformation($requestBody as element()) as element() { <msg>

我是xquery新手,正在尝试阅读关于使用该工具的不同参考资料。我一直在尝试测试和生成一些xml格式的消息,但这一点让我感到困惑。这是我的xQuery代码:

示例XQuery

declare variable $requestBody as element() external;
declare function VerifyOrderDetailTransformation($requestBody as element())
as element() {
    <msg>
        <header>
            <headtitle>This is the title</headtitle>
        </header>
        <dbody>
            {GenerateEquipmentListNodes($requestBody)} 
        </dbody>
    </msg>
};

declare function GenerateEquipmentListNodes($requestBody as element())
as element()* {

    let $titleList := (
        for $e in $requestBody//bookstore//book
            let $dTitle := $e/title/text()
            return 
               <theTitle>{$dTitle}</theTitle>
        )

    return 
       <dTitleList>
           {$titleList}
       </dTitleList>
};

VerifyOrderDetailTransformation($requestBody)
将变量$requestBody声明为元素()外部;
声明函数VerifyOrderDetailTransformation($requestBody作为元素())
as元素(){
这是标题
{GenerateeEquipmentListNodes($requestBody)}
};
声明函数GenerateEquipmentListNodes($requestBody作为元素())
as元素()*{
让$titleList:=(
对于$requestBody//bookstore//book中的$e
让$dTitle:=$e/title/text()
返回
{$dTitle}
)
返回
{$titleList}
};
VerifyOrderDetailTransformation($requestBody)
示例XML

<bookstore>

<book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
</book>

<book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
</book>

<book category="WEB">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
</book>

<book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
</book>

</bookstore>

日常意大利语
吉娅达·德·劳伦蒂斯
2005
30
哈利·波特
J K.罗琳
2005
29.99
XQuery启动
詹姆斯·麦戈文
伯特纳
库尔特·卡格尔
詹姆斯·林恩
瓦迪亚纳坦·纳加拉扬
2003
49.99
学习XML
埃里克·T·雷
2003
39.95
下面是在XML上运行xQuery生成的输出:

电流输出

<msg> 
    <head> 
        <title>This is the title</title> 
    </head> 
    <body> 
        <dTitleList/> 
    </body> 
</msg>
<msg> 
    <head> 
        <title>This is the title</title> 
    </head> 
    <body> 
        <dTitleList> 
        <theTitle>Everyday Italian</theTitle>
        <theTitle>Harry Potter</theTitle>
        <theTitle>XQuery Kick Start</theTitle>
        <theTitle>Learning XML</theTitle>
        <dTitleList/> 
    </body> 
</msg>

这是标题
预期产出

<msg> 
    <head> 
        <title>This is the title</title> 
    </head> 
    <body> 
        <dTitleList/> 
    </body> 
</msg>
<msg> 
    <head> 
        <title>This is the title</title> 
    </head> 
    <body> 
        <dTitleList> 
        <theTitle>Everyday Italian</theTitle>
        <theTitle>Harry Potter</theTitle>
        <theTitle>XQuery Kick Start</theTitle>
        <theTitle>Learning XML</theTitle>
        <dTitleList/> 
    </body> 
</msg>

这是标题
日常意大利语
哈利·波特
XQuery启动
学习XML


我的问题是,我可能遗漏了什么?

您的输入有一些问题:您正在查询此XML:

<bookstore>
  <book>
    <!-- snip -->
  </book>
  <!-- snip -->
</bookstore>


还有一句话:您应该将自己的函数放入
local:
函数名称空间或定义自己的函数名称空间。不鼓励使用默认名称空间,并且不与所有处理器兼容。我将其更改为
本地:
-名称空间。

您的输入有一些问题:您正在查询此XML:

<bookstore>
  <book>
    <!-- snip -->
  </book>
  <!-- snip -->
</bookstore>


还有一句话:您应该将自己的函数放入
local:
函数名称空间或定义自己的函数名称空间。不鼓励使用默认名称空间,并且不与所有处理器兼容。我将其更改为
本地:
-名称空间。

您的
$titeList
似乎是空的。我认为Ranon是对的。也许可以尝试使用$requestBody//book而不是$requestBody//bookstore//book。非常感谢你们的帮助,我做到了:),也感谢你们提醒我使用名称空间。干得好。看来你的
$titeList
是空的。我认为拉农是对的。也许可以尝试使用$requestBody//book而不是$requestBody//bookstore//book。非常感谢你们的帮助,我做到了:),也感谢你们提醒我使用名称空间。干得好。我想你们很接近。Elmer显示的示例XML没有显示包装器,但是代码必须传入根元素而不是根节点,例如文档节点。这导致$requestBody//bookstore不再工作。还可以使用$requestBody/genderant或self::bookstore使该表达式更安全,这与原始表达式略有不同。是的,让代码在没有外部变量的情况下运行以注册该表达式会让人感到困惑。;)也许你应该把它作为一个答案发布,或者更新我的答案……是你的暗示启发了我,所以继续吧,把这归功于你我想你很接近。Elmer显示的示例XML没有显示包装器,但是代码必须传入根元素而不是根节点,例如文档节点。这导致$requestBody//bookstore不再工作。还可以使用$requestBody/genderant或self::bookstore使该表达式更安全,这与原始表达式略有不同。是的,让代码在没有外部变量的情况下运行以注册该表达式会让人感到困惑。;)也许你应该把它作为一个答案发布,或者更新我的答案……是你的暗示启发了我,所以继续吧,把这归功于你