使用XPath查询读取XML标签
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用XPath查询读取XML标签相关的知识,希望对你有一定的参考价值。
我需要通过XPath查询从我在这里报告的XML文件中推断出一系列值。
我声明我对该主题知之甚少,因此,我对技术术语的错误表示歉意。经过几次尝试,我设法获得了一些有用的数据,例如在Idc-> FileGroup-> value节点中报告的数据。与查询
//m:meta[2]/@value
根据索引1、2、3等返回TAGs“值”的不同值。
尽管进行了多次尝试,但是我无法创建一个查询,该查询允许我获取以下TAG的值:Idc-> FileGroup-> File s:format-> Path
对应于docs/002678E3.pdf
,来自TAG
IdC-> SelfDescription-ID s;
方案,对应于
1520038843024
或者对我来说一样,
"local">1520038843024
您能给我帮助吗?这是我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<IdC xmlns="http://www.uni.com/U3011/sincro/" xmlns:s="http://www.uni.com/U3011/sincro/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.uni.com/U3011/sincro/ UNI1138600_2010_EIT.xsd http://andxor.it/tDoc/lotto.xsd lotto.xsd http://andxor.it/tDoc/metadata.xsd metadata.xsd">
<SelfDescription>
<ID s:scheme="local">1520038843024</ID>
<CreatingApplication>
<Name>tDoc</Name>
<Version>r16610</Version>
<Producer>Andxor Soluzioni Informatiche srl</Producer>
</CreatingApplication>
<MoreInfo s:XMLScheme="http://andxor.it/tDoc/lotto.xsd">
<EmbeddedMetadata>
<lotto xmlns="http://andxor.it/tDoc/lotto.xsd">
<company>SCOTT</company>
<doctype>FATTURE_ATTIVE</doctype>
<period>2018</period>
<previous id="1519174898489">ED1C84CE3A825268E8434EFB80B9D09B4E0E9658F7A4D4FF5005CE6D10293388</previous>
</lotto>
</EmbeddedMetadata>
</MoreInfo>
</SelfDescription>
<VdC>
<ID s:scheme="local">1520038843024</ID>
</VdC>
<FileGroup xmlns:m="http://andxor.it/tDoc/metadata.xsd">
<File s:format="application/pdf">
<ID s:scheme="local">2521309</ID>
<Path>docs/002678DD.pdf</Path>
<Hash s:function="SHA-256">A041E222FC3F968EAED07405D9D8A32CD2A8BDB1952E8B726DDE3B7F55F20EE0</Hash>
<MoreInfo s:XMLScheme="http://andxor.it/tDoc/metadata.xsd">
<EmbeddedMetadata>
<m:metadata>
<m:meta class="internal" name="Data inserimento" value="2018-02-16T18:40:12+01:00"/>
<m:meta name="Numero Fattura" value="970081952"/>
<m:meta name="Data" value="2018-02-01"/>
<m:meta name="Ragione Sociale" value="SCOTT ITALIA AUTOFATTURA CORRISPETT"/>
<m:meta name="Codice Cliente" value="140422"/>
<m:meta name="Partita Iva" value="IT03770200164"/>
</m:metadata>
</EmbeddedMetadata>
</MoreInfo>
</File>
<File s:format="application/pdf">
<ID s:scheme="local">2521315</ID>
<Path>docs/002678E3.pdf</Path>
<Hash s:function="SHA-256">6FF865D8ECA154E31D9E362B12E11EA79CE04EB92EB558C7C0AAC32A5883156F</Hash>
<MoreInfo s:XMLScheme="http://andxor.it/tDoc/metadata.xsd">
<EmbeddedMetadata>
<m:metadata>
<m:meta class="internal" name="Data inserimento" value="2018-02-16T18:40:18+01:00"/>
<m:meta name="Numero Fattura" value="970081953"/>
<m:meta name="Data" value="2018-02-02"/>
<m:meta name="Ragione Sociale" value="SCOTT ITALIA AUTOFATTURA CORRISPETT"/>
<m:meta name="Codice Cliente" value="140422"/>
<m:meta name="Partita Iva" value="IT03770200164"/>
</m:metadata>
</EmbeddedMetadata>
</MoreInfo>
</File>
<File s:format="application/pdf">
<ID s:scheme="local">2521321</ID>
<Path>docs/002678E9.pdf</Path>
<Hash s:function="SHA-256">0F3755FA764DD00AA35D801908A4FEE9762C259CE18E2DC6BFAD50C49B013422</Hash>
<MoreInfo s:XMLScheme="http://andxor.it/tDoc/metadata.xsd">
<EmbeddedMetadata>
<m:metadata>
<m:meta class="internal" name="Data inserimento" value="2018-02-16T18:40:22+01:00"/>
<m:meta name="Numero Fattura" value="970081954"/>
<m:meta name="Data" value="2018-02-05"/>
<m:meta name="Ragione Sociale" value="SCOTT ITALIA AUTOFATTURA CORRISPETT"/>
<m:meta name="Codice Cliente" value="140422"/>
<m:meta name="Partita Iva" value="IT03770200164"/>
</m:metadata>
</EmbeddedMetadata>
</MoreInfo>
</File>
<File s:format="application/pdf">
<ID s:scheme="local">2521335</ID>
<Path>docs/002678F7.pdf</Path>
<Hash s:function="SHA-256">F3FE57B26F49065BCAEEC8B5B7DB58B8F9AE0E93ED692D8771D9116F5F6472C0</Hash>
<MoreInfo s:XMLScheme="http://andxor.it/tDoc/metadata.xsd">
<EmbeddedMetadata>
<m:metadata>
<m:meta class="internal" name="Data inserimento" value="2018-02-16T18:41:14+01:00"/>
<m:meta name="Numero Fattura" value="970081955"/>
<m:meta name="Data" value="2018-02-06"/>
<m:meta name="Ragione Sociale" value="SCOTT ITALIA AUTOFATTURA CORRISPETT"/>
<m:meta name="Codice Cliente" value="140422"/>
<m:meta name="Partita Iva" value="IT03770200164"/>
</m:metadata>
</EmbeddedMetadata>
</MoreInfo>
</File>
</FileGroup>
<Process>
<Agent s:type="organization" s:role="PreservationManager">
<AgentName>
<FormalName>KPNQwest Italia SpA</FormalName>
</AgentName>
<Agent_ID s:scheme="TaxCode">07957860153</Agent_ID>
</Agent>
<Agent s:type="person" s:role="PreservationManager">
<AgentName>
<NameAndSurname>
<FirstName>Giuseppe</FirstName>
<LastName>Baccanelli</LastName>
</NameAndSurname>
</AgentName>
<Agent_ID s:scheme="TaxCode">BCCGPP64C01A940D</Agent_ID>
</Agent>
<Agent s:type="person" s:role="PreservationManager">
<AgentName>
<NameAndSurname>
<FirstName>Giuseppe</FirstName>
<LastName>Baccanelli</LastName>
</NameAndSurname>
</AgentName>
<Agent_ID s:scheme="TaxCode">BCCGPP64C01A940D</Agent_ID>
</Agent>
<Agent s:type="organization" s:role="OtherRole" s:otherRole="Other role">
<AgentName>
<FormalName>Amministratore</FormalName>
</AgentName>
</Agent>
<TimeReference>
<TimeInfo>2018-03-03T02:00:43+01:00</TimeInfo>
</TimeReference>
<LawAndRegulations s:language="it">DPCM 3 dicembre 2013</LawAndRegulations>
</Process>
</IdC>
答案
您正在寻找的XPath-1.0表达式可能是
First:“创建一个查询,使我能够获取以下TAG的值”:
IdC-> FileGroup-> File s:格式->路径
简单
/IdC/FileGroup/File[@s:format]/Path
如果必须显式设置名称空间,则可以使用
/std:IdC/std:FileGroup/std:File[@s:format='application/pdf']/std:Path
名称空间设置为
xmlns:std = "http://www.uni.com/U3011/sincro/" xmlns:s = "http://www.uni.com/U3011/sincro/"
或者,如果您根本无法设置名称空间(由于任何原因),请使用
/*[name()='IdC']/*[name()='FileGroup']/*[name()='File'][@*[local-name()='format' and .='application/pdf']]/*[name()='Path']
在所有三种情况下的结果都是
docs / 002678DD.pdf
要检索的值
IdC-> SelfDescription-> ID
使用这些XPath-1.0表达式:
具有上述名称空间:
/std:IdC/std:SelfDescription/std:ID
没有以上名称空间:
/*[name()='IdC']/*[name()='SelfDescription']/*[name()='ID']
两种情况下的结果:
1520038843024
希望这会有所帮助。
以上是关于使用XPath查询读取XML标签的主要内容,如果未能解决你的问题,请参考以下文章
使用XPath查询带有命名空间(有xmlns)的XML(转)