Javascript Xpath:如果存在多个子对象,请选择多个父对象

Javascript Xpath:如果存在多个子对象,请选择多个父对象,javascript,xml,xpath,Javascript,Xml,Xpath,我在将xml文件的多个值放入html表时遇到问题 我希望使用以下xpath表达式获得具有tcp.TcpDispatcherProperties的destinationConnectors属性的所有父级的名称: "/serverConfiguration/channels/channel[destinationConnectors/connector/properties[@class='com.mirth.connect.connectors.tcp.TcpDispatcherPropertie

我在将xml文件的多个值放入html表时遇到问题

我希望使用以下xpath表达式获得具有tcp.TcpDispatcherProperties的destinationConnectors属性的所有父级的名称:

"/serverConfiguration/channels/channel[destinationConnectors/connector/properties[@class='com.mirth.connect.connectors.tcp.TcpDispatcherProperties']]/name"
用这句话:

父级仅显示一次,即使该类有多个目的地

问题是,如果有多个DestinationConnector[1..n],我只能在html表中从//channels/name获取一次父级

例如,如果我有3个通道,我想用Javascript和xml.evaluate显示什么?通道1有1个目标,通道2有2个目标:

------------  -------------  -------------
Channel1Name | Channel2Name | Channel2Name
------------  -------------  -------------
Destination1  Destination2a  Destination2b
我想要的输出是:

<channelGroup version="3.4.2">
  <id>e03a0301-4e29-405e-8ee0-f7a47e30d033</id>
  <channels>
    <channel version="3.4.2">
      <id>e4dccb26-b645-4245-bb7e-67e1e4901cd2</id>
      <name>Channel1</name>
      <destinationConnectors>
        <connector version="3.4.2">
          <name>Destination 1</name>
          <properties class="com.mirth.connect.connectors.tcp.TcpDispatcherProperties" version="3.4.2">
            <pluginProperties/>
          </properties>
        </connector>
      </destinationConnectors>
    </channel>
    <channel version="3.4.2">
      <id>b33ea541-e723-47a6-af52-e76648c5674e</id>
      <name>Channel2</name>
      <destinationConnectors>
        <connector version="3.4.2">
          <name>Destination 2a</name>
          <properties class="com.mirth.connect.connectors.tcp.TcpDispatcherProperties" version="3.4.2">
            <pluginProperties/>
          </properties>
        </connector>
        <connector version="3.4.2">
          <name>Destination 2b</name>
          <properties class="com.mirth.connect.connectors.tcp.TcpDispatcherProperties" version="3.4.2">
            <pluginProperties/>
          </properties>
        </connector>
      </destinationConnectors>
    </channel>
  </channels>
</channelGroup>
  • 来自“/serverConfiguration/channels/channel/name”的Channel1Name
  • 来自“/serverConfiguration/channels/channel/name”的Channel2Name
  • 来自“/serverConfiguration/channels/channel/name”的Channel2Name
不是

  • 通道1名称
  • 通道2名称

XML摘录:

<channelGroup version="3.4.2">
  <id>e03a0301-4e29-405e-8ee0-f7a47e30d033</id>
  <channels>
    <channel version="3.4.2">
      <id>e4dccb26-b645-4245-bb7e-67e1e4901cd2</id>
      <name>Channel1</name>
      <destinationConnectors>
        <connector version="3.4.2">
          <name>Destination 1</name>
          <properties class="com.mirth.connect.connectors.tcp.TcpDispatcherProperties" version="3.4.2">
            <pluginProperties/>
          </properties>
        </connector>
      </destinationConnectors>
    </channel>
    <channel version="3.4.2">
      <id>b33ea541-e723-47a6-af52-e76648c5674e</id>
      <name>Channel2</name>
      <destinationConnectors>
        <connector version="3.4.2">
          <name>Destination 2a</name>
          <properties class="com.mirth.connect.connectors.tcp.TcpDispatcherProperties" version="3.4.2">
            <pluginProperties/>
          </properties>
        </connector>
        <connector version="3.4.2">
          <name>Destination 2b</name>
          <properties class="com.mirth.connect.connectors.tcp.TcpDispatcherProperties" version="3.4.2">
            <pluginProperties/>
          </properties>
        </connector>
      </destinationConnectors>
    </channel>
  </channels>
</channelGroup>

E03A031-4e29-405e-8ee0-f7a47e30d033
e4dccb26-b645-4245-bb7e-67e1e4901cd2
渠道1
目的地1
b33ea541-e723-47a6-af52-e76648c5674e
渠道2
目的地2a
目的地2b
Javascript查询:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        showResult(xhttp.responseXML,"/serverConfiguration/channels/channel/destinationConnectors/connector/properties[@class='com.mirth.connect.connectors.tcp.TcpDispatcherProperties']/concat(../../../name, ' : ',  ../name)", "destTcpChannelName");
    }
};
xhttp.open("GET", "edited/23-07-2019-MirthBackup.xml", true);
xhttp.send();
function showResult(xml, path, head) {
    var txt = "<th>" + head +"</th><tr>";
    if (xml.evaluate) {
       var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
       var result = nodes.iterateNext();
       while (result) {
          txt += "<td>" + result.childNodes[0].nodeValue + "</td>";
          result = nodes.iterateNext();
       }
    }
    document.getElementById(head).innerHTML = txt + "</tr>";
}
var xhttp=new-XMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
showResult(xhttp.responseXML,“/serverConfiguration/channels/channel/destinationConnectors/connector/properties[@class='com.mirth.connect.connectors.tcp.TcpDispatcherProperties']]/concat(../../../../name',:',../name)”,“destTcpChannelName”);
}
};
xhttp.open(“GET”,“edited/23-07-2019-MirthBackup.xml”,true);
xhttp.send();
函数showResult(xml、路径、头){
var txt=“”+头+”;
if(xml.evaluate){
var nodes=xml.evaluate(路径,xml,null,XPathResult.ANY_类型,null);
var result=nodes.iterateNext();
while(结果){
txt+=“”+结果。子节点[0]。节点值+“”;
结果=nodes.iterateNext();
}
}
document.getElementById(head.innerHTML=txt+“”;
}

以下xpath查询返回您需要的内容:

//channel/destinationConnectors/connector/properties[@class='com.mirth.connect.connectors.tcp.TcpDispatcherProperties']/concat(../../../name, ' : ',  ../name)
说明:

  • //频道

    对于任何位置的每个
    频道
    元素

  • /destinationConnectors/connector

    通过
    destinationConnectors/connector/

  • /properties[@class='com.mirth.connect.connectors.tcp.TcpDispatcherProperties']

    使用属性
    =
    c.m.c.c.t.TcpDispatcherProperties选择找到的每个
    属性

  • /concat(../../../name,':',../name)

    concat()

将返回:

Channel1 : Destination 1
Channel2 : Destination 2a
Channel2 : Destination 2b

更新的示例:

示例XML将帮助我们帮助您:)同时,双[]看起来可疑。似乎您只需一个[]就可以实现相同的行为。您好,我将在问题中插入xml文件的摘录:)感谢guido编辑我的文章。我应该澄清一下,我需要的是频道名称,而不是连接器名称。所需输出:Channel1Name,Channel2Name,Channel2Name,而不是我问题中带有xpath示例的输出:Channel1Name,Channel2Name如果我使用
//channel/destinationConnectors/connector/properties[@class='com.mirth.connector.tcp.TcpDispatcherProperties']/../../../../name
我得到的结果与
///channel[destinationConnectors/connector/properties[@class='com.mirth.connect.connectors.tcp.TcpDispatcherProperties']/name
ok,似乎在xpather中工作,但在我的代码中我得到了一个异常:
Uncaught DOMException:未能对“Document”执行“evaluate”:字符串“/serverConfiguration/channels/channel/destinationConnectors/connector/properties[@class='com.mirth.connector.tcp.TcpDispatcherProperties']]/concat(../../../../../../name,:,../name)'不是有效的XPath表达式。
我使用Javascript和xml.evaluateyeah要在路径表达式的右侧有concat()(
/
),您需要XPath 2。您可以使用Saxon JS,或者因为您使用的是一种语言,请写下XPath查询的逻辑