Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to ignore the sort order of the Nodes and attributes #187

Closed
CKUser123 opened this issue Jun 11, 2020 · 4 comments
Closed

Unable to ignore the sort order of the Nodes and attributes #187

CKUser123 opened this issue Jun 11, 2020 · 4 comments
Labels

Comments

@CKUser123
Copy link

CKUser123 commented Jun 11, 2020

I am unable to ignore the ordering of the nodes and the sub nodes.. could you Please help

XML1

<TEST/>
<Section ColumnCount="0" Description="test" ExpandedIndicator="false" SectionCode="code1">
	<Group DependencyID="NONE" Description="asdf" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group1" GroupType="CheckBox">
		<Item DefaultItem="false" Description="YES" ItemCode="YES"/>
		<Item DefaultItem="true" Description="NO" ItemCode="NO"/>
	</Group>
</Section>
<Section ColumnCount="1" Description="jkfgkghjfgh" ExpandedIndicator="true" SectionCode="code3">
	<Group DependencyID="NONE" Description="asdf" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="Group2" GroupType="DropBox">
		<Item DefaultItem="true" Description="Retail - No asdf" ItemCode="Item1"/>
		<Item DefaultItem="false" Description="asdf asdf" ItemCode="Item2"/>
		<Item DefaultItem="false" Description="asdf &asdf; Conventions" ItemCode="Item3"/>
	</Group>
	<Group DependencyID="NONE" DisplayIndicator="false" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group3">
		<Item DefaultItem="false" Description="dfgdfg" ItemCode="Item4"/>
		<Item DefaultItem="false" Description="dfgert" ItemCode="Item5"/>
	</Group>
	<Group DependencyID="NONE" Description="kolj sedf" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group4" GroupType="DropBox">
		<Item DefaultItem="true" Description="asdfas asdfasd" ItemCode="Item6"/>
		<Item DefaultItem="false" Description="asdfasdf
			</Group>
			<Group DependencyID="NONE" Description="Zipcode" DisplayIndicator="false" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group5" GroupType="TextBox(Integer)">
				<Item DefaultItem="true" Description="DEFAULT" ItemCode="Item3"/>
				<Item DefaultItem="false" Description="mjhhhh" ItemCode="Item4"/>
			</Group>
		</Section>
		<Section ColumnCount="1" Description="kkyyu" ExpandedIndicator="true" SectionCode="code2">
			<Group DependencyID="NONE" Description="asdf" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group6" GroupType="CheckBox">
				<Item DefaultItem="true" Description="gdff" ItemCode="Item2"/>
				<Item DefaultItem="false" Description="gsdfs" ItemCode="Item5"/>
			</Group>
			<Group DependencyID="NONE" Description="test" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group7" GroupType="ListBox">
				<Item DefaultItem="true" Description="1" ItemCode="1"/>
				<Item DefaultItem="false" Description="2" ItemCode="2"/>
				<Item DefaultItem="false" Description="3" ItemCode="3"/>
				<Item DefaultItem="false" Description="4" ItemCode="4"/>
				<Item DefaultItem="false" Description="5" ItemCode="5"/>
				<Item DefaultItem="false" Description="6" ItemCode="6"/>
				<Item DefaultItem="false" Description="7" ItemCode="7"/>
				<Item DefaultItem="false" Description="8" ItemCode="8"/>
				<Item DefaultItem="false" Description="9" ItemCode="9"/>
				<Item DefaultItem="false" Description="10" ItemCode="10"/>
				<Item DefaultItem="false" Description="14" ItemCode="14"/>
				<Item DefaultItem="false" Description="21" ItemCode="21"/>
			</Group>
			<Group DependencyID="NONE" Description="Parmsgdfghfgxvb?" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group8" GroupType="CheckBox">
				<Item DefaultItem="true" Description="rthcvbcv" ItemCode="NO"/>
				<Item DefaultItem="false" Description="jsdfgsdfg" ItemCode="YES"/>
			</Group>
			<Group DependencyID="NONE" Description="loik" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group9" GroupType="CheckBox">
				<Item DefaultItem="true" Description="sdfsd" ItemCode="NO"/>
				<Item DefaultItem="false" Description="urtyrt" ItemCode="YES"/>
			</Group>
			<Group DependencyID="NONE" Description="e34234" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group10" GroupType="CheckBox">
				<Item DefaultItem="true" Description="jjj" ItemCode="NO"/>
				<Item DefaultItem="false" Description=hncxv" ItemCode="YES"/>
			</Group>
			<Group DependencyID="NONE" Description="Flex Indicator" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group11" GroupType="DropBox">
				<Item DefaultItem="true" Description="No" ItemCode="N"/>
				<Item DefaultItem="false" Description="Yes" ItemCode="Y"/>
			</Group>
		</Section>
	</Test>

XML2

<TEST>
	<Section ColumnCount="1" Description="jkfgkghjfgh" ExpandedIndicator="true" SectionCode="code3">
		<Group DependencyID="NONE" Description="asdf" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="Group2" GroupType="DropBox">
			<Item DefaultItem="true" Description="Retail - No asdf" ItemCode="Item1"/>
			<Item DefaultItem="false" Description="asdf asdf" ItemCode="Item2"/>
			<Item DefaultItem="false" Description="asdf &asdf; Conventions" ItemCode="Item3"/>
		</Group>
		<Group DependencyID="NONE" DisplayIndicator="false" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group3">
			<Item DefaultItem="false" Description="dfgdfg" ItemCode="Item4"/>
			<Item DefaultItem="false" Description="dfgert" ItemCode="Item5"/>
		</Group>
		<Group DependencyID="NONE" Description="kolj sedf" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group4" GroupType="DropBox">
			<Item DefaultItem="true" Description="asdfas asdfasd" ItemCode="Item6"/>
			<Item DefaultItem="false" Description="asdfasdf
				</Group>
				<Group DependencyID="NONE" Description="Zipcode" DisplayIndicator="false" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group5" GroupType="TextBox(Integer)">
					<Item DefaultItem="true" Description="DEFAULT" ItemCode="Item3"/>
					<Item DefaultItem="false" Description="mjhhhh" ItemCode="Item4"/>
				</Group>
			</Section>
			<Section ColumnCount="1" Description="kkyyu" ExpandedIndicator="true" SectionCode="code2">
				<Group DependencyID="NONE" Description="asdf" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group6" GroupType="CheckBox">
					<Item DefaultItem="true" Description="gdff" ItemCode="Item2"/>
					<Item DefaultItem="false" Description="gsdfs" ItemCode="Item5"/>
				</Group>
				<Group DependencyID="NONE" Description="Flex Indicator" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group11" GroupType="DropBox">
					<Item DefaultItem="true" Description="No" ItemCode="N"/>
					<Item DefaultItem="false" Description="Yes" ItemCode="Y"/>
				</Group>
				<Group DependencyID="NONE" Description="Parmsgdfghfgxvb?" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group8" GroupType="CheckBox">
					<Item DefaultItem="true" Description="rthcvbcv" ItemCode="NO"/>
					<Item DefaultItem="false" Description="jsdfgsdfg" ItemCode="YES"/>
				</Group>
				<Group DependencyID="NONE" Description="loik" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group9" GroupType="CheckBox">
					<Item DefaultItem="true" Description="sdfsd" ItemCode="NO"/>
					<Item DefaultItem="false" Description="urtyrt" ItemCode="YES"/>
				</Group>
				<Group DependencyID="NONE" Description="e34234" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group10" GroupType="CheckBox">
					<Item DefaultItem="true" Description="jjj" ItemCode="NO"/>
					<Item DefaultItem="false" Description=hncxv" ItemCode="YES"/>
				</Group>
				
				<Group DependencyID="NONE" Description="test" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group7" GroupType="ListBox">
					<Item DefaultItem="true" Description="1" ItemCode="1"/>
					<Item DefaultItem="false" Description="2" ItemCode="2"/>
					<Item DefaultItem="false" Description="3" ItemCode="3"/>
					<Item DefaultItem="false" Description="4" ItemCode="4"/>
					<Item DefaultItem="false" Description="5" ItemCode="5"/>
					<Item DefaultItem="false" Description="6" ItemCode="6"/>
					<Item DefaultItem="false" Description="7" ItemCode="7"/>
					<Item DefaultItem="false" Description="8" ItemCode="8"/>
					<Item DefaultItem="false" Description="9" ItemCode="9"/>
					<Item DefaultItem="false" Description="10" ItemCode="10"/>
					<Item DefaultItem="false" Description="14" ItemCode="14"/>
					<Item DefaultItem="false" Description="21" ItemCode="21"/>
				</Group>
			</Section>
			<Section ColumnCount="0" Description="test" ExpandedIndicator="false" SectionCode="code1">
				<Group DependencyID="NONE" Description="asdf" DisplayIndicator="true" FlexibilityDefault="false" FlexibilityIndicator="false" GroupCode="group1" GroupType="CheckBox">
					<Item DefaultItem="false" Description="YES" ItemCode="YES"/>
					<Item DefaultItem="true" Description="NO" ItemCode="NO"/>
				</Group>
			</Section>
		</Test>

I tried using ```
DefaultNodeMatcher(ElementSelectors.conditionalBuilder().whenElementIsNamed("Test")
.thenUse(ElementSelectors.byXPath("SectionCode", ElementSelectors.byNameAndAttributes))
.elseUse(ElementSelectors.byName)))

But doesn't seem to work. 

I would like to compare the section tag based on the sectioncode attribute and also ignore the order of the GRoup and item tags under it.

Can you help me with this pls.
@bodewig
Copy link
Member

bodewig commented Jun 12, 2020

There is no reason to define a special ElementSelector for Test as there is only one of them anyway. You need custom ElementSelectors only for nodes that XMLUnit needs help with matching to each other.

So let us translate what you want to achieve:

compare the section tag based on the sectioncode attribute

.whenElementIsNamed("Section").thenUse(byNameAndAttributes("SectionCode")

and also ignore the order of the GRoup and item tags under it

how do you know the Groups are the same and just re-ordered? What is significant? The GroupCode? Same question for Item. A (wild?) guess:

.whenElementIsNamed("Group").thenUse(byNameAndAttributes("GroupCode")
.whenElementIsNamed("Item").thenUse(byNameAndAttributes("ItemCode")

@CKUser123
Copy link
Author

CKUser123 commented Jun 16, 2020

@bodewig So basically, i just want to compare two xmls ignoring the order of the node(both the parent nodes as well as the child nodes) and only report an error when there are extra nodes or attributes. i know for certain that 99% of the nodes should match but the order of the two xmls will be 100 % different.

i have been using this for quite some time now and this works when the nodes are not in order but there are not many child/subchild nodes(like group and item).

the description tags in both goup and item tags will be unique

So i just want to know if there is any way i can get this to work when there are multiple sub nodes.

@bodewig
Copy link
Member

bodewig commented Jun 16, 2020

This sounds as if you were looking for a way where XMLUnit would try all permutations of nodes in order to pick the one with the least differences. While this is technically possible and #45 exists because of this, the space and time complexity would be pretty big - read, this would be very slow for big XML documents and may exceed your available memory.

In absence of this brute-force approach, XMLUnit needs your help to know which elements are "the same just out-of-seqence", this is where ElementSelector comes in. This certainly works for multiple children and nested structures as long as you can express the rules.

ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("Section").thenUse(ElementSelectors.byNameAndAttributes("SectionCode"))
    .whenElementIsNamed("Group").thenUse(ElementSelectors.byNameAndAttributes("GroupCode"))
    .whenElementIsNamed("Item").thenUse(ElementSelectors.byNameAndAttributes("ItemCode"))
    .build();

should work for the example documents you've given.

@CKUser123
Copy link
Author

thanks Stefan @bodewig .Was able to get the logic to work after putting is a simple sort code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants