Hi,
I’m using Java Aspose Reporting Engine and xml data source loaded as DataSet object,
Can I access a specific element of an array by its index? Without indexOf inside a foreach or skip() method?
My requirement is to use a simple syntax in this case, something like mentioned in the example expected code section
Example:
XML source
<person>
<name>Person's Name</name>
<address>
<street>AddressStreet1</street>
<code>AddressCode1</code>
</address>
<address>
<street>AddressStreet2</street>
<code>AddressCode2</code>
</address>
<address>
<street>AddressStreet3</street>
<code>AddressCode3</code>
</address>
<address>
<street>AddressStreet4</street>
<code>AddressCode4</code>
</address>
</person>
Template
<<foreach [in person]>>
### Working example (Workarounds)
# index 0
<<[person.address.first().street]>>
# index 3
<<[person.address.skip(2).first().street]>>
# Using foreach
<<foreach [in person.address]>><<if [indexOf() == 3]>><<[street]>><</foreach>>
### Expected code (Not Working)
# example index 3
<<[person[3].street]>>
<<[person.elementAt(3).street]>>
<<[persong.get(1).street]>>
<</foreach>
Java 17 code
public class A {
public static void B() {
Document doc = new Document(DOC_PATH);
var ds = new DataSet().readXml(XML_PATH);
ReportingEngine engine = new ReportingEngine();
engine.setOptions(
ReportBuildOptions.REMOVE_EMPTY_PARAGRAPHS +
ReportBuildOptions.ALLOW_MISSING_MEMBERS);
engine.buildReport(doc, ds, "doc");
}
}
@nhnatyshyn
First of all, syntax like <<[persong.get(1).street]>>
does not work, because it should be <<[person.address.get(1).street]>>
. However, if using syntax like that, an exception is thrown. But rewriting the expression in the following way resolves the issue: <<[person.address.get(1).get("street")]>>
.
Sorry, i completely forgot about address element in the expected code example
The correct way was supposed to be <<[person.address.get(N).street]>>
, <<[person.address[N].street]>>
and so on, as you mentioned before.
It is possible to have this in the future? The following expression <<[person.address.get(1).get("street")]>>
changes the original syntax of how templates are written, the get(“street”) method adds more logic to the document
Ps. I’ve tried <<[person.address.get(1).get("street")]>>
and it’s not working.
Having an utility class, registered in the engine with
public static String getType(Object something) {
if (something == null) {
return "Null element";
}
return something.getClass().getName();
}
<<[Util.getType(<<[person.address.get(1))]>>
returns “Null element”, and
<<[Util.getType(<<[person.address]>>
returns aspose internal object class
@nhnatyshyn Thank you for additional information.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-25259
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
The issues you have found earlier (filed as WORDSNET-25259) have been fixed in this Aspose.Words for Java 23.7 update.
@nhnatyshyn
We have added support of elementAt
and elementAtOrDefault
extension methods for enumerations to LINQ Reporting Engine template syntax. They can be used as follows:
<<[persons.elementAt(3).getName()]>>
<<[persons.elementAtOrDefault(5)?.getName()]>>