Skip to content

Commit

Permalink
#210 prefix for ns
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 2, 2024
1 parent ba5f732 commit ee59c83
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,18 @@ ATTR 'price', '$49.99';
After the execution, `ATTR` doesn't move the cursor.

If it's necessary to make sure the attribute belongs to a certain
namespace, put the namespace into the attribute name separating
it with a space from the name:
namespace, put the namespace and its prefix into the

Check failure on line 207 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Trailing spaces [Expected: 0 or 2; Actual: 1]
attribute name separating them with spaces:

```text
ADD 'root';
ATTR 'noNamespaceSchemaLocation http://www.w3.org/2001/XMLSchema-instance', 'https://...';
ADD 'flower';
ATTR 'noNamespaceSchemaLocation xsi http://www.w3.org/2001/XMLSchema-instance', 'foo.xsd';
```

This will generate the following document:

```xml
<flower xsi:noNamespaceSchemaLocation="foo.xsd"/>
```

### SET
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/xembly/AttrDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ public Directive.Cursor exec(final Node dom,
final String val = this.value.raw();
final String[] parts = key.split(" ");
for (final Node node : cursor) {
if (parts.length == 2) {
Element.class.cast(node).setAttributeNS(parts[1], parts[0], val);
if (parts.length == 3) {
Element.class.cast(node).setAttributeNS(
parts[2], String.format("%s:%s", parts[1], parts[0]), val
);
} else {
Element.class.cast(node).setAttribute(key, val);
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/xembly/Transformers.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ final class Node implements Transformers {
* @since 0.30
*/
public Node() {
this.original = new Formatted(
new Default(),
this.original = new Transformers.Formatted(
new Transformers.Default(),
Collections.singletonMap(OutputKeys.OMIT_XML_DECLARATION, "yes")
);
}
Expand Down Expand Up @@ -95,9 +95,9 @@ final class Document implements Transformers {
* @since 0.30
*/
public Document() {
this.original = new Formatted(
new Default(),
Document.defaultProperties()
this.original = new Transformers.Formatted(
new Transformers.Default(),
Transformers.Document.defaultProperties()
);
}

Expand Down Expand Up @@ -139,7 +139,7 @@ final class Default implements Transformers {
* @since 0.30
*/
Default() {
this(Default.tfactory);
this(Transformers.Default.tfactory);
}

/**
Expand Down
20 changes: 12 additions & 8 deletions src/test/java/org/xembly/AttrDirectiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
* Test case for {@link AttrDirective}.
Expand Down Expand Up @@ -105,15 +106,18 @@ void addsCaseSensitiveAttributesDirectly() throws Exception {

@Test
void addAttributeWithNamespace() {
final Node node = new Xembler(
new Directives().add("boom").attr(
"noNamespaceSchemaLocation xsi http://www.w3.org/2001/XMLSchema-instance",
"foo.xsd"
)
).domQuietly();
MatcherAssert.assertThat(
new XMLDocument(node).toString(),
Matchers.containsString("xsi:noNamespaceSchemaLocation")
);
MatcherAssert.assertThat(
new XMLDocument(
new Xembler(
new Directives().add("boom").attr(
"noNamespaceSchemaLocation http://www.w3.org/2001/XMLSchema-instance",
"foo.xsd"
)
).domQuietly()
).nodes("/boom/@xsi:noNamespaceSchemaLocation"),
new XMLDocument(node).nodes("/boom/@xsi:noNamespaceSchemaLocation"),
Matchers.not(Matchers.emptyIterable())
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/xembly/XemblerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,15 @@ void omitsHeader() {
Matchers.equalTo("<animals><cow>му-му</cow></animals>")
);
}

@Test
void printsNamespaces() {
MatcherAssert.assertThat(
"XML output must have namespace prefixes",
new Xembler(
new Directives().add("flower").attr("color x html", "green")
).xmlQuietly(),
Matchers.containsString("x:color=\"green\"")
);
}
}

0 comments on commit ee59c83

Please sign in to comment.