etree icon indicating copy to clipboard operation
etree copied to clipboard

Feature request - the GetPath function must return the full path with the index

Open M0rdecay opened this issue 5 years ago • 1 comments

Now the GetPath function for all VHost elements will return the same path - /VHosts/VHost Document:

<VHosts>
    <VHost>
        <Name>host_1</Name>
        <TimeRunning>64679.885</TimeRunning>
	<Connections>
        	<ConnectionsLimit>0</ConnectionsLimit>
        	<ConnectionsCurrent>0</ConnectionsCurrent>
        	<ConnectionsTotal>0</ConnectionsTotal>
        	<ConnectionsTotalAccepted>0</ConnectionsTotalAccepted>
        	<ConnectionsTotalRejected>0</ConnectionsTotalRejected>
	</Connections>
        <MessagesInBytesRate>0.0</MessagesInBytesRate>
        <MessagesOutBytesRate>0.0</MessagesOutBytesRate>
    </VHost>
    <VHost>
        <Name>host_2</Name>
        <TimeRunning>67777.885</TimeRunning>
	<Connections>
	        <ConnectionsLimit>2</ConnectionsLimit>
        	<ConnectionsCurrent>2</ConnectionsCurrent>
        	<ConnectionsTotal>2</ConnectionsTotal>
        	<ConnectionsTotalAccepted>2</ConnectionsTotalAccepted>
        	<ConnectionsTotalRejected>2</ConnectionsTotalRejected>
	</Connections>
        <MessagesInBytesRate>2.2</MessagesInBytesRate>
        <MessagesOutBytesRate>2.2</MessagesOutBytesRate>
    </VHost>
</VHosts>

Request - the ability to get the path of an element in an array with the index:

/VHosts/VHost[1]
/VHosts/VHost[2]

M0rdecay avatar Sep 10 '20 09:09 M0rdecay

This is not the best quality code, but I managed to do it like this:

	npath := ""
	
	for seg := e; seg != nil; seg = seg.Parent() {
		if seg.Tag != "" {
			index := ""
		
			z := seg.Parent().FindElements(seg.Tag)
			if len(z) > 1 {
				for i, x := range z {
					if x.Index() == seg.Index() {
						index = fmt.Sprintf("[%v]", strconv.Itoa(i + 1))
					}
				}
			}
			npath = fmt.Sprintf("%v%v/%v", seg.Tag, index, npath)
		}
	}

	return fmt.Sprintf("/%v", npath)

M0rdecay avatar Sep 13 '20 12:09 M0rdecay