Xml

xmllint 用法 - 很難獲取 xml 屬性

  • February 12, 2020

我正在從 xml 文件中檢索一些數據,它實際上是一個 SSIS 封包件 (dtsx)。我已經閱讀了有關 using的資訊xmllint,但是我很難檢索到我想要的東西。我想向這裡的人們尋求一些幫助。這就是我的 xml 的樣子:

<?xml version="1.0"?>
<DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts"
 DTS:refId="Package"
 DTS:CreationDate="7/22/2019 4:18:27 PM"
 DTS:CreationName="Microsoft.Package"
 DTS:CreatorComputerName="ADMIN-8DF005D47"
 DTS:CreatorName="ADMIN-8DF005D47\Administrator"
 DTS:DTSID="{8CCA1D42-642A-4932-AAEC-E02175A4B2DB}"
 DTS:ExecutableType="Microsoft.Package"
 DTS:LastModifiedProductVersion="15.0.2000.68"
 DTS:LocaleID="1033"
 DTS:ObjectName="HardestNestedWorkflow2"
 DTS:PackageType="5"
 DTS:VersionBuild="12"
 DTS:VersionGUID="{FDD9B190-1A03-4A19-8794-FA86F4F46A93}">
 <DTS:Property
   DTS:Name="PackageFormatVersion">8</DTS:Property>
 <DTS:ConnectionManagers>
   <DTS:ConnectionManager
     DTS:refId="Package.ConnectionManagers[ADMIN-8DF005D47.AdventureWorks]"
     DTS:CreationName="OLEDB"
     DTS:DTSID="{A32A68DF-3D53-4057-AF80-1B8D524F82BC}"
     DTS:ObjectName="ADMIN-8DF005D47.AdventureWorks">
     <DTS:ObjectData>
       <DTS:ConnectionManager
         DTS:ConnectionString="Data Source=ADMIN-8DF005D47;Initial Catalog=AdventureWorks;Provider=SQLNCLI11;Integrated Security=SSPI;Application Name=SSIS-HardestNestedWorkflow2-{A32A68DF-3D53-4057-AF80-1B8D524F82BC}ADMIN-8DF005D47.AdventureWorks;Auto Translate=False;" />
     </DTS:ObjectData>

   <!--snipped-->

   </DTS:ConnectionManager>
 </DTS:ConnectionManagers>
</DTS:Executable>

我希望從這條線中獲得值“ 8 ”

<DTS:Property
   DTS:Name="PackageFormatVersion">8</DTS:Property>

所以,我所做的是發出命令

xmllint --xpath "//*[local-name()='Executable']/*[local-ame()='Property']/text()" HardestNestedWorkflow2_latest.xml

..這很好,我能夠提取值“8”

現在,我想提取價值

Data Source=ADMIN-8DF005D47;Initial Catalog=AdventureWorks;Provider=SQLNCLI11;Integrated Security=SSPI;Application Name=SSIS-HardestNestedWorkflow2-{A32A68DF-3D53-4057-AF80-1B8D524F82BC}ADMIN-8DF005D47.AdventureWorks;Auto Translate=False;

我發表了這份聲明

xmllint --xpath "//*[local-name()='Executable']/*[local-name()='ConnectionManagers']/*[local-name()='ConnectionManager']/*[local-name()='ObjectData']/*[local-name()='ConnectionManager']" HardestNestedWorkflow2_latest.xml

它返回了這個

<DTS:ConnectionManager DTS:ConnectionString="Data Source=ADMIN-8DF005D47;Initial Catalog=AdventureWorks;Provider=SQLNCLI11;Integrated Security=SSPI;Application Name=SSIS-HardestNestedWorkflow2-{A32A68DF-3D53-4057-AF80-1B8D524F82BC}ADMIN-8DF005D47.AdventureWorks;Auto Translate=False;"/>

我怎樣才能省略標籤DTS:ConnectionManager DTS:ConnectionString?對不起,我對使用xmllint.

提前致謝

您已經找到了元素,但您需要屬性@DTS:ConnnectionString的值而不是元素本身的值。@ConnectionString如果沒有命名空間,xmllint您會選擇後綴為@*[local-name()='ConnectionString']. 此外,為了從結果的前面去除屬性名稱,您需要使用string(). 醜陋。

xmllint --xpath "string(//*[local-name()='Executable']/*[local-name()='ConnectionManagers']/*[local-name()='ConnectionManager']/*[local-name()='ObjectData']/*[local-name()='ConnectionManager']/@*[local-name()='ConnectionString'])" file.xml

輸出

Data Source=ADMIN-8DF005D47;Initial Catalog=AdventureWorks;Provider=SQLNCLI11;Integrated Security=SSPI;Application Name=SSIS-HardestNestedWorkflow2-{A32A68DF-3D53-4057-AF80-1B8D524F82BC}ADMIN-8DF005D47.AdventureWorks;Auto Translate=False;

我個人更喜歡xmlstarlet這類操作,因為它更清楚地理解命名空間,但最終最好使用最方便的工具。

xmlstarlet sel xmlns:DTS="www.microsoft.com/SqlServer/Dts" -t -v '/DTS:Executable/DTS:Property' -n file.xml
8

xmlstarlet sel xmlns:DTS="www.microsoft.com/SqlServer/Dts" -t -v '/DTS:Executable//DTS:ConnectionManager/@DTS:ConnectionString' -n file.xml
Data Source=ADMIN-8DF005D47;Initial Catalog=AdventureWorks;Provider=SQLNCLI11;Integrated Security=SSPI;Application Name=SSIS-HardestNestedWorkflow2-{A32A68DF-3D53-4057-AF80-1B8D524F82BC}ADMIN-8DF005D47.AdventureWorks;Auto Translate=False;

引用自:https://unix.stackexchange.com/questions/567102