Xpath提取SSIS XML中的属性值

huangapple go评论66阅读模式
英文:

Xpath to pull out property value from SSIS XML

问题

我可以提取组件的属性,例如通过 ./@refId 提取 refID,通过向上遍历祖先树 ../../../../@DTS:refId 提取主要父级,但是我无法提取属性的值,这些属性可以是 "OpenRowset" 或 "SqlCommand",其值为 "[dbo].[DestinationTable]"

WITH XMLNAMESPACES ('www.microsoft.com/SqlServer/Dts' AS DTS)
SELECT
XML.XMLDOC
, C.Comp.value('../../../../@DTS:DTSID','nvarchar(max)') as [DTSID]
, C.Comp.value('../../../../@DTS:refId','nvarchar(max)') as [Executable]
, C.Comp.value('../../../../@DTS:ObjectName','nvarchar(max)') as [ObjectName]
, C.Comp.value('../../../../@DTS:Description','nvarchar(max)') as [Description]
, C.Comp.value('../../../../@DTS:ExecutableType','nvarchar(max)') as [ExecutableType]
, C.Comp.value('./@refId','nvarchar(max)') as [Component]
, C.Comp.value('./property[@name=''OpenRowset|SqlCommand'']','nvarchar(max)') as [SQL]
FROM dbo.XMLFiles XML
CROSS APPLY XMLDOC.nodes('//DTS:ObjectData/pipeline/components/component') C(Comp)

希望有所帮助。

谢谢!

英文:

I've got the following XML

  <DTS:Executables>
    <DTS:Executable DTS:refId="Package\...." DTS:CreationName="STOCK:SEQUENCE" DTS:Description="..." DTS:DTSID="{...}" DTS:ExecutableType="STOCK:SEQUENCE" DTS:LocaleID="-1" DTS:ObjectName="...">
      <DTS:Variables />
      <DTS:Executables>
        <DTS:Executable DTS:refId="Package\...." DTS:CreationName="Microsoft.Pipeline" DTS:Description="..." DTS:DTSID="{...}" DTS:ExecutableType="Microsoft.Pipeline" DTS:LocaleID="-1" DTS:ObjectName="..." DTS:TaskContact="Performs high-performance data extraction, transformation and loading;Microsoft Corporation; Microsoft SQL Server; (C) Microsoft Corporation; All Rights Reserved;http://www.microsoft.com/sql/support/default.asp;1">
          <DTS:Variables />
          <DTS:ObjectData>
            <pipeline defaultBufferMaxRows="25000" defaultBufferSize="100485760" version="1">
              <components>
                <component refId="...\OLE DB Destination" componentClassID="Microsoft.OLEDBDestination" contactInfo="OLE DB Destination;Microsoft Corporation; Microsoft SQL Server; (C) Microsoft Corporation; All Rights Reserved; http://www.microsoft.com/sql/support;4" description="OLE DB Destination" name="OLE DB Destination" usesDispositions="true" version="4">
                  <properties>
                    <property dataType="System.Int32" description="The number of seconds before a command times out.  A value of 0 indicates an infinite time-out." name="CommandTimeout">0</property>
                    <property dataType="System.String" description="Specifies the name of the database object used to open a rowset." name="OpenRowset">[dbo].[DestinationTable]</property>
                    <property dataType="System.String" description="Specifies the variable that contains the name of the database object used to open a rowset." name="OpenRowsetVariable" />
...                    

I can pull out the attributes of the component i.e. the refID with ./@refId, the main parent by going up the ancestor tree ../../../../@DTS:refId, but I'm unable to pull out the "[dbo].[DestinationTable]" value for the property which can be either "OpenRowset" or "SqlCommand"

WITH XMLNAMESPACES ('www.microsoft.com/SqlServer/Dts' AS DTS)
SELECT
XML.XMLDOC
, C.Comp.value('../../../../@DTS:DTSID','nvarchar(max)') as [DTSID]
, C.Comp.value('../../../../@DTS:refId','nvarchar(max)') as [Executable]
, C.Comp.value('../../../../@DTS:ObjectName','nvarchar(max)') as [ObjectName]
, C.Comp.value('../../../../@DTS:Description','nvarchar(max)') as [Description]
, C.Comp.value('../../../../@DTS:ExecutableType','nvarchar(max)') as [ExecutableType]
, C.Comp.value('./@refId','nvarchar(max)') as [Component]
, C.Comp.value('./property[@name=''OpenRowset|SqlCommand'']','nvarchar(max)') as [SQL]
FROM dbo.XMLFiles XML
CROSS APPLY XMLDOC.nodes('//DTS:ObjectData/pipeline/components/component' ) C(Comp)

Any help is appreciated.

Thanks

答案1

得分: 0

  1. property 似乎是 properties 的子元素,因此属性元素的路径将是:properties/property
  2. 为了在 XPath 1.0 中使用多个值,您必须像这样使用 or[context='some-text' or context='some-text']
  3. 由于您多次查询了 @name 属性,您可以像这样在该属性上放置谓词:@name[.='OpenRowset' or .='SqlCommand']

XPath 将如下所示:

properties/property[@name[.='OpenRowset' or .='SqlCommand']]
英文:
  1. property seems to be a child of properties, so the path to the property-element will be: properties/property

  2. in order to use multiple values in XPath 1.0 you have to use the or like this: [context='some-text' or context= 'some-text']

  3. Since you are querying the @name attribute more than once you can put a predicate on that attribute like this: @name[.=''OpenRowset'' or .= ''SqlCommand'']

The XPath will than be:

properties/property[@name[.=''OpenRowset'' or .= ''SqlCommand'']]

huangapple
  • 本文由 发表于 2023年7月31日 22:25:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定