The first thing to underline is that, while in XPath 1.0 namespace declarations were optional, and XPath 1.0 is quite common in Java and web applications, they are not optional in modern versions of XPath. In order to avoid namespace declaration you can use the * operator to mark *:your-element-name now. A query like //the-element-name won't give you results with a document like:
> First non root element > Second non root element > > >
In contrast a query like //:the-element-name will give you results on XPath 3.x and following. Also, if no namespace would be used on the document, a no namespace query on XPath 3.x will give you results.
Working with namespaces on Xesta
-
Documents without namespaces. In this case you do not need to use wilcards on the mappings. Simple expressions, similar to the ones in XPath 1.0, would work:
@Bind(expression = "/project")public class MavenProject implements Serializable {
public MavenProject() {super();}@Bind(expression = "modelVersion")private String modelVersion;@Bind(expression = "groupId")private String groupId;@Bind(expression = "*:artifactId")private String artifactId;...
@Bind(expression = "./reporting/plugins", classNames = "net.vionta.xml.xesta.test.beansamples.pom.both.Plugin")private ArrayList plugins = new ArrayList();...In this case we are using a maven document without namespaces like:
> 4.0.0 > com.some.library.group > somename > pom > Some Project Name > ...
Using wildcards is another common way to query documents with namespace declarations when we don't need anything specific about any of them. In the following example we will use it with a ISO2022 file.
@Bind(expression = "//*:CstmrCdtTrfInitn" )public class CheckNoNS implements Serializable {
public CheckNoNS() {}
@Bind(expression = ":PmtInf/:PmtMtd")private String paymentMethod;
@Bind(expression = ":PmtInf/:Dbtr/*:Nm")private String debtorName;
@Bind(expression = ":PmtInf/:DbtrAcct/:Id/:Othr/*:Id")private String debtorAccount;...
The sample file would start like this, and could be reviewed at https://developer.gs.com/docs/services/transaction-banking/iso20022samples/.
> > > > aeoueaoueaouoea > 2024-05-10T16:10:02.017+00:00 > 1 > 510.24 > > > > > Client-Id > ...
Using Q{} syntax (not recommended). One only namespace for line and element could be especified using Q{} syntax. Although simple, this approach is not recommended.
@Bind(expression = "//QCstmrCdtTrfInitn" )public class Check implements Serializable {
public Check() {}
@Bind(expression = "QPmtInf/*:PmtMtd")private String paymentMethod;
@Bind(expression = "QPmtInf/:Dbtr/:Nm")private String debtorName;...
Using namespace alias and uris. This is the most complete and recommended way if namespaces are needed and wildcards are not enough.
@Bind(expression = "//iso:CstmrCdtTrfInitn" , namespaceAlias = {"iso"} , namespaceUris = {"urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"})public class CheckNS implements Serializable {
public CheckNS() {}
@Bind(expression = "iso:PmtInf/iso:PmtMtd" , namespaceAlias = {"iso"} , namespaceUris = {"urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"})private String paymentMethod;
@Bind(expression = ":PmtInf/:Dbtr/*:Nm")private String debtorName;
@Bind(expression = ":PmtInf/:DbtrAcct/:Id/:Othr/*:Id")private String debtorAccount;...
More than one namespace could be added, like you could see in the following example (styles list).
@Bind(expression="office:document-styles", namespaceAlias = {"office"} ,namespaceUris = {"urn:oasis:names:tc:opendocument:xmlns:office:1.0"})public class Document implements Serializable {
@Bind(expression="office:styles", namespaceAlias = {"office", "style"},namespaceUris = {"urn:oasis:names:tc:opendocument:xmlns:office:1.0","urn:oasis:names:tc:opendocument:xmlns:style:1.0"})private List styles = new ArrayList();...
Another example could be checked at the following property
@Bind(expression="./style:text-properties/@fo:font-size", namespaceAlias = {"style","fo"}, namespaceUris = {"urn:oasis:names:tc:opendocument:xmlns:style:1.0", "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"})private String fontSize;...
Like in any XPath query, wildcards and namespaces can be mixed
@Bind(expression="./*:text-properties/@fo:font-size", namespaceAlias = {"style","fo"}, namespaceUris = {"urn:oasis:names:tc:opendocument:xmlns:style:1.0", "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"})private String fontSize;...
Jump to content