使用 XQuery 获取此数据
我是 XQuery 的新手,并且遇到了一些问题。这是我的例子。
我有这个变量:
declare @xmlDoc XML
它存储了以下 xml:
<?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)的像样的查询都会很棒。)
我真正想做的是得到这样的结果集:
Name Value1 Value2 Value3 Value4
Sharedparam shared Hey Null Null
Another share what Null Null
....
这将使我忽略“Value4”之外的任何数据(这是可以接受的)我对这些数据的使用)。
I am new to XQuery and am having some problems with it. Here is my example.
I have this variable:
declare @xmlDoc 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>
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
....
This would have me ignoring any data beyond "Value4" (and that is acceptable for my use of this data).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的操作:
给我一个输出:
更新: 如果您想获取
元素内的所有 XML 元素及其值,您可以使用此 XQuery:输出:
Update #2: 获取第一个
和第二个
的值XML 节点彼此相邻,您需要对.nodes()
进行两次调用 - 一次检索第一个节点,另一次检索第二个节点。它变得有点毛茸茸的,特别是如果你想进一步扩展 - 并且性能将会很糟糕 - 但它有效:-)给出的输出:
Try something like this:
Gives me an output of:
Update: if you want to get at all the XML elements and their values inside the
<Table1>
elements, you can use this XQuery:which outputs:
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 :-)Gives an output of:
这是一个奇怪的布局。您期望列 Sharedparam、Antoher 等:而不是行。
如果我没看错的话,Table1
maxOccurs="unbounded"
这意味着可变的列数 = 不是 SQL,它是固定的列类型和编号将每个标签读取为一列(这是固定且有限的) )你会这样做:
That's an odd layout. You expect columns Sharedparam,Antoher etc: not rows.
And if I read that right, Table1
maxOccurs="unbounded"
which means an variable number of columns = not SQL which is fixed column type and numberTo read each tag as a column (which are fixed and finite) you'd do this: