在XML中的任何位置查找元素或属性值

时间:2017-07-27 12:56:52

标签: sql sql-server xml xpath

我试图找到元素/属性的值,而不管它在XML中的位置。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<cXML payloadID="12345677-12345567" timestamp="2017-07-26T09:11:05">
  <Header>
    <From>
      <Credential domain="1212">
        <Identity>01235          </Identity>
        <SharedSecret/>
      </Credential>
    </From>
    <To>
      <Credential domain="1212">
        <Identity>01234</Identity>
      </Credential>
    </To>
    <Sender>
      <UserAgent/>
      <Credential domain="8989">
        <Identity>10678</Identity>
        <SharedSecret>Testing123</SharedSecret>
      </Credential>
    </Sender>
  </Header>
  <Request deploymentMode="Prod">
    <ConfirmationRequest>
      <ConfirmationHeader noticeDate="2017-07-26T09:11:05" operation="update" type="detail">
        <Total>
          <Money>0.00</Money>
        </Total>
        <Shipping>
          <Description>Delivery</Description>
        </Shipping>

        <Comments>WO# generated</Comments>
      </ConfirmationHeader>
      <OrderReference orderDate="2017-07-25T15:22:11" orderID="123456780000">
        <DocumentReference payloadID="5678-4567"/>
      </OrderReference>
      <ConfirmationItem quantity="1" lineNumber="1">
        <ConfirmationStatus quantity="1" type="detail">
          <ItemIn quantity="1">
            <ItemID>
              <SupplierPartID>R954-89</SupplierPartID>
            </ItemID>
            <ItemDetail>
              <UnitPrice>
                <Money currency="USD">0.00</Money>
              </UnitPrice>
              <Description>Test Descritpion 1</Description>
              <UnitOfMeasure>QT</UnitOfMeasure>
            </ItemDetail>
          </ItemIn>
        </ConfirmationStatus>
      </ConfirmationItem>
      <ConfirmationItem quantity="1" lineNumber="2">
        <ConfirmationStatus quantity="1" type="detail">
          <ItemIn quantity="1">
            <ItemID>
              <SupplierPartID>Y954-89</SupplierPartID>
            </ItemID>
            <ItemDetail>
              <UnitPrice>
                <Money currency="USD">0.00</Money>
              </UnitPrice>
              <Description>Test Descritpion 2</Description>
              <UnitOfMeasure>QT</UnitOfMeasure>
            </ItemDetail>
          </ItemIn>
        </ConfirmationStatus>
      </ConfirmationItem>

    </ConfirmationRequest>
  </Request>
</cXML>

我想获得payloadID元素上DocumentReference的值。这是我到目前为止所尝试的:

BEGIN
     Declare @Xml xml
     Set @Xml = ('..The XML From Above..' as xml)       
END

--no value comes back
Select c.value('(/*/DocumentReference/@payloadID)[0]','nvarchar(max)') from @Xml.nodes('//cXML') x(c)

--no value comes back
Select c.value('@payloadID','nvarchar(max)') from @Xml.nodes('/cXML/*/DocumentReference') x(c)

--check if element exists and it does
Select @Xml.exist('//DocumentReference');

我在xPath编辑器中试过这个://DocumentReference/@payloadID 这确实有效,但我不确定SQL中的等效语法是什么

1 个答案:

答案 0 :(得分:1)

调用.nodes()(如评论中建议的那样)是一项不必要的开销......

最好这样试试:

SELECT @XML.value('(//DocumentReference/@payloadID)[1]','nvarchar(max)')

请注意,XPath1开始计算。您使用[0]的示例无效...

--no value comes back
Select c.value('(/*/DocumentReference/@payloadID)[0]','nvarchar(max)') from...