我有一个文件,其中 ReferenceId="0128140321960O"
还有RelatedReferenceId="SWCC"
<FinancialTransfersInstruction xmlns="middleware.frk.com/Schemas/Onetis/1.0">
<SWIFTHeader DeliveryMethod="SWIFT" MessageType="202" OrigSendDate="2019-11-15" Receiver="SBOSUS3UIMS" ReferenceId="0207060050180O" Sender="TESTBIC" SenderName="TEST INVESTORS, INC" TransType="NEW"/>
<PortalHeader BusinessUnit="GTS"/> <Wires> <Wire Currency="USD" Id="20706s5018.0" RelatedReferenceId="CCPM" </Wire> </Wires>
<FinancialTransfersInstruction/></FinancialTransfersInstruction>
当我运行命令来提取值时,我的结果也为我提供了相关参考;我只想要 ReferenceId 标签值。
cat *.xml |grep -P -o -e '(?<=ReferenceId=").*?(?=")'
答案1
您问题中的 XML 无效。假设您更正后的 XML 实际上是这样的,
<?xml version="1.0"?>
<FinancialTransfersInstruction xmlns="https://middleware.frk.com/Schemas/Onetis/1.0">
<SWIFTHeader DeliveryMethod="SWIFT" MessageType="202" OrigSendDate="2019-11-15" Receiver="SBOSUS3UIMS" ReferenceId="0207060050180O" Sender="TESTBIC" SenderName="TEST INVESTORS, INC" TransType="NEW"/>
<PortalHeader BusinessUnit="GTS"/>
<Wires>
<Wire Currency="USD" Id="20706s5018.0" RelatedReferenceId="CCPM"/>
</Wires>
</FinancialTransfersInstruction>
您可以使用它xmlstarlet
来解析 XML 并提取适当的属性值
xmlstarlet sel -t -v '//_:SWIFTHeader/@ReferenceId' bankfile.xml
0207060050180O