sql-server
  • xpath
  • 2013-04-05 129 views 4 likes 
    4

    我正尝试在Sql Server中使用XPath来选择以下xml文档的Lookup.Result元素的文本节点。使用XPath选择名称中带有点的xml节点

    <Commands> 
    <Command id="1"> 
    <Lookup.Result>Result.OK</Lookup.Result> 
    </Command> 
    </Commands> 
    

    我尝试以下查询:

    declare @xml xml 
    set @xml = '<Commands> 
        <Command id="1"> 
        <Lookup.Result>Result.OK</Lookup.Result> 
        </Command> 
        </Commands> 
    ' 
    select t.c.value('./Lookup&#46;Result/text()[1]', 'varchar(20)') 
    from @xml.nodes('/Commands/Command') t(c) 
    

    ,但我得到了以下错误: 的XQuery [值()]:语法错误附近 '查找'

    我如何能逃脱。 (Lookup.Result元素标记名称中的点)在XPath表达式中? 请帮忙找到一个优雅的解决方案来解决这个问题。 预先感谢您的时间。

    回答

    3
    declare @xml xml 
        set @xml = '<Commands> 
        <Command id="1"> 
        <Lookup.Result>Result.OK</Lookup.Result> 
        </Command> 
        </Commands> ' 
    
        select t.c.value('(./Lookup.Result/text())[1]', 'varchar(20)') 
        from @xml.nodes('/Commands/Command') t(c) 
    

    你缺少的

    ()

    围绕上述的XPath,MSSQL处理。在我的机器上罚款

    +0

    你说得对。调试时有很多组合,我在其中一个组合中省略了括号...... :))现在可以工作。似乎一个点总是被解释为当前节点,但似乎在某些情况下它被认为只是一个文本。谢谢。 – Vakho 2013-04-05 13:06:39

    +0

    @ user2248972没问题。如果您愿意,您可以将答案标记为已接受。 – Filip 2013-04-05 13:59:27

    0

    那么只有5个逃生字符:

    " &quot; 
    ' &apos; 
    < &lt; 
    > &gt; 
    & &amp; 
    

    ,所以你需要预处理的功能,然后和另一个后期处理。即纸5嵌套REPLACE语句在UDF ...

    相关问题