1

我是 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)

示例 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>

以下是在 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>

我的问题是,我可能错过了什么?

4

1 回答 1

1

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

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

XPath 查询的第一部分,即$queryBody//bookstore查找所有具有<bookstore/>以下元素的后代元素 - 返回空结果。$queryBody//bookstore也不会这样做,因为上下文已经在<bookstore/>元素上。

出于这个原因, ommit //bookstore,所以你的 this 应该是$queryBody//book

将此函数与其中已更改的 XPath 一起使用:

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

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

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

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

于 2013-02-06T15:46:54.373 回答