<small id='CIUXS'></small><noframes id='CIUXS'>

        <tfoot id='CIUXS'></tfoot>
      1. <legend id='CIUXS'><style id='CIUXS'><dir id='CIUXS'><q id='CIUXS'></q></dir></style></legend>

      2. <i id='CIUXS'><tr id='CIUXS'><dt id='CIUXS'><q id='CIUXS'><span id='CIUXS'><b id='CIUXS'><form id='CIUXS'><ins id='CIUXS'></ins><ul id='CIUXS'></ul><sub id='CIUXS'></sub></form><legend id='CIUXS'></legend><bdo id='CIUXS'><pre id='CIUXS'><center id='CIUXS'></center></pre></bdo></b><th id='CIUXS'></th></span></q></dt></tr></i><div id='CIUXS'><tfoot id='CIUXS'></tfoot><dl id='CIUXS'><fieldset id='CIUXS'></fieldset></dl></div>
          <bdo id='CIUXS'></bdo><ul id='CIUXS'></ul>

        使用 XQuery 获取这些数据

        时间:2023-06-05
      3. <small id='umV96'></small><noframes id='umV96'>

        <tfoot id='umV96'></tfoot>

            • <bdo id='umV96'></bdo><ul id='umV96'></ul>

                <legend id='umV96'><style id='umV96'><dir id='umV96'><q id='umV96'></q></dir></style></legend>
              1. <i id='umV96'><tr id='umV96'><dt id='umV96'><q id='umV96'><span id='umV96'><b id='umV96'><form id='umV96'><ins id='umV96'></ins><ul id='umV96'></ul><sub id='umV96'></sub></form><legend id='umV96'></legend><bdo id='umV96'><pre id='umV96'><center id='umV96'></center></pre></bdo></b><th id='umV96'></th></span></q></dt></tr></i><div id='umV96'><tfoot id='umV96'></tfoot><dl id='umV96'><fieldset id='umV96'></fieldset></dl></div>
                    <tbody id='umV96'></tbody>
                  本文介绍了使用 XQuery 获取这些数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我是 XQuery 的新手,但遇到了一些问题.这是我的例子.

                  I am new to XQuery and am having some problems with it. Here is my example.

                  我有这个变量:

                  declare @xmlDoc XML
                  

                  其中存储了以下 xml:

                  it has the following xml stored in it:

                  <?xml version="1.0" encoding="utf-8"?>
                  <NewDataSet>
                    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                      <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="">
                        <xs:complexType>
                          <xs:choice minOccurs="0" maxOccurs="unbounded">
                            <xs:element name="Table1">
                              <xs:complexType>
                                <xs:sequence>
                                  <xs:element name="Sharedparam" type="xs:string" minOccurs="0" />
                                  <xs:element name="Antoher" type="xs:string" minOccurs="0" />
                                  <xs:element name="RandomParam2" type="xs:string" minOccurs="0" />
                                  <xs:element name="MoreParam" type="xs:string" minOccurs="0" />
                                  <xs:element name="ResultsParam" type="xs:string" minOccurs="0" />
                                </xs:sequence>
                              </xs:complexType>
                            </xs:element>
                          </xs:choice>
                        </xs:complexType>
                      </xs:element>
                    </xs:schema>
                    <Table1>
                      <Sharedparam>shared</Sharedparam>
                      <Antoher>sahre</Antoher>
                      <RandomParam2>Good stuff</RandomParam2>
                      <MoreParam>and more</MoreParam>
                      <ResultsParam>2</ResultsParam>
                    </Table1>
                    <Table1>
                      <Sharedparam>Hey</Sharedparam>
                      <Antoher>what </Antoher>
                      <RandomParam2>do you</RandomParam2>
                      <MoreParam>think</MoreParam>
                      <ResultsParam>2</ResultsParam>
                    </Table1>
                    <Table1 />
                  </NewDataSet>
                  

                  如何选择 Sharedparam 的所有值?(或者真的任何返回值(不是 xml)的体面的查询都会很棒.)

                  How can I select all the values of Sharedparam? (Or really any decent query that returns values (not xml) would be great.)

                  我真正想做的是得到这样的结果集:

                  What I am really looking to do is get a result set like this:

                  Name             Value1          Value2          Value3        Value4
                  Sharedparam      shared          Hey             Null          Null
                  Another          share           what            Null          Null
                  ....
                  

                  这会让我忽略Value4"之外的任何数据(这对于我使用这些数据是可以接受的).

                  This would have me ignoring any data beyond "Value4" (and that is acceptable for my use of this data).

                  推荐答案

                  试试这个:

                  SELECT
                      TBL.SParam.value('(.)[1]', 'varchar(50)')
                  FROM
                      @xmldoc.nodes('/NewDataSet/Table1/Sharedparam') AS TBL(SParam)
                  

                  给我一​​个输出:

                  (No column name)
                  shared
                  Hey
                  

                  更新:如果您想获得 <Table1> 元素中的所有 XML 元素及其值,您可以使用这个 XQuery:

                  Update: if you want to get at all the XML elements and their values inside the <Table1> elements, you can use this XQuery:

                  SELECT
                      TBL.SParam.value('local-name(.)[1]', 'varchar(50)') 'Attribute',
                      TBL.SParam.value('(.)[1]', 'varchar(50)') 'Value'
                  FROM
                      @xmldoc.nodes('/NewDataSet/Table1/*') AS TBL(SParam)
                  

                  输出:

                  Attribute            Value
                  Sharedparam          shared
                  Antoher              sahre
                  RandomParam2         Good stuff
                  MoreParam            and more
                  ResultsParam         2
                  Sharedparam          Hey
                  Antoher              what 
                  RandomParam2         do you
                  MoreParam            think
                  ResultsParam         2
                  

                  更新 #2: 获取第一个 和第二个 XML 节点旁边的值彼此之间,您需要对 .nodes() 进行两次调用 - 一次检索第一个节点,另一次检索第二个.它有点毛茸茸的,特别是如果你想进一步扩展它 - 性能会很糟糕 - 但它有效:-)

                  Update #2: to get the values of the first <Table1> and the second <Table1> XML node next to one another, you need to do two calls to .nodes() - once retrieving the first node, the other time the second one. It gets a bit hairy, especially if you want to extend that even further - and performance is going to be abysmal - but it works :-)

                  SELECT
                      TBL.SParam.value('local-name(.)[1]', 'varchar(50)') 'Attribute',
                      TBL.SParam.value('(.)[1]', 'varchar(50)') 'Value 1',
                      TBL2.SParam2.value('(.)[1]', 'varchar(50)') 'Value 2'
                  FROM
                      @xmldoc.nodes('/NewDataSet/Table1[1]/*') AS TBL(SParam)
                  INNER JOIN
                      @xmldoc.nodes('/NewDataSet/Table1[2]/*') AS TBL2(SParam2) ON TBL.SParam.value('local-name(.)[1]', 'varchar(50)') = TBL2.SParam2.value('local-name(.)[1]', 'varchar(50)')
                  

                  给出以下输出:

                  Attribute      Value 1     Value 2
                  Sharedparam    shared       Hey
                  ResultsParam      2          2
                  RandomParam2   Good stuff   do you
                  Antoher        sahre        what 
                  MoreParam      and more     think
                  

                  这篇关于使用 XQuery 获取这些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在单个查询中将多个节点插入到 xml 字段 下一篇:如何在 SQL 语句中将变量传递到 XQuery 的 XPath

                  相关文章

                  最新文章

                  <legend id='WfQZw'><style id='WfQZw'><dir id='WfQZw'><q id='WfQZw'></q></dir></style></legend>
                  <i id='WfQZw'><tr id='WfQZw'><dt id='WfQZw'><q id='WfQZw'><span id='WfQZw'><b id='WfQZw'><form id='WfQZw'><ins id='WfQZw'></ins><ul id='WfQZw'></ul><sub id='WfQZw'></sub></form><legend id='WfQZw'></legend><bdo id='WfQZw'><pre id='WfQZw'><center id='WfQZw'></center></pre></bdo></b><th id='WfQZw'></th></span></q></dt></tr></i><div id='WfQZw'><tfoot id='WfQZw'></tfoot><dl id='WfQZw'><fieldset id='WfQZw'></fieldset></dl></div>

                  1. <tfoot id='WfQZw'></tfoot>
                      <bdo id='WfQZw'></bdo><ul id='WfQZw'></ul>

                    <small id='WfQZw'></small><noframes id='WfQZw'>