当URI未知时,XPath是否有定义的方法从其缩写中检索命名空间的URI?

时间:2017-11-18 04:57:22

标签: sql-server xml xpath namespaces

我正在尝试编写一个XPath查询来提取将始终使用相同缩写的特定命名空间的URI。

示例:

<RequestPacket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="urn:dont:really:care">
    <a:Requests xmlns:queryNS="urn:uri:i:need">
        <a:Request xsi:type="queryNS:GetImportantData">
            <queryNS:Version>3</queryNS:Version>
            ...
        </a:Request>
    </a:Requests>
</RequestPacket>

我需要提取与queryNS命名空间相对应的URI,在我的示例中为urn:uri:i:need

这可能吗?如果是这样,有人可以帮忙吗?

我试图使用namespace-uri()函数来获取它,但为了这样做,我必须指定一个节点,我只能通过在每个节点指定命名空间来做(这就是我所做的)试图搞清楚)。所以它看起来像鸡和鸡蛋的情况。

谢谢!

2 个答案:

答案 0 :(得分:3)

有两种方法:

DECLARE @xml XML=
N'<RequestPacket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="urn:dont:really:care">
    <a:Requests xmlns:queryNS="urn:uri:i:need">
        <a:Request xsi:type="queryNS:GetImportantData">
            <queryNS:Version>3</queryNS:Version>
            ...
        </a:Request>
    </a:Requests>
</RequestPacket>';

- 对一个节点使用namespace-uri(),该节点

SELECT @xml.value(N'namespace-uri((//*:Version)[1])',N'nvarchar(max)')

- 这可能是罕见的情况之一,FROM OPENXML is still helpful。试试这个:

DECLARE @docHandle INT
EXEC sp_xml_preparedocument @docHandle OUTPUT, @xml;
SELECT * FROM OPENXML (@docHandle, '',1)   
EXEC sp_xml_removedocument @docHandle  

结果

+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| id | parentid | nodetype | localname     | prefix  | namespaceuri                              | datatype | prev | text                                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 0  | NULL     | 1        | RequestPacket | NULL    | NULL                                      | NULL     | NULL | NULL                                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 2  | 0        | 2        | xsi           | xmlns   | NULL                                      | NULL     | NULL | NULL                                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 10 | 2        | 3        | #text         | NULL    | NULL                                      | NULL     | NULL | http://www.w3.org/2001/XMLSchema-instance |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 3  | 0        | 2        | a             | xmlns   | NULL                                      | NULL     | NULL | NULL                                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 11 | 3        | 3        | #text         | NULL    | NULL                                      | NULL     | NULL | urn:dont:really:care                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 4  | 0        | 1        | Requests      | a       | urn:dont:really:care                      | NULL     | NULL | NULL                                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 5  | 4        | 2        | queryNS       | xmlns   | NULL                                      | NULL     | NULL | NULL                                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 12 | 5        | 3        | #text         | NULL    | NULL                                      | NULL     | NULL | urn:uri:i:need                            |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 6  | 4        | 1        | Request       | a       | urn:dont:really:care                      | NULL     | NULL | NULL                                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 7  | 6        | 2        | type          | xsi     | http://www.w3.org/2001/XMLSchema-instance | NULL     | NULL | NULL                                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 13 | 7        | 3        | #text         | NULL    | NULL                                      | NULL     | NULL | queryNS:GetImportantData                  |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 8  | 6        | 1        | Version       | queryNS | urn:uri:i:need                            | NULL     | NULL | NULL                                      |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 14 | 8        | 3        | #text         | NULL    | NULL                                      | NULL     | NULL | 3                                         |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+
| 9  | 6        | 3        | #text         | NULL    | NULL                                      | NULL     | 8    | ...                                       |
+----+----------+----------+---------------+---------+-------------------------------------------+----------+------+-------------------------------------------+

您可以使用parentidnodetype来查找您的URI:属性queryNS的ID为5。这是元素12的parentid,其中#text是您要查找的URI。但是这不能在 adhoc / inline 查询中使用。

更新:您可以使用.query()

SELECT @xml.query(
N'
<root>
{
    for $nd in //*
    return
    <nd ns="{namespace-uri($nd)}" name="{local-name($nd)}" value="{($nd/text())[1]}">
    {
        for $attr in $nd/@*
        return
        <attr ns="{namespace-uri($attr)}" name="{local-name($attr)}" value="{$attr}"  />
    }
    </nd>
}
</root>
');

结果

<root>
  <nd ns="" name="RequestPacket" value="" />
  <nd ns="urn:dont:really:care" name="Requests" value="" />
  <nd ns="urn:dont:really:care" name="Request" value="&#xA;            ...&#xA;        ">
    <attr ns="http://www.w3.org/2001/XMLSchema-instance" name="type" value="queryNS:GetImportantData" />
  </nd>
  <nd ns="urn:uri:i:need" name="Version" value="3" />
</root>

答案 1 :(得分:1)

只需使用命名空间轴:

string(namespace::queryNS)

或者如果名称空间前缀不是静态已知的

string(namespace::*[name()=$prefix])

命名空间轴在XQuery中不可用,并且在XPath 2.0+中已弃用,因此您可以改为使用

namespace-uri-for-prefix($prefix, $element)

CAVEAT :我刚刚注意到这个问题被标记为sql-server。我对SQL Server中的XPath / XQuery实现一无所知,除了它并不总是符合这一点。