【问题标题】:Extract data from XML string in SQL using XQuery使用 XQuery 从 SQL 中的 XML 字符串中提取数据
【发布时间】:2014-10-03 13:12:46
【问题描述】:

我正在尝试从存储在我的表中 XMLString 列中的 XML 字符串中提取数据,如下所示..

VId   Uid   Name    TWd     XMLString 
26    jti   jbreti  testell string in xml format
26    Man   Lomond  Mcan    string in xml format
26    mw    mlwTest tewWell string in xml format
26    tot   teapot  te2Well string in xml format

具有以下格式的 XML 字符串。它也有多级节点..

<well uid="b4e952f9">
    <name>Demo</name>
    <field>Fi Tk</field>
    <country>India</country>
    <county>South India</county>
    <region>SiD</region>
    <block>09-365</block>
    <timeZone>+09:00</timeZone>
    <operator>AACE Oil CO</operator>
    <operatorDiv>AAACE South Australia</operatorDiv>
    <statusWell>unknown</statusWell>
    <wellDatum defaultElevation="true" uid="SL">
        <name>Mean Sea Level</name>
        <code>SL</code>
    </wellDatum>
    <waterDepth uom="ft">269.0256</waterDepth>
    <wellLocation uid="loc-1">
        <latitude uom="dega">-28.601403</latitude>
        <longitude uom="dega">137.444458</longitude>
    </wellLocation>
    <commonData>
        <dTimCreation>2012-04-10T13:17:45.959Z</dTimCreation>
        <dTimLastChange>2013-11-08T14:42:56.340Z</dTimLastChange>
    </commonData>
</well>

我需要上面 XML 中的几个节点,这几个节点的详细信息也在 XML 字符串中,如下所示..

<well>
    <name></name>       
    <country></country>
    <block></block>
    <timeZone></timeZone>
    <wellDatum>
        <name></name>
        <code></code>
    </wellDatum>
    <waterDepth></waterDepth>
</well>

现在我需要从第一个 XML 中提取节点,其节点值字符串存在于第二个 XML 字符串中。 并且输出也应该是一个 XML 字符串。 输出字符串应该如下......

<well uid="b4e952f9">
    <name>Demo</name>
    <country>India</country>
    <block>09-365</block>
    <timeZone>+09:00</timeZone>
    <wellDatum defaultElevation="true" uid="SL">
        <name>Mean Sea Level</name>
        <code>SL</code>
    </wellDatum>
    <waterDepth uom="ft">269.0256</waterDepth>
</well>

这一切我只想在 MSSQL 中完成。 谁能帮帮我..

【问题讨论】:

标签: sql-server xml xquery


【解决方案1】:
SELECT XMLString.query(
'<well>
    <name>{/well/name/node()}</name>       
    <country>{/well/country/node()}</country>
    <block>{/well/block/node()}</block>
    <timeZone>{/well/timeZone/node()}</timeZone>
    <wellDatum>
        <name>{/well/wellDatum/name/node()}</name>
        <code>{/well/wellDatum/code/node()}</code>
    </wellDatum>
    <waterDepth>{/well/waterDepth/node()}</waterDepth>
 </well>'
),
   XMLString FROM   Well

【讨论】:

  • 如何生成我在 XMLString.query 函数中传递的字符串?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多