Skip to content

Commit

Permalink
Merge pull request #2496 from codehackerr/develop
Browse files Browse the repository at this point in the history
2454: Preserve whitespace in node value when using  karate.xmlPath
  • Loading branch information
ptrthomas authored Feb 5, 2024
2 parents ccd7ee9 + 7358a8d commit 8c57a0d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion karate-core/src/main/java/com/intuit/karate/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public static Document toXmlDoc(String xml) {
public static Document toXmlDoc(String xml, boolean namespaceAware) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(namespaceAware);
factory.setIgnoringElementContentWhitespace(false);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
DtdEntityResolver dtdEntityResolver = new DtdEntityResolver();
Expand Down Expand Up @@ -294,7 +295,9 @@ public static int getChildElementCount(Node node) {
private static Object getElementAsObject(Node node, boolean removeNamespace) {
int childElementCount = getChildElementCount(node);
if (childElementCount == 0) {
return StringUtils.trimToNull(node.getTextContent());
String textContent = node.getTextContent();
return StringUtils.isBlank(textContent) ? null:
textContent;
}
Map<String, Object> map = new LinkedHashMap<>(childElementCount);
NodeList nodes = node.getChildNodes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ void testEvalXmlAndXpath() {
matchEquals("temp", "<foo><bar>baz</bar></foo>");
assign("temp", "get myMap /root/foo");
matchEquals("temp", "<foo><bar>baz</bar></foo>");

// preserves whitespace in content
assign("myXml", "<root><foo><bar> baz </bar></foo></root>");
value = engine.evalKarateExpression("$myXml/root/foo/bar");
matchEval(value.getValue(), " baz ");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Preserve white space in text content
Scenario:
* def xml =
"""
<myRoot xml:space="preserve">
<myNode> myValue </myNode>
</myRoot>
"""
* match karate.xmlPath(xml, '/myRoot/myNode') == ' myValue '

0 comments on commit 8c57a0d

Please sign in to comment.