I have xml like below, in the runtime i want to dynamically process . First time first then etc
<tag1>
<tag2>
.
.
.
</tag2>
<tag2>
.
.
.
</tag2>
</tag1>
CASE1: Working code sending static xslt parameter:
.transform(Transformers.xslt(this.config.getSome().getXslt(), xsltParameters(1 or 2)))
public Tuple2[] xsltParameters(int number)
{
final SpelExpressionParser parser = new SpelExpressionParser();
final List<Tuple2<String,Expression>> parameters = new ArrayList<>();
parameters.add(Tuples.of("Id1", parser.parseRaw(String.format("headers['%s']",ID1))));
parameters.add(Tuples.of("Id2", parser.parseRaw(String.format("headers['%s']",ID2))));
parameters.add(Tuples.of("tagNumber", parser.parseRaw(String.format("%d",number))));
return parameters.toArray(new Tuple2[]{});
}
Above is working and transformer can trasnform xslt.
CASE2: Getting error when i want to pass dynamic number:
.transform(Message.class, m-> Transformers.xslt(this.config.getSome().getXslt(), xsltParameters())
xsltparametr(){
parameters.add(Tuples.of("tagNumber",
parser.parseRaw(String.format("headers['%s']",COUNT))));
}
ERROR while transforming:
Error on line 158 of test.xslt(Prior to line 158 is working fine)
XPTY0004: A sequence of more than one item is not allowed as the first argument of starts-with() ("xxmmyty", "xxmmyty")
My xslt at line 158
<xsl:if test="starts-with(xc:tag1/xc:tag2[$count]/xc:trade/xcml:header/xcml:Identifier/xcml:tradeId[#IdScheme='urn:abcd.something'], 'XYZ')" >
<xsl:value-of select="tag" />
</xsl:if>
In CASE1 with same xslt file i can pass above code but in case of dynamic passing(CASE2) it is failing.
First of all it looks like you use old enough Spring version.
Would be grate to upgrade at least to 5.0.x.
Secondly it isn't clear what is your xsltParameters(). You hide it from us and we fully don't know what you do over there. Although we can assume that you are fully based on this method:
#SafeVarargs
public static XsltPayloadTransformer xslt(Resource xsltTemplate,
Tuple2<String, Expression>... xslParameterMappings) {
So, you build in that xsltParameters() an array of Tuple2. OK. So, get access to the header in the param you need to build an appropriate expression.
I'm rally sure that it can be a FunctionExpression:
.transform(Transformers.xslt(this.config.getSome().getXslt(),
Tuples.of("foo", new FunctionExpression<Message<?>>(m -> m.getHeader.get("int_val"))))).
What you do with the lambda can't be combined with a transformer from that factory.
UPDATE
You need to modify your xsltParameters() to accept a String instead. Which is going to be just raw expression representation.
So, your CASE1 is going to be like this:
.transform(Transformers.xslt(this.config.getSome().getXslt(), xsltParameters("'1'")))
With meaning as literal expression.
The CASE2 is going to be like this:
.transform(Transformers.xslt(this.config.getSome().getXslt(), xsltParameters("headers.int_val")))
Related
Preamble
I have a JMeter script with an XPath Extractor, in this I have specified a query that gets multiple values from the XML document. This all works fine
XML
<?xml version="1.0" encoding="UTF-8"?>
<InventoryAvailabilityAdvice>
<Warehouse>WFC2</Warehouse>
<Timestamp>2019-07-31T23:00:02.177</Timestamp>
<InventoryItem>
<ItemNumber>80903</ItemNumber>
<AvailableQuantity UnitOfMeasure="EA">13</AvailableQuantity>
</InventoryItem>
<InventoryItem>
<ItemNumber>80901</ItemNumber>
<AvailableQuantity UnitOfMeasure="EA">17</AvailableQuantity>
</InventoryItem>
</InventoryAvailabilityAdvice>
Problem
When I then try to get these values in a loop using a JSR232 Sampler they don't seem to come out in the order declared in the XPath Query.
I expected theData_2 to contain the UnitOfMeasure attribute and theData_3 to contain the quantity, but as you can see they are reversed.
Question
Is this expected behavior? If so, when an element has multiple attributes how do I know which order those will be made available as?
Thanks
The order of XPath nodesets produced by union operator is not guaranteed, you can see putValuesForXPathInList() function for implementation details
Actually if you've decided to go for Groovy - you don't even need the XPath Extractor, you can use XmlSlurper class for parsing the XML response.
Example code:
def response = new XmlSlurper().parseText(prev.getResponseDataAsString())
response.InventoryItem.eachWithIndex { item, index ->
log.info('Item: ' + index)
log.info('ItemNumber: ' + item.ItemNumber)
log.info('AvailableQuantiry: ' + item.AvailableQuantity)
log.info('UnitOfMeasure:' + item.AvailableQuantity.#UnitOfMeasure)
}
Demo:
References:
Groovy: Processing XML
Apache Groovy - Why and How You Should Use It
I have this xml that contains certificates and its attributes. Here is the xml over which I am running the extraction query
<Certificates>
<CertificateAndChain>
<Certificate>
<FriendlyName />
<Thumbprint>EE8B375347FCADDC25547FDDF61866E07D5B6A71</Thumbprint>
<SerialNumber>256BB11DB5BE889E46EBCD85608DA849</SerialNumber>
<Version>3</Version>
<SignatureAlgorithm>sha1RSA</SignatureAlgorithm>
<Issuer>CN=certname</Issuer>
<Subject>CN=certname</Subject>
<NotAfter>2040-09-26T18:30:00Z</NotAfter>
<NotBefore>2016-09-27T18:30:00Z</NotBefore>
<IsVerified>false</IsVerified>
</Certificate>
.
.
.(continued)...
I have written another test over this xml that needs to extract thumbprints of all the certificates that are about to get expired in next 10 days.
As part of the test, , I have written the Extraction query as:
<XPath2ResponseParameter Name="TPName"><ExtractionQuery><![CDATA[string(//CertificateAndChain/Certificate[NotAfter[text() <= (current-dateTime() + xs:dayTimeDuration('P10D'))]]/Thumbprint)]]></ExtractionQuery></XPath2ResponseParameter>
The test is in xml file as well. However, when I run the test, I get this exception:
A sequence of more than one item is not allowed as the first argument of string().
This is probably happening because there are multiple certificate elements present in the xml. So, I couldn't figure out the way to extract all the thumbprints failing the mentioned condition(there could be more than 1 and I need to extract all of them)
Can anyone help me fix this?
You can invoke function in axis when using XPath 2.0. So, try to move string() function to the end of the inner XPath (formatted for readability) :
//CertificateAndChain
/Certificate[
NotAfter[
text() <= (current-dateTime() + xs:dayTimeDuration('P10D'))
]
]/Thumbprint
/string()
UPDATE :
Since CDATA can't receive multiple strings in this context, according to the error message in your first comment, then you need to combine those strings into one, presumably using string-join() :
string-join(
//CertificateAndChain
/Certificate[
NotAfter[
text() <= (current-dateTime() + xs:dayTimeDuration('P10D'))
]
]/Thumbprint
/string()
,' ')
I have what I think is an interesting problem executing queries in Jackrabbit when a node in the query path is a UUID that start with a number.
For example, this query work fine as the second node starts with a letter, 'f':
/*/JCP/feeadeaf-1dae-427f-bf4e-842b07965a93/label//*[#sequence]
This query however does not, if the first 'f' is replaced with '2':
/*/JCP/2eeadeaf-1dae-427f-bf4e-842b07965a93/label//*[#sequence]
The exception:
Encountered "-" at line 1, column 26.
Was expecting one of:
<IntegerLiteral> ...
<DecimalLiteral> ...
<DoubleLiteral> ...
<StringLiteral> ...
... rest omitted for brevity ...
for statement: for $v in /*/JCP/2eeadeaf-1dae-427f-bf4e-842b07965a93/label//*[#sequence] return $v
My code in general
def queryString = queryFor path
def queryManager = session.workspace.queryManager
def query = queryManager.createQuery queryString, Query.XPATH // fails here
query.execute().nodes
I'm aware my query, with the leading asterisk, may not be the best, but I'm just starting out with querying in general. Maybe using another language other than XPATH might work.
I tried the advice in this post, adding a save before creating the query, but no luck
Jackrabbit Running Queries against UUID
Thanks in advance for any input!
A solution that worked was to try and properly escape parts of the query path, namely the individual steps used to build up the path into the repository. The exception message was somewhat misleading, at least to me, as in made me think that the hyphens were part of the root cause. The root problem was that the leading number in the node name created an illegal XPATH query as suggested above.
A solution in this case is to encode the individual steps into the path and build the rest of the query. Resulting in the leading number only being escaped:
/*/JCP/_x0032_eeadeaf-1dae-427f-bf4e-842b07965a93//*[#sequence]
Code that represents a list of steps or a path into the Jackrabbit repository:
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.util.ISO9075;
class Path {
List<String> steps; //...
public String asQuery() {
return steps.size() > 0 ? "/*" + asPathString(encodedSteps()) + "//*" : "//*";
}
private String asPathString(List<String> steps) {
return '/' + StringUtils.join(steps, '/');
}
private List<String> encodedSteps() {
List<String> encodedSteps = new ArrayList<>();
for (String step : steps) {
encodedSteps.add(ISO9075.encode(step));
}
return encodedSteps;
}
}
Some more notes:
If we escape more of the query string as in:
/_x002a_/JCP/_x0032_eeadeaf-1dae-427f-bf4e-842b07965a93//_x002a_[#sequence]
Or the original path encoded as a whole as in:
_x002f_a_x002f_fffe4dcf0-360c-11e4-ad80-14feb59d0ab5_x002f_2cbae0dc-35e2-11e4-b5d6-14feb59d0ab5_x002f_c
The queries do not produce the wanted results.
Thanks to #matthias_h and #LarsH
An XML element name cannot start with a digit. See the XML spec's rules for STag, Name, and NameStartChar. Therefore, the "XPath expression"
/*/JCP/2eeadeaf-1dae-427f-bf4e-842b07965a93/label//*[#sequence]
is illegal, because the name test 2eead... isn't a legal XML name.
As such, you can't just use any old UUID as an XML element name nor as a name test in XPath. However if you put a legal NameStartChar on the front (such as _), you can probably use any UUID.
I'm not clear on whether you think you already have XML data with an element named <2eead...> (and are trying to query that element's descendants); if so, whatever tool produced it is broken, as it emits illegal XML. On the other hand if the <2eead...> is something that you yourself are creating, then presumably you have the option of modifying the element name to be a legal XML name.
I am testing a controller using MockMvc. This is what the response looks like:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[text/xml]}
Content type = text/xml
Body = <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:diagnosisCode xmlns:ns2="http://schemas.mycompany.co.za/health" effectiveStartDate="2014-03-05T00:00:00+02:00" effectiveEndDate="2014-03-05T23:59:59.999+02:00" diagnosisId="1"><diagnosisCodeId><codingSchemaCode>irrelevant schema</codingSchemaCode><diagnosisCode>irrelevant code</diagnosisCode></diagnosisCodeId></ns2:diagnosisCode>
Forwarded URL = null
Redirected URL = null
Cookies = []
Pretty-printed version of the Body line:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:diagnosisCode xmlns:ns2="http://schemas.mycompany.co.za/health" effectiveStartDate="2014-03-05T00:00:00+02:00" effectiveEndDate="2014-03-05T23:59:59.999+02:00" diagnosisId="1">
<diagnosisCodeId>
<codingSchemaCode>irrelevant schema</codingSchemaCode>
<diagnosisCode>irrelevant code</diagnosisCode>
</diagnosisCodeId>
</ns2:diagnosisCode>
The call on MockMvc looks like
mockMvc.perform(
get("/diagnostic/diagnosisCodes/{schema}/{code}", IRRELEVANT_SCHEMA, IRRELEVANT_CODE).accept(MediaType.TEXT_XML))
.andDo(print())
.andExpect(content().contentType(MediaType.TEXT_XML))
.andExpect(status().isOk())
.andExpect(xpath("diagnosisCodeId/diagnosisCode").string(IRRELEVANT_CODE))
.andExpect(xpath("diagnosisCodeId/codingSchemaCode").string(IRRELEVANT_SCHEMA));
I am pretty sure I am misunderstanding how I'm supposed to use XPath here, but why is this assertion failing? What should my expectation look like?
java.lang.AssertionError: XPath diagnosisCode expected:<irrelevant code> but was:<>
I'm not totally sure what the XPath context is (or whether it is the document node), but I see two possible problems and guess both apply:
You try to match < diagnosisCodeId/> elements that are the root element. There are none, but they're children of <diagnosisCode>. Either include an axis step for the root node (probably better way) or use the descendant-or-self axis step // in front of the query.
/diagnosisCode/diagnosisCodeId/diagnosisCode
//diagnosisCodeId/diagnosisCode
The document uses namespaces (for the root element). In addition to the root element problem described above, either register that namespace (better solution, but I don't know how to do this in spring MVC) or ignore it using following workaround:
/*[local-name() = 'diagnosisCode']/diagnosisCodeId/diagnosisCode
Which first matches all child nodes, but then limits to the ones having the apropriate element name (ignoring namespaces).
By adding XPath 2.0 support (for example by including Saxon as library), you can also use the wildcard namespace matcher:
/*:diagnosisCode/diagnosisCodeId/diagnosisCode
If you register the namespace URI http://schemas.mycompany.co.za/health as ns2, the query would look like
/ns2:diagnosisCode/diagnosisCodeId/diagnosisCode
There is an overload for xpath that takes a Map<String, String> of namespaces:
Map<String, String> ns = Map.of("ns2", "http://schemas.mycompany.co.za/health");
mockMvc.perform(get("/diagnostic/diagnosisCodes/{schema}/{code}", IRRELEVANT_SCHEMA, IRRELEVANT_CODE)
.accept(MediaType.TEXT_XML))
.andExpect(xpath("ns2:diagnosisCodeId/diagnosisCode", ns).string(IRRELEVANT_CODE))
.andExpect(xpath("ns2:diagnosisCodeId/codingSchemaCode", ns).string(IRRELEVANT_SCHEMA));
If i have the & symbol in some field (from a db, cannot be changed), and i want to display this via freemarker... but have the display (from freemarker) read &, what is the way to do so?
To reiterate, I cannot change the value before hand (or at least, I don't want to), i'd like freemarker to "unmark" &.
To double re-iterate, this is a value that is being placed with a lot of other xml. The value itself is displayed on its own, surrouded by tags... so something like
<someTag>${wheeeMyValueWithAnAmpersand}<someTag>
As a result, i don't want all ampersands escaped, or the xml will look funny... just that one in the interpolation.
Oh goodness.
I see the problem: the code was written like this:
<#escape x as x?xml>
<#import "small.ftl" as my>
<#my.macro1/>
</#escape>
and at which i'd assumed that the excape would excape all the calls within it - it is certainly what the documentation sort of implies
http://freemarker.org/docs/ref_directive_escape.html
<#assign x = "<test>"> m1>
m1: ${x}
</#macro>
<#escape x as x?html>
<#macro m2>m2: ${x}</#macro>
${x}
<#m1/>
</#escape>
${x}
<#m2/>
the output will be:
<test>
m1: <test>
<test>
m2: <test>
However it appears that when you import the file, then this isn't the case, and the escape... escapes!
SOLUTION:
http://watchitlater.com/blog/2011/10/default-html-escape-using-freemarker/
the above link details how to solve the problem. In effect, it comes down to loading a different FreemakerLoader, one that wraps all templates with an escape tag.
class SomeCoolClass implements TemplateLoader {
//other functions here
#Override
public Reader getReader(Object templateSource, String encoding) throws IOException {
Reader reader = delegate.getReader(templateSource, encoding);
try {
String templateText = IOUtils.toString(reader);
return new StringReader(ESCAPE_PREFIX + templateText + ESCAPE_SUFFIX);
} finally {
IOUtils.closeQuietly(reader);
}
}
which is a snippet from the link above. You create the class with the existing templateLoader, and just defer all the required methods to that.
Starting from FreeMarker 2.3.24 no TemplateLoader "hack" is needed anymore. There's a setting called output_format, which specifies if and what escaping is needed. This can be configured both globally, and/or per-template-name-pattern utilizing the template_configurations setting. The recommend way of doing this is even simpler (from the manual):
[...] if the
recognize_standard_file_extensions setting is true (which is the
default with the incompatible_improvements setting set to 2.3.24 or
higher), templates whose source name ends with ".ftlh" gets "HTML"
output format, and those with ".ftlx" get "XML" output format