Xquery 如何将纬度和经度值替换为一个元素值?

Xquery 如何将纬度和经度值替换为一个元素值?,xquery,marklogic,Xquery,Marklogic,如何做到这一点? 您需要将中的纬度和经度值替换为一个元素值 例如: 现有的 <Longitude>-0.30365</Longitude> <Latitude>51.61965</Latitude> -0.30365 51.61965 新的: -0.30365,51.619165您可以使用以下功能: let $doc :- fn:doc("/uri/of/your/doc.xml") let $long := $doc/root/Longitu

如何做到这一点? 您需要将中的纬度和经度值替换为一个元素值

例如: 现有的

<Longitude>-0.30365</Longitude>
<Latitude>51.61965</Latitude>
-0.30365
51.61965
新的:
-0.30365,51.619165

您可以使用以下功能:

let $doc :- fn:doc("/uri/of/your/doc.xml")
let $long := $doc/root/Longitude
let $lat := $doc/root/Latitude
(: construct an element with text() value of the Longitude and Latitude elements :)
let $point := <cordinate-point>{string-join(($lat,$long), ",")}</cordinate-point>
return
  (
    (: insert a new coordinate-point element before the Longitude element :)
    xdmp:node-insert-before($long, $point),
    (: Remove the Longitude element :)
    xdmp:node-delete($long),
    (: Remove the Latitude element :)
    xdmp:node-delete($lat)
  )
let$doc:-fn:doc(“/uri/of/your/doc.xml”)
设$long:=$doc/根/经度
设$lat:=$doc/根/纬度
(:使用经度和纬度元素的text()值构造元素:)
让$point:={string join(($lat,$long),“,”)}
返回
(
(:在经度元素之前插入新的坐标点元素:)
xdmp:在($long,$point)之前插入节点,
(:删除经度元素:)
xdmp:节点删除($long),
(:删除纬度元素:)
xdmp:节点删除($lat)
)

您可以使用本地评估的样式表或安装的XSLT使用XSLT转换文档,然后使用以下转换结果替换现有文档:

声明变量$XSLT:=
;
让$doc:=fn:doc(“/uri/to/your/doc.xml”)
返回
xdmp:node replace($doc,xdmp:xslt eval($xslt,$doc))

您是将这两个元素放在父元素容器中,还是将它们放在元素序列中?@MartinHonnen Yes。父元素是
declare variable $XSLT := 
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    <!--replace the Latitude element with a cordinate-point element -->
    <xsl:template match="Latitude">
        <cordinate-point>
            <xsl:value-of select="., ../Longitude" separator=","/>
        </cordinate-point>
    </xsl:template>

    <!--drop the Longitude element -->
    <xsl:template match="Longitude"/>

</xsl:stylesheet>;

let $doc := fn:doc("/uri/to/your/doc.xml")
return
    xdmp:node-replace($doc, xdmp:xslt-eval($XSLT, $doc))