【问题标题】:Getting the right node for query获取正确的节点进行查询
【发布时间】:2019-08-14 01:03:34
【问题描述】:

我在获取查询的正确节点时遇到问题。请问有没有写XML.value查询的资源?

例如,我在下面有这个 XML。 (注意我没有放完整的代码)。

<Result>
  <TestCaseCollection>
    <TestCase name="PowerUPDUT" isPass="true" totalTime="7095.7095">
      <Result name="TestSoftwareID" type="Text" unit="" value="TX1431-002" limitType="Equals" limit="TX1431-002" isPass="true" />
    </TestCase>
    <TestCase name="ReadTestData" isPass="false" totalTime="60120.0114">
      <Result name="Send Radio Enable command" type="Text" unit="" value="" isPass="false">
        <Error code="EER10101002" message="Failed to write value to FG for SetCAMCommand: R;1;RADIO-EN">
          <Exception level="0">
            <Message>EER10101002-Failed to write value to FG for SetCAMCommand: R;1;RADIO-EN</Message>
            <StackTrace>   at TAF.Device.NewportDUT.OnSetMTModemCmd(Text MTModemCmd)
   at TAF.Parameter.WriteAttribute`1.set_Value(MeasurableType value)
   at TAF.Step.AssignStep`1.OnExecute()
   at TAF.Step.Step.Execute()</StackTrace>
            <Source>NewportDUT</Source>
            <TargetSite>Void OnSetMTModemCmd(TAF.Measurable.Text)</TargetSite>
          </Exception>
        </Error>
      </Result>
    </TestCase>
    <TestCase name="TraceabilityCheck" isPass="true" totalTime="0" />
    <TestCase name="FirmwareUpgradeConfirmation" isPass="true" totalTime="0" />
    <TestCase name="UploadCAMFirmware" isPass="true" totalTime="0" />
    <TestCase name="UploadCambridgeFw" isPass="true" totalTime="0" />
    <TestCase name="PreTestCheck" isPass="true" totalTime="0" />
    <TestCase name="CAM Configuration" isPass="true" totalTime="0" />
    <TestCase name="ShippingStates" isPass="true" totalTime="0" />
    <TestCase name="ECO Data Fetching" isPass="true" totalTime="0" />
    <TestCase name="LED Test" isPass="true" totalTime="0" />
    <TestCase name="SaveECOData" isPass="true" totalTime="0" />
  </TestCaseCollection>
</Result>

我尝试使用此代码获取 isPass = failed TestCase 的错误代码、错误消息和 TestCase 名称。

,[ResultXML].value('(/Results/TestCaseCollection/TestCase[@isPass="false"]/Result[@isPass="false"]/Error)[1]/@code', 'varchar(max)') As "ErrorCode"
,[ResultXML].value('(/Results/TestCaseCollection/TestCase[@isPass="false"])[1]/@name', 'varchar(max)') As "ErrorType"
,[ResultXML].value('(/Results/TestCaseCollection/TestCase[@isPass="false"]/Result[@isPass="false"]/Error)[1]/@message', 'varchar(max)') As "ErrorMessage"

如果我做错了什么,以及是否有关于如何正确编写这些内容的资源,请告诉我。

【问题讨论】:

  • 在给定的代码中,您使用了 标签。在获取值时,您在代码中使用“结果”。我认为应该是“结果”[ResultXML].value('(/Result/TestCaseCollection/TestCase[@isPass="false"]/Result[@isPass="false"]/Error)[1]/@code', 'varchar(max)') As "ErrorCode"

标签: sql sql-server xml nodes


【解决方案1】:

您的 XPath 表达式已关闭。请尝试以下测试用例。

SQL

DECLARE @xml XML =
N'<Result>
  <TestCaseCollection>
    <TestCase name="PowerUPDUT" isPass="true" totalTime="7095.7095">
      <Result name="TestSoftwareID" type="Text" unit="" value="TX1431-002" limitType="Equals" limit="TX1431-002" isPass="true" />
    </TestCase>
    <TestCase name="ReadTestData" isPass="false" totalTime="60120.0114">
      <Result name="Send Radio Enable command" type="Text" unit="" value="" isPass="false">
        <Error code="EER10101002" message="Failed to write value to FG for SetCAMCommand: R;1;RADIO-EN">
          <Exception level="0">
            <Message>EER10101002-Failed to write value to FG for SetCAMCommand: R;1;RADIO-EN</Message>
            <StackTrace>   at TAF.Device.NewportDUT.OnSetMTModemCmd(Text MTModemCmd)
   at TAF.Parameter.WriteAttribute`1.set_Value(MeasurableType value)
   at TAF.Step.AssignStep`1.OnExecute()
   at TAF.Step.Step.Execute()</StackTrace>
            <Source>NewportDUT</Source>
            <TargetSite>Void OnSetMTModemCmd(TAF.Measurable.Text)</TargetSite>
          </Exception>
        </Error>
      </Result>
    </TestCase>
    <TestCase name="TraceabilityCheck" isPass="true" totalTime="0" />
    <TestCase name="FirmwareUpgradeConfirmation" isPass="true" totalTime="0" />
    <TestCase name="UploadCAMFirmware" isPass="true" totalTime="0" />
    <TestCase name="UploadCambridgeFw" isPass="true" totalTime="0" />
    <TestCase name="PreTestCheck" isPass="true" totalTime="0" />
    <TestCase name="CAM Configuration" isPass="true" totalTime="0" />
    <TestCase name="ShippingStates" isPass="true" totalTime="0" />
    <TestCase name="ECO Data Fetching" isPass="true" totalTime="0" />
    <TestCase name="LED Test" isPass="true" totalTime="0" />
    <TestCase name="SaveECOData" isPass="true" totalTime="0" />
  </TestCaseCollection>
</Result>';

SELECT col.value('(Result[@isPass="false"]/Error)[1]/@code', 'varchar(max)') As "ErrorCode"
    ,col.value('@name', 'varchar(max)') As "ErrorType"
    ,col.value('(Result[@isPass="false"]/Error)[1]/@message', 'varchar(max)') As "ErrorMessage"
FROM @xml.nodes('/Result/TestCaseCollection/TestCase[@isPass="false"]') AS tab(col);

【讨论】:

    猜你喜欢
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 2016-09-25
    • 2021-05-24
    • 2012-05-15
    相关资源
    最近更新 更多