【问题标题】:How to rename node element names using a FLWOR XQuery?如何使用 FLWOR XQuery 重命名节点元素名称?
【发布时间】:2020-06-18 06:40:45
【问题描述】:

如何使用XQuery 将所有foo 标签重命名为baz,如下所示?

FLWOR:

declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
declare namespace array = "http://www.w3.org/2005/xpath-functions/array";

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

for $x in /root

return rename node $x/root/foo as "baz"

示例文档:

  <root>
    <foo>bar</foo>
  </root>

我正在寻找的输出:

  <root>
    <baz>bar</baz>
  </root>

另见:

http://xqueryfiddle.liberty-development.net/nc4P6y8

和:

removing an element from xml using saxon xquery

它使用的东西并不完全不同..

【问题讨论】:

  • 您使用的是哪个版本的撒克逊语? XQuery fiddle 在不支持 XQuery 更新的情况下使用 Saxon HE。

标签: xml xml-parsing xquery saxon flwor


【解决方案1】:

你应该可以使用

for $foo in doc('input.xml')//foo
return rename node $foo as "baz"

假设您支持 XQuery 更新(例如在 Saxon 9 或 10 EE 或 BaseX 中)。

【讨论】:

    【解决方案2】:

    假设您的真实文档比所示文档更复杂,那么使用 XSLT 而不是 XQuery 通常更容易对大文档进行小幅更改。在 XSLT 3.0 中是

    <xsl:stylesheet ...>
      <xsl:mode on-no-match="shallow-copy"/>
      <xsl:template match="foo">
        <baz><xsl:apply-templates/></baz>
      </xsl:template>
    </xsl:stylesheet>
    

    或者,如果这是一次性要求,请考虑使用 Saxon 的 Gizmo 实用程序:

    java net.sf.saxon.Gizmo -s:source.xml
    />rename //foo to "baz"
    />save out.xml
    />quit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多