From 9154f4c08767dd94d88092c2ed2f53d12d1a3f31 Mon Sep 17 00:00:00 2001
From: Achraf <aai@kereval.com>
Date: Thu, 19 Oct 2023 13:11:33 +0200
Subject: [PATCH] Add dynamic namespace resolving from BBR

---
 .../attr/test/AttrClassCodeAnalyzerIT.java    |  5 +-
 .../AttrContextConductionIndAnalyzerIT.java   |  2 +-
 .../AttrContextControlCodeAnalyzerIT.java     |  2 +-
 .../attr/test/AttrExtensionAnalyzerIT.java    |  2 +-
 .../attr/test/AttrProhibitedAnalyzerIT.java   |  2 +-
 .../tempgen/attr/test/AttrUseAnalyzerIT.java  |  2 +-
 .../attr/test/AttrValueAnalyzerIT.java        |  6 +-
 .../attr/test/AttributeAnalyzerIT.java        | 15 +++--
 ...ExtraNegativeDistinguisherControlerIT.java |  4 +-
 .../tempgen/test/RDMandatoryAnalyzerIT.java   |  8 +--
 .../test/RDMaximumMultiplicityAnalyzerIT.java | 14 +++++
 .../test/RDMinimumMultiplicityAnalyzerIT.java |  4 +-
 .../test/RuleDefinitionAnalyzerIT.java        |  1 +
 .../gazelle/tempgen/test/TextAnalyzerIT.java  |  2 +-
 .../tempgen/test/ValueSetFlexibility2IT.java  |  4 +-
 .../tempgen/test/ValueSetFlexibility3IT.java  |  4 +-
 .../tempgen/test/ValueSetFlexibilityIT.java   |  4 +-
 .../test/resources/decor_obs_namespace.xml    | 59 +++++++++++++++++++
 .../tempmodel/test/RuleDefinitionUtilIT.java  |  8 +--
 .../tempmodel/dpath/model/DElement.java       | 13 +++-
 .../tempmodel/dpath/utils/DPathExtractor.java | 31 ++++++----
 .../RuleDefinitionDistinguisherUtil.java      |  6 +-
 .../decor/art/utils/RuleDefinitionUtil.java   | 14 +++--
 .../tempmodel/test/DPathExtractorTest.java    | 10 ++--
 24 files changed, 161 insertions(+), 61 deletions(-)
 create mode 100644 hl7templates/hl7templates-generator-jar/src/test/resources/decor_obs_namespace.xml

diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrClassCodeAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrClassCodeAnalyzerIT.java
index f8f0dc4..5d0fb9b 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrClassCodeAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrClassCodeAnalyzerIT.java
@@ -1,5 +1,6 @@
 package net.ihe.gazelle.tempgen.attr.test;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Before;
@@ -41,8 +42,8 @@ public class AttrClassCodeAnalyzerIT {
 		RuleDefinition firstRD = TemplateDefinitionUtil.getFirstElement(RulesUtil.getTemplates(decorTemplates.getRules()).get(0));
 		Attribute attr = firstRD.getAttribute().get(0);
 		String comment = (new AttrClassCodeAnalyzer()).generateCommentConstraint(attr);
-		assertTrue(comment.equals("In Text Observation, in /hl7:observation[hl7:templateId/@root='2.16.840.1.113883.10.20.6.2.12'], "
-				+ "the attribute classCode SHALL have the value 'OBS' if present"));
+		assertEquals("In Text Observation, in /cda:observation[cda:templateId/@root='2.16.840.1.113883.10.20.6.2.12'], "
+				+ "the attribute classCode SHALL have the value 'OBS' if present",comment);
 	}
 
 	@Test
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrContextConductionIndAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrContextConductionIndAnalyzerIT.java
index 238f40d..eb8b885 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrContextConductionIndAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrContextConductionIndAnalyzerIT.java
@@ -45,7 +45,7 @@ public class AttrContextConductionIndAnalyzerIT {
 		Attribute attr = entr.getAttribute().get(0);
 		String comment = (new AttrContextConductionIndAnalyzer()).generateCommentConstraint(attr);
 		assertTrue(comment.equals("In Hospital Discharge Diagnosis Section, in "
-				+ "/hl7:section[hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/hl7:entry, the attribute contextConductionInd SHALL "
+				+ "/cda:section[cda:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/cda:entry, the attribute contextConductionInd SHALL "
 				+ "have the value 'true' if present"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrContextControlCodeAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrContextControlCodeAnalyzerIT.java
index c921ee2..0136bb8 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrContextControlCodeAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrContextControlCodeAnalyzerIT.java
@@ -56,7 +56,7 @@ public class AttrContextControlCodeAnalyzerIT {
 		Attribute attr = subj.getAttribute().get(0);
 		String comment = (new AttrContextControlCodeAnalyzer()).generateCommentConstraint(attr);
 		assertTrue(comment.equals("In Hospital Discharge Diagnosis Section, in "
-				+ "/hl7:section[hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/hl7:subject, the attribute contextControlCode SHALL "
+				+ "/cda:section[cda:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/cda:subject, the attribute contextControlCode SHALL "
 				+ "have the value 'OP' if present"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrExtensionAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrExtensionAnalyzerIT.java
index b64dea8..1a11f24 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrExtensionAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrExtensionAnalyzerIT.java
@@ -45,7 +45,7 @@ public class AttrExtensionAnalyzerIT {
 		Attribute attr = id.getAttribute().get(0);
 		String comment = (new AttrExtensionAnalyzer()).generateCommentConstraint(attr);
 		assertTrue(comment.equals("In Hospital Discharge Diagnosis Section, in "
-				+ "/hl7:section[hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/hl7:id, the attribute extension SHALL "
+				+ "/cda:section[cda:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/cda:id, the attribute extension SHALL "
 				+ "have the value '111' if present"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrProhibitedAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrProhibitedAnalyzerIT.java
index eef7c05..a8ce896 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrProhibitedAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrProhibitedAnalyzerIT.java
@@ -57,7 +57,7 @@ public class AttrProhibitedAnalyzerIT {
 		aa.setAttrName(att.getName());
 		String comment = aa.generateCommentConstraint(att);
 		assertTrue(comment.equals("In US Realm Header, in "
-				+ "/hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/hl7:templateId[@root='2.16.840.1.113883.10.20.22.1.1'], "
+				+ "/cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/cda:templateId[@root='2.16.840.1.113883.10.20.22.1.1'], "
 				+ "the attribute extension SHALL not be provided (prohibited=true)"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrUseAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrUseAnalyzerIT.java
index b7f6c59..31d11aa 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrUseAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrUseAnalyzerIT.java
@@ -49,7 +49,7 @@ public class AttrUseAnalyzerIT {
 		Attribute attr = add.getAttribute().get(0);
 		String comment = (new AttrUseAnalyzer()).generateCommentConstraint(attr);
 		assertTrue(comment.equals("In Hospital Discharge Diagnosis Section, in "
-				+ "/hl7:section[hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/hl7:subject/hl7:relatedSubject/hl7:addr, "
+				+ "/cda:section[cda:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/cda:subject/cda:relatedSubject/cda:addr, "
 				+ "the attribute use SHALL have the value 'HP' if present"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrValueAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrValueAnalyzerIT.java
index 1df1b04..1bb89de 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrValueAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttrValueAnalyzerIT.java
@@ -61,8 +61,8 @@ public class AttrValueAnalyzerIT {
 		Attribute attr = entr.getAttribute().get(0);
 		String comment = (new AttrValueAnalyzer()).generateCommentConstraint(attr);
 		assertTrue(comment.equals("In Text Observation, "
-				+ "in /hl7:observation[hl7:templateId/@root='2.16.840.1.113883.10.20.6.2.12']"
-				+ "/hl7:entryRelationship[@typeCode='SPRT'], the attribute typeCode SHALL have the value 'SPRT' if present"));
+				+ "in /cda:observation[cda:templateId/@root='2.16.840.1.113883.10.20.6.2.12']"
+				+ "/cda:entryRelationship[@typeCode='SPRT'], the attribute typeCode SHALL have the value 'SPRT' if present"));
 	}
 	
 	@Test
@@ -72,7 +72,7 @@ public class AttrValueAnalyzerIT {
 		Attribute attr = entr.getAttribute().get(0);
 		String comment = (new AttrValueAnalyzer()).generateCommentConstraint(attr);
 		assertTrue(comment.equals("In Text Observation, "
-				+ "in /hl7:observation[hl7:templateId/@root='2.16.840.1.113883.10.20.6.2.12']/hl7:entryRelationship[@typeCode='SPRT'], "
+				+ "in /cda:observation[cda:templateId/@root='2.16.840.1.113883.10.20.6.2.12']/cda:entryRelationship[@typeCode='SPRT'], "
 				+ "the attribute typeCode SHALL have the value 'SPRT' if present"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttributeAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttributeAnalyzerIT.java
index 98cd630..928606b 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttributeAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/attr/test/AttributeAnalyzerIT.java
@@ -1,8 +1,5 @@
 package net.ihe.gazelle.tempgen.attr.test;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
 import java.io.ByteArrayOutputStream;
 import java.io.FileNotFoundException;
 import java.util.ArrayList;
@@ -29,6 +26,8 @@ import net.ihe.gazelle.tempmodel.org.decor.art.utils.RuleDefinitionUtil;
 import net.ihe.gazelle.tempmodel.org.decor.art.utils.RulesUtil;
 import net.ihe.gazelle.tempmodel.org.decor.art.utils.TemplateDefinitionUtil;
 
+import static org.junit.Assert.*;
+
 /**
  * @author Abderrazek Boufahja
  */
@@ -497,10 +496,10 @@ public class AttributeAnalyzerIT extends AttributeAnalyzer {
         RuleDefinition tdi = RuleDefinitionUtil.getElementsByName(firstRD, "cda:templateId").get(1);
         this.selectedAttribute = tdi.getAttribute().get(0);
         this.currentPackagedElement = PackagedElementUtil.initPackagedElement();
-        DistinguisherFollower.followDistinguisher("/hl7:observation[hl7:templateId/@root='2.16.840.1.113883.10.20.6.2.12']/"
-                + "hl7:templateId[@root='4.3.2']/@root", "4.3.2");
+        DistinguisherFollower.followDistinguisher("/cda:observation[cda:templateId/@root='2.16.840.1.113883.10.20.6.2.12']/"
+                + "cda:templateId[@root='4.3.2']/@root", "4.3.2");
         this.processValue("4.3.2");
-        assertTrue(this.currentPackagedElement.getOwnedRule().size() == 0);
+        assertEquals( 0, this.currentPackagedElement.getOwnedRule().size());
     }
 
     @Test
@@ -510,8 +509,8 @@ public class AttributeAnalyzerIT extends AttributeAnalyzer {
         RuleDefinition tdi = RuleDefinitionUtil.getElementsByName(firstRD, "cda:templateId").get(1);
         this.selectedAttribute = tdi.getAttribute().get(0);
         this.currentPackagedElement = PackagedElementUtil.initPackagedElement();
-        DistinguisherFollower.unFollowDistinguisher("/hl7:observation[hl7:templateId/@root='2.16.840.1.113883.10.20.6.2.12']/"
-                + "hl7:templateId[@root='4.3.2']/@root");
+        DistinguisherFollower.unFollowDistinguisher("/cda:observation[cda:templateId/@root='2.16.840.1.113883.10.20.6.2.12']/"
+                + "cda:templateId[@root='4.3.2']/@root");
         this.processValue("4.3.2");
         assertTrue(this.currentPackagedElement.getOwnedRule().size() > 0);
     }
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ExtraNegativeDistinguisherControlerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ExtraNegativeDistinguisherControlerIT.java
index 448cc41..91a1260 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ExtraNegativeDistinguisherControlerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ExtraNegativeDistinguisherControlerIT.java
@@ -34,9 +34,9 @@ public class ExtraNegativeDistinguisherControlerIT extends ExtraNegativeDistingu
 		RuleDefinition firstRD = TemplateDefinitionUtil.getFirstElement(RulesUtil.getTemplates(decorTemplates.getRules()).get(0));
 		RuleDefinition title = RuleDefinitionUtil.getElementByName(firstRD, "cda:title");
 		assertTrue(this.extractQuestion(title).equals("Do you accept a negative distinguisher for the RuleDefinition : "
-				+ "/hl7:section[hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/hl7:title?"));
+				+ "/cda:section[cda:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/cda:title?"));
 		assertTrue(this.extractQuestion(firstRD).equals("Do you accept a negative distinguisher for the RuleDefinition : "
-				+ "/hl7:section[hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.24']?"));
+				+ "/cda:section[cda:templateId/@root='2.16.840.1.113883.10.20.22.2.24']?"));
 	}
 
 }
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMandatoryAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMandatoryAnalyzerIT.java
index 3d8918f..9eeb939 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMandatoryAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMandatoryAnalyzerIT.java
@@ -64,12 +64,12 @@ public class RDMandatoryAnalyzerIT {
 		RuleDefinition firstRD = TemplateDefinitionUtil.getFirstElement(RulesUtil.getTemplates(decorTemplatesMandatory.getRules()).get(0));
 		RuleDefinition custodian = RuleDefinitionUtil.getElementByName(firstRD, "cda:custodian");
 		assertTrue((new RDMandatoryAnalyzer()).generateCommentConstraint(custodian).equals(
-				"In US Realm Header, in /hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.1'], "
-				+ "the element(s) hl7:custodian SHALL not have nullFlavor (mandatory)"));
+				"In US Realm Header, in /cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.1'], "
+				+ "the element(s) cda:custodian SHALL not have nullFlavor (mandatory)"));
 		RuleDefinition assignedCustodian = RuleDefinitionUtil.getElementByName(custodian, "cda:assignedCustodian");
 		assertTrue((new RDMandatoryAnalyzer()).generateCommentConstraint(assignedCustodian).equals(
-				"In US Realm Header, in /hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/hl7:custodian, "
-				+ "the element(s) hl7:assignedCustodian SHALL not have nullFlavor (mandatory)"));
+				"In US Realm Header, in /cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/cda:custodian, "
+				+ "the element(s) cda:assignedCustodian SHALL not have nullFlavor (mandatory)"));
 	}
 
 }
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMaximumMultiplicityAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMaximumMultiplicityAnalyzerIT.java
index d0def1a..a6f018c 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMaximumMultiplicityAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMaximumMultiplicityAnalyzerIT.java
@@ -1,5 +1,6 @@
 package net.ihe.gazelle.tempgen.test;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import net.ihe.gazelle.goctests.definitions.annotations.Covers;
@@ -78,4 +79,17 @@ public class RDMaximumMultiplicityAnalyzerIT {
 				+ "contain at most ONE hl7:targetSiteCode"));
 	}
 
+	@Test
+	public void testGenerateMaximumMultiplicityDescriptionRuleNamescape() {
+		Decor decorNamespace = DecorMarshaller.loadDecor("src/test/resources/decor_obs_namespace.xml");
+		RuleDefinition firstRD = TemplateDefinitionUtil.getFirstElement(RulesUtil.getTemplates(decorNamespace.getRules()).get(0));
+		RuleDefinition entr = RuleDefinitionUtil.getElementByName(firstRD, "cda:entryRelationship");
+		RuleDefinition obs = RuleDefinitionUtil.getElementByName(entr, "cda:observation");
+		RuleDefinition status = RuleDefinitionUtil.getElementByName(obs, "cda:targetSiteCode");
+		String gen = (new RDMaximumMultiplicityAnalyzer()).generateCommentConstraint(status);
+		assertEquals("In Problem Concern, "
+				+ "/cda:act[cda:templateId/@root='1.3.6.1.4.1.19376.1.5.3.1.4.5.2']/cda:entryRelationship[cda:observation/cda:code/@code='48766-0']/cda:observation SHALL "
+				+ "contain at most ONE cda:targetSiteCode", gen);
+	}
+
 }
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMinimumMultiplicityAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMinimumMultiplicityAnalyzerIT.java
index fbe6d34..0950e1e 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMinimumMultiplicityAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RDMinimumMultiplicityAnalyzerIT.java
@@ -157,8 +157,8 @@ public class RDMinimumMultiplicityAnalyzerIT {
         RuleDefinition temp = RuleDefinitionUtil.getElementByName(serviceEvent, "cda:templateId");
         String gen = (new RDMinimumMultiplicityAnalyzer()).generateCommentConstraint(temp);
         assertEquals("In GEE Progress Note, "
-                + "/hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.9']/hl7:documentationOf/hl7:serviceEvent "
-                + "SHALL contain at least ONE hl7:templateId[@root='2.16.840.1.113883.10.20.21.3.1']", gen);
+                + "/cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.9']/cda:documentationOf/cda:serviceEvent "
+                + "SHALL contain at least ONE cda:templateId[@root='2.16.840.1.113883.10.20.21.3.1']", gen);
     }
 
     @Test
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RuleDefinitionAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RuleDefinitionAnalyzerIT.java
index a71f536..cb07b61 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RuleDefinitionAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/RuleDefinitionAnalyzerIT.java
@@ -526,6 +526,7 @@ public class RuleDefinitionAnalyzerIT extends RuleDefinitionAnalyzer {
     public void testFollowDistinguisher() throws Exception {
         DElement ruleDefinitionDParent = new DElement();
         ruleDefinitionDParent.setName("addr");
+        ruleDefinitionDParent.setNameSpace("hl7");
         ruleDefinitionDParent.setFollowingAttributeOrElement(new DAttribute());
         ruleDefinitionDParent.getFollowingAttributeOrElement().setName("use");
         followDistinguisher("test", ruleDefinitionDParent);
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/TextAnalyzerIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/TextAnalyzerIT.java
index b5f7e64..d0d3297 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/TextAnalyzerIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/TextAnalyzerIT.java
@@ -56,7 +56,7 @@ Decor decorTemplates = null;
 		this.setSelectedRuleDefinition(title);
 		String comment = this.generateCommentConstraint(title.getText());
 		assertTrue(comment.equals("In Hospital Discharge Diagnosis Section, "
-				+ "in /hl7:section[hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/hl7:title, "
+				+ "in /cda:section[cda:templateId/@root='2.16.840.1.113883.10.20.22.2.24']/cda:title, "
 				+ "the string value shall have the value 'test'"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibility2IT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibility2IT.java
index 2739dcc..ba1f237 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibility2IT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibility2IT.java
@@ -32,10 +32,10 @@ public class ValueSetFlexibility2IT extends DecorAnalyzer {
 		assertEquals(2, xmi.getModel().getPackagedElement().size());
 		assertEquals(9, xmi.getModel().getPackagedElement().get(0).getOwnedRule().size());
 		assertTrue(xmiStr.contains("In US Realm Header, the code of "
-				+ "/hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/hl7:confidentialityCode SHALL be from"
+				+ "/cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/cda:confidentialityCode SHALL be from"
 				+ " the valueSet 2.16.840.1.113883.1.11.16926"));
 		assertTrue(xmiStr.contains("In US Realm Header, the code of "
-				+ "/hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/hl7:languageCode SHALL be from"
+				+ "/cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/cda:languageCode SHALL be from"
 				+ " the valueSet 2.16.840.1.113883.1.11.11526"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibility3IT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibility3IT.java
index 75febc8..7fcd280 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibility3IT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibility3IT.java
@@ -31,10 +31,10 @@ public class ValueSetFlexibility3IT extends DecorAnalyzer {
 		assertTrue(xmi.getModel().getPackagedElement().size() == 2);
 		assertTrue(xmi.getModel().getPackagedElement().get(0).getOwnedRule().size() == 9);
 		assertTrue(xmiStr.contains("In US Realm Header, the code of "
-				+ "/hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/hl7:confidentialityCode SHALL be from"
+				+ "/cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/cda:confidentialityCode SHALL be from"
 				+ " the valueSet 2.16.840.1.113883.1.11.16926"));
 		assertTrue(xmiStr.contains("In US Realm Header, the code of "
-				+ "/hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/hl7:languageCode SHALL be from"
+				+ "/cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/cda:languageCode SHALL be from"
 				+ " the valueSet 2.16.840.1.113883.1.11.11526"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibilityIT.java b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibilityIT.java
index fa7d982..02ffbac 100644
--- a/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibilityIT.java
+++ b/hl7templates/hl7templates-generator-jar/src/it/java/net/ihe/gazelle/tempgen/test/ValueSetFlexibilityIT.java
@@ -32,10 +32,10 @@ public class ValueSetFlexibilityIT extends DecorAnalyzer {
 		assertEquals(2, xmi.getModel().getPackagedElement().size());
 		assertEquals(9, xmi.getModel().getPackagedElement().get(0).getOwnedRule().size());
 		assertTrue(xmiStr.contains("In US Realm Header, the code of "
-				+ "/hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/hl7:confidentialityCode SHALL be from"
+				+ "/cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/cda:confidentialityCode SHALL be from"
 				+ " the valueSet 2.16.840.1.113883.1.11.16926"));
 		assertTrue(xmiStr.contains("In US Realm Header, the code of "
-				+ "/hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/hl7:languageCode SHALL be from"
+				+ "/cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1.1']/cda:languageCode SHALL be from"
 				+ " the valueSet 2.16.840.1.113883.1.11.11526"));
 	}
 
diff --git a/hl7templates/hl7templates-generator-jar/src/test/resources/decor_obs_namespace.xml b/hl7templates/hl7templates-generator-jar/src/test/resources/decor_obs_namespace.xml
new file mode 100644
index 0000000..2adad3d
--- /dev/null
+++ b/hl7templates/hl7templates-generator-jar/src/test/resources/decor_obs_namespace.xml
@@ -0,0 +1,59 @@
+<?xml-model href="http://art-decor.org/ADAR/rv/DECOR.xsd" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
+<decor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epsos="urn:epSOS:ps:ps:2010" xsi:noNamespaceSchemaLocation="http://art-decor.org/ADAR/rv/DECOR.xsd" repository="true" epsos:dummy-1="urn:epSOS:ps:ps:2010" xsi:dummy-2="http://www.w3.org/2001/XMLSchema-instance" versionDate="2015-06-09T11:03:41" versionLabel="" compilationDate="2015-06-09T11:03:41" language="en-US" deeplinkprefix="http://decor.nictiz.nl/art-decor/" deeplinkprefixservices="http://decor.nictiz.nl/decor/services/">
+<!--
+ This is a compiled version of a DECOR based project. Compilation date:  2015-06-09T11:03:41 
+ PLEASE NOTE THAT ITS ONLY PURPOSE IS TO FACILITATE HTML AND SCHEMATRON GENERATION. HENCE THIS IS A ONE OFF FILE UNSUITED FOR ANY OTHER PURPOSE 
+ Compilation process calls getFullDataSetTree where all inheritance of concepts from repositories is resolved 
+ Compilation process leaves valueSet[@ref] as-is but adds, if available, the valueSet (versions) it references. These are marked with valueSet[@referencedFrom, @ident and/or @url] 
+ Compilation process tries to find names for any OIDs referenced in the project but not yet in ids/id, and adds an entry if a name is found 
+ Compilation process does not yet handle retrieval of referenced templates if they are not inside this project-->
+    <project id="2.16.840.1.113883.2.4.3.11.60.22" prefix="ccda-" defaultLanguage="en-US">
+    </project>
+    <rules>
+        <!--epSOS CDA ManufacturedProduct-->
+        <!--Problem Concern-->
+        <template id="1.3.6.1.4.1.19376.1.5.3.1.4.5.2" name="EntryProblemConcern" effectiveDate="2013-12-20T00:00:00" statusCode="draft" displayName="Problem Concern">
+            <desc language="en-US">
+                <p>This entry is a specialization of the Concern Entry, wherein the subject of the concern is focused on a problem. Elements shown in the example below in gray are explained in the Concern Entry. </p>
+                <p>Parent Template </p>
+                <p>The parent of this template is Concern Entry. This template is compatible with the ASTM/HL7 Continuity of Care Document template: 2.16.840.1.113883.10.20.1.27 </p>
+                <p>This entry has a template identifier of 1.3.6.1.4.1.19376.1.5.3.1.4.5.2, and is a subtype of the Concern Entry, and so must also conform to that specification, with the template identifier of 1.3.6.1.4.1.19376.1.5.3.1.4.5.1. These elements are required and shall be recorded exactly as shown. </p>
+            </desc>
+            <classification type="cdaentrylevel"/>
+            <context id="**"/>
+            <element name="cda:act" conformance="R">
+                <attribute name="classCode" value="ACT"/>
+                <attribute name="moodCode" value="EVN"/>
+                <element name="cda:templateId" minimumMultiplicity="1" maximumMultiplicity="1" datatype="II" isMandatory="true" conformance="R">
+                    <attribute name="root" value="1.3.6.1.4.1.19376.1.5.3.1.4.5.2"/>
+                </element>
+                <!-- optional entry relationship providing more information about the concern -->
+                <element name="cda:entryRelationship" minimumMultiplicity="0" maximumMultiplicity="*" conformance="R">
+                    <desc language="en-US">optional entry relationship providing more information about the concern. A source of information observation SHALL be the target of an entryRelationship whose value for “entryRelationship / @typeCode” SHALL be “REFR”  “Refers to” 2.16.840.1.113883.5.1002 ActRelationshipType STATIC. A source of information observation SHALL be represented with Observation.</desc>
+                    <item label="CONF-524 / CONF-525"/>
+                    <attribute name="typeCode" value="REFR" isOptional="false"/>
+                    <element name="cda:observation">
+                        <desc language="en-US">ASTM CCR requires that all data objects have a stated source (or state explicitly that the source is unknown) so that any data within the summary can be validated. The source of data may be a person, organization, reference to some other data object, etc.</desc>
+                        <attribute classCode="OBS" isOptional="true">
+                            <desc language="en-US">The value for “Observation / @classCode” in a source of information observation SHALL be “OBS” 2.16.840.1.113883.5.6 ActClass STATIC.</desc>
+                            <item label="CONF-526"/>
+                        </attribute>
+                        <element name="cda:code" minimumMultiplicity="1" maximumMultiplicity="1" conformance="R" datatype="CD">
+                            <desc language="en-US">A source of information observation SHALL contain exactly one Observation / code. The value for “Observation / code” in a source of information observation SHALL be “48766-0” “Information source” 2.16.840.1.113883.6.1 LOINC STATIC.</desc>
+                            <item label="CONF-530 / CONF-531"/>
+                            <vocabulary code="48766-0" codeSystem="2.16.840.1.113883.6.1" displayName="Information source"/>
+                            <attribute name="code" value="48766-0" />
+                        </element>
+                        <element name="cda:targetSiteCode" minimumMultiplicity="1" maximumMultiplicity="1" conformance="R"/>
+                        <element name="cda:value" datatype="CD">
+		                    <attribute name="code">
+		                        <item label="conf-16563"/>
+		                        <vocabulary valueSet="2.16.840.1.113883.11.20.9.41"/>
+		                    </attribute>
+		                </element>
+                    </element>
+                </element>
+            </element>
+        </template>
+    </rules>
+</decor>
\ No newline at end of file
diff --git a/hl7templates/hl7templates-model-jar/src/it/java/net/ihe/gazelle/tempmodel/test/RuleDefinitionUtilIT.java b/hl7templates/hl7templates-model-jar/src/it/java/net/ihe/gazelle/tempmodel/test/RuleDefinitionUtilIT.java
index 7c232ca..d1b6437 100644
--- a/hl7templates/hl7templates-model-jar/src/it/java/net/ihe/gazelle/tempmodel/test/RuleDefinitionUtilIT.java
+++ b/hl7templates/hl7templates-model-jar/src/it/java/net/ihe/gazelle/tempmodel/test/RuleDefinitionUtilIT.java
@@ -189,9 +189,9 @@ public class RuleDefinitionUtilIT extends RuleDefinitionUtil {
         templateId.setParentObject(containDefinition);
         DParent dp = RuleDefinitionUtil.getDParentOfRuleDefinition(templateId);
         String repr = DPathExtractor.createPathFromDParent(dp);
-        assertEquals("/hl7:ClinicalDocument[hl7:templateId/@root='2.16.840.1.113883.10.20.22.1" +
-                ".9']/hl7:component/hl7:structuredBody/hl7:component[hl7:section/hl7:templateId/@root='2.16.840.1.113883.10.20.21.2" +
-                ".1']/hl7:templateId", repr);
+        assertEquals("/cda:ClinicalDocument[cda:templateId/@root='2.16.840.1.113883.10.20.22.1" +
+                ".9']/cda:component/cda:structuredBody/cda:component[cda:section/cda:templateId/@root='2.16.840.1.113883.10.20.21.2" +
+                ".1']/cda:templateId", repr);
     }
 
     @Test
@@ -712,7 +712,7 @@ public class RuleDefinitionUtilIT extends RuleDefinitionUtil {
             replacementRD.setName("cda:component");
             replacementRD.setParentObject(currentContainDefinition);
             RuleDefinition templateId = new RuleDefinition();
-            templateId.setName("hl7:templateId");
+            templateId.setName("cda:templateId");
             templateId.getAttribute().add(new Attribute());
             templateId.getAttribute().get(0).setName("root");
             templateId.getAttribute().get(0).setValue(TemplateDefinitionUtil.extractTemplateId(td));
diff --git a/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/dpath/model/DElement.java b/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/dpath/model/DElement.java
index aca9dc2..a11b063 100644
--- a/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/dpath/model/DElement.java
+++ b/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/dpath/model/DElement.java
@@ -8,6 +8,8 @@ public class DElement  extends DParent{
 	
 	private String extendedType;
 
+	private String nameSpace;
+
 	public String getExtendedType() {
 		return extendedType;
 	}
@@ -32,6 +34,13 @@ public class DElement  extends DParent{
 			DParent distinguisherAttributeOrElement) {
 		this.distinguisherAttributeOrElement = distinguisherAttributeOrElement;
 	}
-	
-	
+
+	public String getNameSpace() {
+		return nameSpace;
+	}
+
+	public DElement setNameSpace(String nameSpace) {
+		this.nameSpace = nameSpace;
+		return this;
+	}
 }
diff --git a/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/dpath/utils/DPathExtractor.java b/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/dpath/utils/DPathExtractor.java
index 21391da..56b2e70 100644
--- a/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/dpath/utils/DPathExtractor.java
+++ b/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/dpath/utils/DPathExtractor.java
@@ -1,15 +1,14 @@
 package net.ihe.gazelle.tempmodel.dpath.utils;
 
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
+import net.ihe.gazelle.tempmodel.dpath.model.DAttribute;
+import net.ihe.gazelle.tempmodel.dpath.model.DElement;
+import net.ihe.gazelle.tempmodel.dpath.model.DParent;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import net.ihe.gazelle.tempmodel.dpath.model.DAttribute;
-import net.ihe.gazelle.tempmodel.dpath.model.DElement;
-import net.ihe.gazelle.tempmodel.dpath.model.DParent;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * 
@@ -37,8 +36,14 @@ public class DPathExtractor {
 	private static final Pattern PATT_PATH_ATTR = Pattern.compile(PATH_ATTR);
 	
 	private static final Pattern PATT_PATH_ELEM = Pattern.compile(PATH_ELEM);
-	
+
+
+
 	public static DParent extractDElementFromDPath(String dpath){
+		return extractDElementFromDPathWithNS(dpath, "hl7");
+	}
+
+	public static DParent extractDElementFromDPathWithNS(String dpath, String namespace){
 		if (!DPathExtractor.pathIsValidDPath(dpath)) {
 			log.error("The path specified is not a valid dpath : {}", dpath);
 			return null;
@@ -68,7 +73,7 @@ public class DPathExtractor {
 				}
 			}
 			else {
-				DElement ell = createElementFromPathElement(mat.group());
+				DElement ell = createElementFromPathElement(mat.group(), namespace);
 				if (ell == null) {
 					// the tool not able to process
 					return null;
@@ -79,6 +84,7 @@ public class DPathExtractor {
 				}
 				else if (pointer instanceof DElement){
 					((DElement)pointer).setFollowingAttributeOrElement(ell);
+					((DElement)pointer).setNameSpace(namespace);
 					pointer = ell;
 				}
 				else {
@@ -121,7 +127,7 @@ public class DPathExtractor {
 		}
 		else if(attrOrElement instanceof DElement){
 			DElement el = (DElement)attrOrElement;
-			res.append("hl7:").append(el.getName());
+			res.append(el.getNameSpace()).append(":").append(el.getName());
 			if (el.getDistinguisherAttributeOrElement() != null){
 				res.append("["); 
 				createCorePathFromDParentByStringBuilder(el.getDistinguisherAttributeOrElement(), res);
@@ -133,7 +139,11 @@ public class DPathExtractor {
 		}
 	}
 
-	protected static DElement createElementFromPathElement(String group) {
+	protected static DElement createElementFromPathElement(String group){
+		return createElementFromPathElement(group, "hl7");
+	}
+
+	protected static DElement createElementFromPathElement(String group, String namespace) {
 		if (group == null) {
 			return null;
 		}
@@ -141,6 +151,7 @@ public class DPathExtractor {
 		if (mat.find()){
 			DElement elem = new DElement();
 			elem.setName(mat.group(3));
+			elem.setNameSpace(namespace);
 			if (mat.group(5) != null){
 				if (!StringUtils.isNumeric(mat.group(5))) {
 					DParent dist = extractDElementFromDPath("/" + mat.group(5));
diff --git a/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/org/decor/art/utils/RuleDefinitionDistinguisherUtil.java b/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/org/decor/art/utils/RuleDefinitionDistinguisherUtil.java
index 2da75b7..3a46484 100644
--- a/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/org/decor/art/utils/RuleDefinitionDistinguisherUtil.java
+++ b/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/org/decor/art/utils/RuleDefinitionDistinguisherUtil.java
@@ -1,5 +1,6 @@
 package net.ihe.gazelle.tempmodel.org.decor.art.utils;
 
+import net.ihe.gazelle.tempmodel.dpath.model.DElement;
 import net.ihe.gazelle.tempmodel.dpath.model.DParent;
 import net.ihe.gazelle.tempmodel.dpath.utils.DPathExtractor;
 import net.ihe.gazelle.tempmodel.org.decor.art.behavior.HasParent;
@@ -112,7 +113,10 @@ public class RuleDefinitionDistinguisherUtil {
     public static DParent generateDistinguisherForRuleDefinition(RuleDefinition ruleDefinition) {
         Pair<String, String> pair = RuleDefinitionDistinguisherUtil.extractUniqueDistinguisher(ruleDefinition);
         if (pair != null && pair.getObject1() != null && pair.getObject2() != null) {
-            DParent dElement = DPathExtractor.extractDElementFromDPath(pair.getObject1());
+            DParent dElement = DPathExtractor.extractDElementFromDPathWithNS(pair.getObject1(), ruleDefinition.getName().split(":")[0]);
+            if(dElement instanceof DElement){
+                ((DElement) dElement).setNameSpace(ruleDefinition.getName().split(":")[0]);
+            }
             String valueDPath = pair.getObject2();
             if (valueDPath != null) {
                 dElement.getTail().setValue(valueDPath);
diff --git a/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/org/decor/art/utils/RuleDefinitionUtil.java b/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/org/decor/art/utils/RuleDefinitionUtil.java
index bfd37ee..1015827 100644
--- a/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/org/decor/art/utils/RuleDefinitionUtil.java
+++ b/hl7templates/hl7templates-model-jar/src/main/java/net/ihe/gazelle/tempmodel/org/decor/art/utils/RuleDefinitionUtil.java
@@ -171,12 +171,12 @@ public class RuleDefinitionUtil {
             } else if (cd.getParentObject() instanceof TemplateDefinition) {
                 TemplateDefinition parentTD = (TemplateDefinition) cd.getParentObject();
                 String nameRD = RuleDefinitionUtil.getRealNameOfRuleDefinition(ruleDefinition);
-                res = treatTemplateDefinitionToGetDParent(parentTD, nameRD);
+                res = treatTemplateDefinitionToGetDParent(parentTD, nameRD, ruleDefinition.getName().split(":")[0]);
             }
         } else if (ruleDefinition.getParentObject() instanceof TemplateDefinition) {
             TemplateDefinition parentTD = (TemplateDefinition) ruleDefinition.getParentObject();
             String nameRD = RuleDefinitionUtil.getRealNameOfRuleDefinition(ruleDefinition);
-            res = treatTemplateDefinitionToGetDParent(parentTD, nameRD);
+            res = treatTemplateDefinitionToGetDParent(parentTD, nameRD, ruleDefinition.getName().split(":")[0]);
         } else if (ruleDefinition.getParentObject() instanceof ContainDefinition) {
             ContainDefinition cd = (ContainDefinition) ruleDefinition.getParentObject();
             if (cd.getParentObject() instanceof RuleDefinition) {
@@ -192,7 +192,9 @@ public class RuleDefinitionUtil {
         DElement el = (DElement) res;
         if (el.get(el.size() - 1) instanceof DElement) {
             DElement lastDE = new DElement();
-            lastDE.setName(RuleDefinitionUtil.getRealNameOfRuleDefinition(ruleDefinition));
+            String realName = RuleDefinitionUtil.getRealNameOfRuleDefinition(ruleDefinition);
+            lastDE.setName(realName);
+            lastDE.setNameSpace(ruleDefinition.getName().split(":")[0]);
             if (RuleDefinitionProblemUtil.ruleDefinitionNeedsDistinguisher(ruleDefinition)) {
                 // TODO still need to process when the ruleDefinition has a negative distinguisher
                 lastDE.setDistinguisherAttributeOrElement(RuleDefinitionDistinguisherUtil.generateDistinguisherForRuleDefinition(ruleDefinition));
@@ -210,15 +212,17 @@ public class RuleDefinitionUtil {
         }
     }
 
-    private static DParent treatTemplateDefinitionToGetDParent(TemplateDefinition parentTD, String nameRD) {
+    private static DParent treatTemplateDefinitionToGetDParent(TemplateDefinition parentTD, String nameRD, String namespace) {
         DParent res = null;
         DElement el = new DElement();
         el.setName(nameRD);
+        el.setNameSpace(namespace);
         String templateIdValue = TemplateDefinitionUtil.extractTemplateId(parentTD);
         if (templateIdValue != null) {
             el.setDistinguisherAttributeOrElement(new DElement());
             DElement dis = (DElement) el.getDistinguisherAttributeOrElement();
             dis.setName("templateId");
+            dis.setNameSpace(namespace);
             dis.setFollowingAttributeOrElement(new DAttribute());
             ((DAttribute) dis.getFollowingAttributeOrElement()).setName("root");
             ((DAttribute) dis.getFollowingAttributeOrElement()).setValue(templateIdValue);
@@ -316,7 +320,7 @@ public class RuleDefinitionUtil {
         List<Distinguisher> ldis = DistinguisherUtil.getRealDistinguisher(eldisc);
         if (eldisc != null && ldis != null && !ldis.isEmpty()) {
             for (Distinguisher distinguisher : ldis) {
-                DParent dElement = DPathExtractor.extractDElementFromDPath(distinguisher.getXpath());
+                DParent dElement = DPathExtractor.extractDElementFromDPathWithNS(distinguisher.getXpath(), ruleDefinition.getName().split(":")[0]);
                 String valueDPath = RuleDefinitionUtil.extractValueDPathFromRuleDefinition(dElement, ruleDefinition);
                 if (valueDPath != null) {
                     Pair<String, String> pr = new Pair<>(distinguisher.getXpath(), valueDPath);
diff --git a/hl7templates/hl7templates-model-jar/src/test/java/net/ihe/gazelle/tempmodel/test/DPathExtractorTest.java b/hl7templates/hl7templates-model-jar/src/test/java/net/ihe/gazelle/tempmodel/test/DPathExtractorTest.java
index c74f296..4052057 100644
--- a/hl7templates/hl7templates-model-jar/src/test/java/net/ihe/gazelle/tempmodel/test/DPathExtractorTest.java
+++ b/hl7templates/hl7templates-model-jar/src/test/java/net/ihe/gazelle/tempmodel/test/DPathExtractorTest.java
@@ -1,9 +1,5 @@
 package net.ihe.gazelle.tempmodel.test;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
 import org.junit.Test;
 
 import net.ihe.gazelle.tempmodel.dpath.model.DAttribute;
@@ -11,6 +7,8 @@ import net.ihe.gazelle.tempmodel.dpath.model.DElement;
 import net.ihe.gazelle.tempmodel.dpath.model.DParent;
 import net.ihe.gazelle.tempmodel.dpath.utils.DPathExtractor;
 
+import static org.junit.Assert.*;
+
 /**
  * 
  * @author Abderrazek Boufahja
@@ -46,7 +44,7 @@ public class DPathExtractorTest extends DPathExtractor {
 		DParent par = DPathExtractor.extractDElementFromDPath("/hl7:templateId/@root");
 		assertTrue(par instanceof DElement);
 		String path = DPathExtractor.createPathFromDParent(par);
-		assertTrue(path.equals("/hl7:templateId/@root"));
+		assertEquals("/hl7:templateId/@root", path);
 	}
 	
 	@Test
@@ -157,7 +155,7 @@ public class DPathExtractorTest extends DPathExtractor {
 		assertTrue(del == null);
 		del = createElementFromPathElement("/hl7:templateId[");
 		String path = DPathExtractor.createPathFromDParent(del);
-		assertTrue(path.equals("/hl7:templateId"));
+		assertEquals("/hl7:templateId", path);
 	}
 	
 	@Test
-- 
GitLab