I have the following XML...

<TechnicalMeterDetails>
<RemovedMeterRegisters MeterCategoryCode="RM067">
</RemovedMeterRegisters>
<RemovedMeterRegisters MeterCategoryCode="RM066">
</RemovedMeterRegisters>
<NewMeterRegisters MeterCategoryCode="RM068">
</NewMeterRegisters>
<NewMeterRegisters MeterCategoryCode="RM067">
</NewMeterRegisters>
</TechnicalMeterDetails>

I want to check that the MeterCategoryCode value in the RemovedMeterRegisters segments is the same value in the NewMeterRegisters segments.

In the above example the answer would be false.

If RM068 was RM066 then the answer would be true.

The order of MeterCategoryCode values in each segments could be different as shown in the example above.

I want to represent this in a single XPATH statement or comparing 2 XPATH statements.

Anyone with any ideas, thanks in advance?

Recommended Answers

All 5 Replies

if in RemovedMeterRegisters is worth one
and is also unique is NewMeterRegisters
then you can do the following

count(//@MeterCategoryCode[contains(.,'RM067')])

is the result of 1 or 0, there is the desired value is zero or no time
if that result is greater than 1 then the value sought more time indoors

other way
result which is double

//TechnicalMeterDetails/RemovedMeterRegisters[@MeterCategoryCode = ../NewMeterRegisters/@MeterCategoryCode]

result can be node set when many double inside xml file

when search by value

//TechnicalMeterDetails/RemovedMeterRegisters[@MeterCategoryCode = ../NewMeterRegisters/@MeterCategoryCode][@MeterCategoryCode='RM067']//@MeterCategoryCode

result ist the search attribut

Junior

Thanks for the reply.

I think I need to expand on what I am after a bit more clearly as I am not sure what you have stated is what I am after.

I am trying to determine whether the MeterCategoryCodes in the NewMeterRegisters nodes exist in the RemovedMeterRegisters nodes. The values in the MeterCategoryCodes can be anything so just looking for the values I have supplied in the example will not work for all values. We know there will always be an equal number of NewMeterRegisters and RemovedMeterRegisters nodes. The MeterCategoryCodes values from the NewMeterRegisters nodes could appear in any position in the RemovedMeterRegisters nodes as indicated in the example provided.

I want to be able to determine this using XPATH.

use case 2
Sun gets all the attributes found in both cases

i make a xsl example so you can what xpath is doing
xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes" method="xml"/>
	<xsl:template match="/">
		<root>
			<xsl:apply-templates select="TechnicalMeterDetails"/>
		</root>
	</xsl:template>
	<xsl:template match="TechnicalMeterDetails">
		<xsl:for-each select="RemovedMeterRegisters[@MeterCategoryCode = ../NewMeterRegisters/@MeterCategoryCode]">
			<item>
				<xsl:value-of select="./@MeterCategoryCode"/>
			</item>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

so i make a xml file which use xsl file for testing
start xml in browser
in the same dir a xsl und xml installed

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="TechnicalMeterDetails.xsl" type="text/xsl" ?>
<TechnicalMeterDetails>
	<RemovedMeterRegisters MeterCategoryCode="RM067">
	</RemovedMeterRegisters>
	<RemovedMeterRegisters MeterCategoryCode="RM066">
	</RemovedMeterRegisters>
	<RemovedMeterRegisters MeterCategoryCode="RM065">
	</RemovedMeterRegisters>
	<RemovedMeterRegisters MeterCategoryCode="RM064">
	</RemovedMeterRegisters>
	<NewMeterRegisters MeterCategoryCode="RM068">
	</NewMeterRegisters>
	<NewMeterRegisters MeterCategoryCode="RM067">
	</NewMeterRegisters>
	<NewMeterRegisters MeterCategoryCode="RM065">
	</NewMeterRegisters>
	<NewMeterRegisters MeterCategoryCode="RM064">
	</NewMeterRegisters>
</TechnicalMeterDetails>

you the result

<?xml version='1.0' ?>
<root>
  <item>RM067</item>
  <item>RM065</item>
  <item>RM064</item>
</root>

you in my example a three doubles a found

xml_looser

Thanks for the reply.

I have tried out what you suggested and have some success but not exactly what I am looking for. I think we are going in the right direction.

The solution

//TechnicalMeterDetails/RemovedMeterRegisters[@MeterCategoryCode = ../NewMeterRegisters/@MeterCategoryCode]

will return True if any of the MeterCategoryCode attributes match.

What I am after is True if all the MeterCategoryCode attributes match. You can assume that there are the same number of RemovedMeterRegisters and NewMeterRegisters segments.

I wanted to xslt only show what is found with xpath

XPath is a description to find the node in the xml file
you're right with your solution

//TechnicalMeterDetails/RemovedMeterRegisters[@MeterCategoryCode = ../NewMeterRegisters/@MeterCategoryCode]

I select the node with the attribute selected

<xsl:template match="TechnicalMeterDetails">
		<xsl:for-each select="RemovedMeterRegisters[@MeterCategoryCode = ../NewMeterRegisters/@MeterCategoryCode]">
			<item>
				<xsl:value-of select="./@MeterCategoryCode"/>
			</item>
		</xsl:for-each>
	</xsl:template>

it could be let go with your solution

the solution then refers to the root node

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.