【发布时间】:2014-05-15 15:07:01
【问题描述】:
我正在尝试分解具有多层嵌套元素的 XML 文档:
<server>
<name>111.111.11.11</name>
<displayName>EVIL SERVER</displayName>
<comment />
<logonCredentials inherit="None">
<userName>user</userName>
<domain>DOMAIN</domain>
<password storeAsClearText="True">xxxxxxx</password>
</logonCredentials>
<connectionSettings inherit="FromParent" />
<gatewaySettings inherit="FromParent" />
<remoteDesktop inherit="FromParent" />
<localResources inherit="None">
<audioRedirection>2</audioRedirection>
<audioRedirectionQuality>2</audioRedirectionQuality>
<audioCaptureRedirection>0</audioCaptureRedirection>
<keyboardHook>2</keyboardHook>
<redirectClipboard>True</redirectClipboard>
<redirectDrives>True</redirectDrives>
<redirectPorts>False</redirectPorts>
<redirectPrinters>False</redirectPrinters>
<redirectSmartCards>False</redirectSmartCards>
</localResources>
<securitySettings inherit="FromParent" />
<displaySettings inherit="FromParent" />
</server>
<server>
<name>111.12.11.11</name>
<displayName>NICE SERVER</displayName>
<comment />
<logonCredentials inherit="None">
<userName>user2</userName>
<domain>DOMAIN2</domain>
<password storeAsClearText="True">xxxxxxx</password>
</logonCredentials>
<connectionSettings inherit="FromParent" />
<gatewaySettings inherit="FromParent" />
<remoteDesktop inherit="FromParent" />
<localResources inherit="FromParent" />
<securitySettings inherit="FromParent" />
<displaySettings inherit="FromParent" />
</server>
我想将它们返回到一个表格中,其中包含从服务器名称到登录凭据的所有值。
我已经能够像这样使用 CROSS APPLY 做到这一点:(不完整)
SELECT
name = n.value('name[1]','varchar(15)'),
displayName = dn.value('.','varchar(8000)'),
comment = c.value('.','varchar(8000)'),
userName = un.value('.','varchar(8000)'),
domain = d.value('.','varchar(8000)'),
[password] = p.value('.','varchar(8000)'),
connectionSettings = cs.value('.','varchar(8000)')
audioRedirection = ar.value('.', 'varchar(8000)')
FROM @RDCM.nodes('/group/server') AS s(n)
CROSS APPLY s.n.nodes('displayName') s1(dn)
CROSS APPLY s.n.nodes('comment') s2(c)
CROSS APPLY s.n.nodes('logonCredentials') s4(lc)
CROSS APPLY s4.lc.nodes('userName') lc1(un)
CROSS APPLY s4.lc.nodes('domain') lc2(d)
CROSS APPLY s4.lc.nodes('password') lc3(p)
CROSS APPLY s.n.nodes('connectionSettings') s3(cs)
CROSS APPLY s.n.nodes('localResources') s8(lr)
OUTER APPLY s8.lr.nodes('audioRedirection') lr1(ar)
但是,我才刚刚开始构建这个查询,很明显它需要很长时间才能运行。
我也尝试过 OPENXML,但我只能获得一个级别。
【问题讨论】:
-
永远是一段很长的时间。你的 XML 有多大?你需要解析多少个服务器节点?需要多长时间?
-
而且看起来您实际上根本不需要任何交叉申请。在 values() 函数中使用路径表达式是完全可以的。 openxml 也是如此。您正在服务器上粉碎,剩下的只是找到您需要的价值。
标签: sql-server xml performance tsql xquery-sql