【问题标题】:How to properly generate an xml from xquery如何从 xquery 正确生成 xml
【发布时间】:2013-02-06 14:56:29
【问题描述】:

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

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

【问题讨论】:

  • 您的$titeList 似乎是空的。
  • 我认为拉农是对的。也许尝试 $requestBody//book 而不是 $requestBody//bookstore//book。
  • 非常感谢你们帮助我,做到了:) 也感谢关于命名空间的提醒。干得好。

标签: xquery


【解决方案1】:

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

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

XPath 查询的第一部分,即$queryBody//bookstore,查找下面有元素&lt;bookstore/&gt; 的所有后代元素——返回空结果。 $queryBody//bookstore 也不会这样做,因为上下文已经在 &lt;bookstore/&gt; 元素上。

因此,请省略 //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。

【讨论】:

  • 我认为你很接近。 Elmer 显示的示例 XML 没有显示包装器,但是代码是这样的,您必须传入根元素而不是根节点,例如文档节点。这会导致 $requestBody//bookstore 不再工作。还可以使用 $requestBody/descendant-or-self::bookstore 使该表达式更加安全,这与原始表达式略有不同。
  • 是的,对于让代码在没有外部变量来注册的情况下运行感到困惑。 ;) 也许您应该将其发布为答案或更新我的...
  • 是你的暗示启发了我,所以继续努力吧。 ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 1970-01-01
  • 2012-02-20
  • 2020-01-08
相关资源
最近更新 更多