473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Comparing node sets

I need to compare two "address" structures within a document, and
perform some action if they are not equal.

The XML document is a purchase order, with an address at both the
header and line item level:

<Order>
<Header>
... other header level stuff ...
<Address>
<AddressLine>address 1</AddressLine>
<AddressLine>address 2</AddressLine>
<CityName>city</CityName>
<PostalCode>post code</PostalCode>
<PostalCountry>country</PostalCountry>
</Address>
</Header>
<Body>
<LineItem>
... other line item stuff ...
<Address>
<AddressLine>address 1</AddressLine>
<AddressLine>address 2</AddressLine>
<CityName>city</CityName>
<PostalCode>post code</PostalCode>
<PostalCountry>country</PostalCountry>
</Address>
</LineItem>
<LineItem>
... other line item stuff ...
<Address>
<AddressLine>somewhere different</AddressLine>
<CityName>other city</CityName>
<PostalCode>other post code</PostalCode>
<PostalCountry>country</PostalCountry>
</Address>
</LineItem>
</Body>
</Order>
For line items where the address is the same as the header level
address I need take no action. For addresses which differ I need to
populate an output attribute with a value. Therefore, ideally I'm
looking for an expression that I can use in the test clause of an "if"
element.

How can I compare the two address structures? I want "true" to mean
both structures are completely identical in element name, order and
value. In the above example the first line item's address is identical
to the header level, the second is different.

Is it possible to do this with XSLT? Can the XML be accessed in a
string format (like the .xml method of a node in DOM), so that I can
compare the resulting strings?

Thanks in advance for any help.

Doug
Jul 20 '05 #1
1 2948
Hi Doug,

You could try copying the two node-sets into two RTFs (result tree
fragments) and then comparing the two RTFs, e.g. something along the lines
of...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Order">
<xsl:variable name="setHdr">
<xsl:copy-of select="Header/Address"/>
</xsl:variable>
<xsl:variable name="setAdd1">
<xsl:copy-of select="Body/LineItem[1]/Address"/>
</xsl:variable>
<xsl:variable name="setAdd2">
<xsl:copy-of select="Body/LineItem[2]/Address"/>
</xsl:variable>
<xsl:text>(Header/Address = Body/LineItem[1]/Address) ? </xsl:text>
<xsl:value-of select="$setHdr = $setAdd1"/>
<xsl:text> </xsl:text>
<xsl:text>(Header/Address = Body/LineItem[2]/Address) ? </xsl:text>
<xsl:value-of select="$setHdr = $setAdd2"/>
</xsl:template>
</xsl:stylesheet>

That may be sufficient for what you need.
The <xsl:strip-space elements="*"/> is required in case you have white-space
preservation turned on for the input XML - otherwise the additional
indentation on the deeper addresses will be preserved and cause a result of
false for both comparisons.
Although even that may have a pit-fall - if you should have an explicit
xml:space="preserve" in the XML that preserves the whitespace indentation.
For example...

<Order xml:space="preserve">
<Header>
... other header level stuff ...
<Address>
<AddressLine>address 1</AddressLine>
<AddressLine>address 2</AddressLine>
<CityName>city</CityName>
<PostalCode>post code</PostalCode>
<PostalCountry>country</PostalCountry>
</Address>
</Header>
<Body>
<LineItem>
... other line item stuff ...
<Address>
<AddressLine>address 1</AddressLine>
<AddressLine>address 2</AddressLine>
<CityName>city</CityName>
<PostalCode>post code</PostalCode>
<PostalCountry>country</PostalCountry>
</Address>
</LineItem>
<LineItem>
... other line item stuff ...
<Address>
<AddressLine>somewhere different</AddressLine>
<CityName>other city</CityName>
<PostalCode>other post code</PostalCode>
<PostalCountry>country</PostalCountry>
</Address>
</LineItem>
</Body>
</Order>

In that case you'd need to make the stylesheet a little more complex, e.g.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="Order">
<xsl:variable name="setHdr">
<xsl:apply-templates select="Header/Address" mode="rtfcopy"/>
</xsl:variable>
<xsl:variable name="setAdd1">
<xsl:apply-templates select="Body/LineItem[1]/Address" mode="rtfcopy"/>
</xsl:variable>
<xsl:variable name="setAdd2">
<xsl:apply-templates select="Body/LineItem[2]/Address" mode="rtfcopy"/>
</xsl:variable>
<xsl:text>(Header/Address = Body/LineItem[1]/Address) ? </xsl:text>
<xsl:value-of select="$setHdr = $setAdd1"/>
<xsl:text> </xsl:text>
<xsl:text>(Header/Address = Body/LineItem[2]/Address) ? </xsl:text>
<xsl:value-of select="$setHdr = $setAdd2"/>
</xsl:template>

<xsl:template match="*" mode="rtfcopy">
<xsl:copy>
<xsl:apply-templates select="node() | @*" mode="rtfcopy"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@* | comment() | processing-instruction()"
mode="rtfcopy">
<xsl:copy/>
</xsl:template>

<xsl:template match="text()" mode="rtfcopy">
<xsl:if test="normalize-space(.)">
<xsl:copy/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

NB. the modes are only there so that the copying templates don't interfere
with any other templates you might have.

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator

"Doug" <do*********@johncrane.co.uk> wrote in message
news:62**************************@posting.google.c om...
I need to compare two "address" structures within a document, and
perform some action if they are not equal.

The XML document is a purchase order, with an address at both the
header and line item level:

<Order>
<Header>
... other header level stuff ...
<Address>
<AddressLine>address 1</AddressLine>
<AddressLine>address 2</AddressLine>
<CityName>city</CityName>
<PostalCode>post code</PostalCode>
<PostalCountry>country</PostalCountry>
</Address>
</Header>
<Body>
<LineItem>
... other line item stuff ...
<Address>
<AddressLine>address 1</AddressLine>
<AddressLine>address 2</AddressLine>
<CityName>city</CityName>
<PostalCode>post code</PostalCode>
<PostalCountry>country</PostalCountry>
</Address>
</LineItem>
<LineItem>
... other line item stuff ...
<Address>
<AddressLine>somewhere different</AddressLine>
<CityName>other city</CityName>
<PostalCode>other post code</PostalCode>
<PostalCountry>country</PostalCountry>
</Address>
</LineItem>
</Body>
</Order>
For line items where the address is the same as the header level
address I need take no action. For addresses which differ I need to
populate an output attribute with a value. Therefore, ideally I'm
looking for an expression that I can use in the test clause of an "if"
element.

How can I compare the two address structures? I want "true" to mean
both structures are completely identical in element name, order and
value. In the above example the first line item's address is identical
to the header level, the second is different.

Is it possible to do this with XSLT? Can the XML be accessed in a
string format (like the .xml method of a node in DOM), so that I can
compare the resulting strings?

Thanks in advance for any help.

Doug

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: inquirydog | last post by:
Hi- Does anyone know a way to compare whether two nodes contain the same information in xslt (the name, attributes, and all content recursivly should be the same. I am interested in the case...
2
by: Steffen Beyer | last post by:
Hi, following situation: <xsl:variable name="newsarticles"> <xsl:call-template name="find_current_newsarticles"/> </xsl:variable> <!-- $newsarticles now contains something like
3
by: Andy Fish | last post by:
Hi, I just found this template in someone else's xslt (it's Microsoft's "word2html" stylesheet to convert WordProcessingML to HTML) <xsl:template match="WX:sect"> <xsl:variable...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
11
by: John Salerno | last post by:
I'd like to compare the values in two different sets to test if any of the positions in either set share the same value (e.g., if the third element of each set is an 'a', then the test fails). I...
8
by: Jean-François Michaud | last post by:
Who in the name of #%@! thought this one out?? I noticed this behavior when trying to debug a problem I was having. I used this logical expression and some XPATH in a specific sequence of...
2
by: Yash | last post by:
Hi, We are in the process of tuning the performance of our stored procs in SQL 2000 and are looking for a tool that would help us in comparing the result sets of an old SP and a modified SP. The...
0
by: DomiNeug | last post by:
Hello, Since a while i have to find a way of comparing "Sets" (multiple int Values) and so to find equal sets. There simply 3 tables ValueList with: ID int ValueListHasValue: ID int, ValueListID...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.