XPath为以下条件

时间:2013-09-19 10:54:21

标签: xml xpath

假设我有给定的XML

<StyleML>
<transaction>
<party>
<partyId>Party1</partyId>
<partyAlias>
    <partyAliasId>2323</partyAliasId>
    <partyAliasLongName>AQR LP</partyAliasLongName>
     </partyAlias>
     </party>
<party>
<partyId>Party2</partyId>
<partyAlias>
     <partyAliasId>2323</partyAliasId>
     <partyAliasLongName>Scotland</partyAliasLongName>
</partyAlias>
</party>
     <transactionParticipant>
          <counterpartyRef>Party2</counterpartyRef>
          <participantType>Counterparty Participant</participantType>
     </transactionParticipant>
     <transactionParticipant>
         <counterpartyRef>Party3</counterpartyRef>
         <participantType>Broker Participant</participantType>
     </transactionParticipant>
</transaction>
</StyleML>

现在我想将两个XPath值填充为单个逗号分隔值。

例如。我想填写2323,AQR LP,即[/ styleml / transaction / party / partyAlias / partyAliasId,/ styleml / transaction / party / partyAlias / partyAliasLongName]

我试过了|运算符加入XPath结果,但它只填充第一个xpath数据作为最终结果。

1 个答案:

答案 0 :(得分:1)

修复

请务必尊重标记案例(styelmlStyleML)。

使用or

的解决方案

我会像XPath to select multiple tags中描述的那样:

/StyleML/transaction/party/partyAlias/*[self::partyAliasId or self::partyAliasLongName]

使用|saxon

的解决方案

使用saxon XQuery,您可以使用/path/(a|b)语法:

java -cp "/path/to/saxon9ee.jar" net.sf.saxon.Query -s:"/path/to/file.xml" \
-qs:"/StyleML/transaction/party/partyAlias/(partyAliasId|partyAliasLongName)"

输出:

<?xml version="1.0" encoding="UTF-8"?>
  <partyAliasId>2323</partyAliasId><partyAliasLongName>AQR LP</partyAliasLongName>
  <partyAliasId>2323</partyAliasId><partyAliasLongName>Scotland</partyAliasLongName>