提问人:nikhil 提问时间:11/1/2023 最后编辑:halfernikhil 更新时间:11/23/2023 访问量:88
root.findall() 在动态指定命名空间时找不到任何内容
root.findall() doesn't find anything when specifying the namespace dynamically
问:
我正在阅读一些 XML 文件,其中每个文件的命名空间都不同。问题是,当我说 和 说 root.findall 时,它找不到任何东西。另一方面,如果我指定实际的命名空间字符串,而不是 root.tag,例如“https://schemas....”无论如何,findall 正在工作。ns={"x":root.tag}
这是我的代码,请看ns={"x":root.tag}
def parse_csprojForNuGetPackages(repository_root, repository_name, source):
try:
p=os.path.join(repository_root,repository_name,source)
with open(p,encoding='utf-8-sig') as f:
file_content=f.read().replace('\n','')
root=ET.fromstring(str(file_content))
except Exception as ex:
return None
# Extract the list of NuGet packages that are used by the csproj file.
print(root.tag)
ns={"x":root.tag}
for element in root.findall('.//x:PackageReference',ns): #I want all PackageReference elements with its attributes in the xml file.
ref = PackageReference(
ecosystem = 'NuGet',
repository_root = repository_root,
repository_name = repository_name,
source = source,
package_name = element.attrib['Include'],
version = element.find('.//x:Version',ns).text) #element.attrib['Version'])
refs.append(ref) #Get all the Nuget Packages.
return refs
例如,一些 xml 可以像
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.ServiceModel.Duplex" Version="4.8" />
<PackageReference Include="System.ServiceModel.Http" Version="4.8" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.8" />
<PackageReference Include="System.ServiceModel.Primitives" Version="4.8" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.ServiceModel" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>
</Project>
或者它可以像
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{F3B4CDC7-94F8-40BF-AB48-9920A873A6DD}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Labcyte.Core.Server</RootNamespace>
<AssemblyName>CoreServer</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<IsWebBootstrapper>false</IsWebBootstrapper>
<TargetFrameworkProfile />
<SccProjectName>
</SccProjectName>
<SccLocalPath>
</SccLocalPath>
<SccAuxPath>
</SccAuxPath>
<SccProvider>
</SccProvider>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;-c -s</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup>
<StartupObject>
</StartupObject>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug-Linux|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug-Linux\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug-Linux|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug-Linux\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-Linux|AnyCPU'">
<OutputPath>bin\Release-Linux\</OutputPath>
<DefineConstants>TRACE;-c -s</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-Linux|x86'">
<OutputPath>bin\x86\Release-Linux\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CmdProcessor.cs" />
<Compile Include="Config.cs" />
<Compile Include="Host.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="SSLCommandProcessor.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config">
<SubType>Designer</SubType>
</None>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<None Include="SimulationData\3FeatureFullCapture1pt20V.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
<Visible>False</Visible>
<ProductName>Windows Installer 3.1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Common\Labcyte.Core.Daq\Labcyte.Core.Daq.csproj">
<Project>{1b8c9428-1378-46c4-9bca-2e5d5365e256}</Project>
<Name>Labcyte.Core.Daq</Name>
</ProjectReference>
<ProjectReference Include="..\..\Common\Labcyte.Core.Interface\Labcyte.Core.Interface.csproj">
<Project>{680bc720-71fa-4377-8b31-50267a538d5f}</Project>
<Name>Labcyte.Core.Interface</Name>
</ProjectReference>
<ProjectReference Include="..\..\Common\Labcyte.Core.IScript\Labcyte.Core.IScript.csproj">
<Project>{4f92afb1-000b-44e1-9823-98de10ce72b0}</Project>
<Name>Labcyte.Core.IScript</Name>
</ProjectReference>
<ProjectReference Include="..\..\Common\Labcyte.Core.Math\Labcyte.Core.NumericalMethods.csproj">
<Project>{d4bf4690-9549-4527-816b-39518618e611}</Project>
<Name>Labcyte.Core.NumericalMethods</Name>
</ProjectReference>
<ProjectReference Include="..\..\Common\Labcyte.Core.NLog\Labcyte.Core.NLog.csproj">
<Project>{6e4ac4fd-b91d-4544-8d72-e502d1db7c51}</Project>
<Name>Labcyte.Core.NLog</Name>
</ProjectReference>
<ProjectReference Include="..\..\Common\Labcyte.Core.Script\Labcyte.Core.Script.csproj">
<Project>{d8b06bbf-43c1-4e83-bb1f-0c8dce5470ca}</Project>
<Name>Labcyte.Core.Script</Name>
</ProjectReference>
<ProjectReference Include="..\..\Common\Labcyte.License\Labcyte.License.csproj">
<Project>{cc14afbb-6358-4358-a87a-a8219df220aa}</Project>
<Name>Labcyte.License</Name>
</ProjectReference>
<ProjectReference Include="..\Labcyte.Core.Hardware\Labcyte.Core.Hardware.csproj">
<Project>{578923d9-359d-4754-ae49-9d9d5ff40be5}</Project>
<Name>Labcyte.Core.Hardware</Name>
</ProjectReference>
<ProjectReference Include="..\Labcyte.Core.Instrument\Labcyte.Core.Instrument.csproj">
<Project>{236787d2-8406-4072-b25e-9d1d36f77827}</Project>
<Name>Labcyte.Core.Instrument</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="Properties\BuildDateTime.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Mono.Posix">
<Version>4.0.0</Version>
</PackageReference>
<PackageReference Include="System.IO">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.Algorithms">
<Version>4.3.1</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>$(SolutionDir)..\..\ThirdParty\Mono.Unofficial.pdb2mdb\4.2.3.4\tools\pdb2mdb.exe $(TargetDir)CoreServer.exe
$(SolutionDir)..\..\ThirdParty\Mono.Unofficial.pdb2mdb\4.2.3.4\tools\pdb2mdb.exe $(TargetDir)Labcyte.Core.Daq.dll
$(SolutionDir)..\..\ThirdParty\Mono.Unofficial.pdb2mdb\4.2.3.4\tools\pdb2mdb.exe $(TargetDir)Labcyte.Core.Hardware.dll
$(SolutionDir)..\..\ThirdParty\Mono.Unofficial.pdb2mdb\4.2.3.4\tools\pdb2mdb.exe $(TargetDir)Labcyte.Core.Instrument.dll
$(SolutionDir)..\..\ThirdParty\Mono.Unofficial.pdb2mdb\4.2.3.4\tools\pdb2mdb.exe $(TargetDir)Labcyte.Core.Interface.dll
$(SolutionDir)..\..\ThirdParty\Mono.Unofficial.pdb2mdb\4.2.3.4\tools\pdb2mdb.exe $(TargetDir)Labcyte.Core.NLog.dll
$(SolutionDir)..\..\ThirdParty\Mono.Unofficial.pdb2mdb\4.2.3.4\tools\pdb2mdb.exe $(TargetDir)Labcyte.Core.Script.dll
copy $(SolutionDir)\..\Installers\CoreServer\Images\rootfs\public\Labcyte.pfx $(TargetDir)
rmdir /S /Q "$(SolutionDir)..\..\Library\Core\CoreServer\"
mkdir "$(SolutionDir)..\..\Library\Core\CoreServer\"
copy "$(TargetDir)*.dll" "$(SolutionDir)..\..\Library\Core\CoreServer\"
copy "$(TargetDir)*.pdb" "$(SolutionDir)..\..\Library\Core\CoreServer\"
</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PreBuildEvent>del /F "$(ProjectDir)\Properties\BuildDateTime.txt"
echo %25date%25 %25time%25 > "$(ProjectDir)\Properties\BuildDateTime.txt"</PreBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
答: 暂无答案
评论