Skip to content

Commit acdeddf

Browse files
committedDec 2, 2024··
#207 ns for attrs
1 parent da995bb commit acdeddf

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed
 

‎src/main/java/org/xembly/AttrDirective.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ public Directive.Cursor exec(final Node dom,
7575
final Directive.Cursor cursor, final Directive.Stack stack) {
7676
final String key = this.name.raw();
7777
final String val = this.value.raw();
78+
final String[] parts = key.split(" ");
7879
for (final Node node : cursor) {
79-
Element.class.cast(node).setAttribute(key, val);
80+
if (parts.length == 2) {
81+
Element.class.cast(node).setAttributeNS(parts[1], parts[0], val);
82+
} else {
83+
Element.class.cast(node).setAttribute(key, val);
84+
}
8085
}
8186
return cursor;
8287
}

‎src/main/java/org/xembly/Directives.java

+4
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ public Directives remove() {
332332
/**
333333
* Set attribute.
334334
*
335+
* <p>If it's necessary to add an attribute with a namespace, append
336+
* the namespace to the name of the attribute, separating them
337+
* with a space.</p>
338+
*
335339
* <p>If a value provided contains illegal XML characters, a runtime
336340
* exception will be thrown. To avoid this, it is recommended to use
337341
* {@link Xembler#escape(String)}.

‎src/test/java/org/xembly/AttrDirectiveTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
package org.xembly;
3131

3232
import com.jcabi.matchers.XhtmlMatchers;
33+
import com.jcabi.xml.XMLDocument;
3334
import java.util.Collections;
3435
import javax.xml.parsers.DocumentBuilderFactory;
3536
import org.apache.commons.lang3.StringUtils;
3637
import org.hamcrest.MatcherAssert;
38+
import org.hamcrest.Matchers;
3739
import org.junit.jupiter.api.Test;
3840
import org.w3c.dom.Document;
3941
import org.w3c.dom.Element;
@@ -100,4 +102,19 @@ void addsCaseSensitiveAttributesDirectly() throws Exception {
100102
XhtmlMatchers.hasXPath("/f[@Price='\u20ac50']")
101103
);
102104
}
105+
106+
@Test
107+
void addAttributeWithNamespace() {
108+
MatcherAssert.assertThat(
109+
new XMLDocument(
110+
new Xembler(
111+
new Directives().add("boom").attr(
112+
"noNamespaceSchemaLocation http://www.w3.org/2001/XMLSchema-instance",
113+
"foo.xsd"
114+
)
115+
).domQuietly()
116+
).nodes("/boom/@xsi:noNamespaceSchemaLocation"),
117+
Matchers.not(Matchers.emptyIterable())
118+
);
119+
}
103120
}

0 commit comments

Comments
 (0)
Please sign in to comment.