XSL - Externalizing Xpath queries to a property file - xpath

I went through various posts, regarding reading properties from external property files. Looks like there is a function - getProperty, which can read values from a property file, using a key. I am using saxon parser with spring integration. I am trying something like this, as described in the post :-
spring context file:
<int-xml:xslt-transformer id="xsltTransformer" input-channel="bulkStringInboundChannel"
output-channel="toBridgeChannel" result-type="StringResult" **transformer-factory-class="net.sf.saxon.TransformerFactoryImpl"**
xsl-resource="classpath:/META-INF/spring/integration/intake/intake-flow/bulkTransformer.xsl" />
XSL style sheet:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
....
<xsl:variable name="props" select="document('prop.xml')" />
<xsl:value-of select="f:getProperty('query1')"/>
....
Prop.xml:
query1 = /Batch/RequestID/text()
Error description:
XPST0003: XPath syntax error at char 23 on line 30 in {f:getProperty('query1')}:
XTSE0650: No template exists named getProperty
I now have two questions- first of all, how do I get rid of these errors?
Second, can I store xPath queries in property files? The post describes a method, to read a property file and use the value pertaining to its key. However, I am thinking that getProperty will just print the query's text equivalent instead of evaluating the query and processing it. Is there a way to achieve this?
Post - How to read a .properties file inside a .xsl file?

I can't help you with the Spring side of the question, but as for the Saxon side, you can call the JDK method System.getProperty() using code like this:
<xsl:value-of select="System:getProperty('user.dir')" xmlns:System="java:java.lang.System"/>
Java extensibility requires Saxon-PE or higher.
If the value of the property that you read is an XPath expression, you can then execute it using the XSLT 3.0 xsl:evaluate instruction - which also requires Saxon-PE or higher.

Related

Update parameter value in XML format

I have parameters stored in an XML file. Below is a sample of the file.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<terminal id="A">
<terminalCapacity>3</terminalCapacity>
<terminalMembers id="1">
<memberID>0001</memberID>
<memberCapacity>2</memberCapacity>
</terminalMembers>
</terminal>
<terminal id="B">
<terminalCapacity>4</terminalCapacity>
<terminalMembers id="1">
<memberID>0002</memberID>
<memberCapacity>1</memberCapacity>
</terminalMembers>
<terminalMembers id="2">
<memberID>0003</memberID>
<memberCapacity>3</memberCapacity>
</terminalMembers>
</terminal>
</root>
Each terminalID is associated to a type of simpleModule found in my NED file. The idea is to programmatically update these values throughout the simulation run. The current logic revolves around getting the current parameters in XML format and update the memberCapacity field.
From the Omnet cPar and cXMLElement documentation, I tried using the par("moduleParameter").xmlValue()->getXML() function, but this returns the XML as a string. I also tried using the getAttribute() function, but to no success.
Don't do this. par("moduleParameter").xmlValue() will give you the in memory object tree of the XML document, but that is not meant for modification. Your XML file seems to be just a hierarchical structure and modules and their parameters can mirror that exactly. There is absolutely no reason to reinvent the wheel when you can mirror that with INI file parameters.

How to extract values (list and unique tags) from xml in Nifi?

I have a xml file that contains some tags, one is unique other are same tags. I'd like to get values from these tags and convert them to another xml tag. Here is example of xml file:
<?xml version="1.0" encoding="UTF-8"?>
<entry dataset="Swiss-Prot" created="2005-04-26" modified="2019-11-13" version="158" xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<accession>Q96IY4</accession>
<accession>A8K464</accession>
<accession>Q15114</accession>
<accession>Q5T9K1</accession>
<accession>Q5T9K2</accession>
<accession>Q9P2Y6</accession>
<name>CBPB2_HUMAN</name>
<protein>
<recommendedName>
<fullName>Carboxypeptidase B2</fullName>
<ecNumber evidence="7">3.4.17.20</ecNumber>
</recommendedName>
<alternativeName>
<fullName>Carboxypeptidase U</fullName>
<shortName>CPU</shortName>
</alternativeName>
<alternativeName>
<fullName>Plasma carboxypeptidase B</fullName>
<shortName>pCPB</shortName>
</alternativeName>
<alternativeName>
<fullName>Thrombin-activable fibrinolysis inhibitor</fullName>
<shortName>TAFI</shortName>
</alternativeName>
</protein>...other tags
</entry>
I though of using EvaluateXQuery processor and EvaluateXPath. EvaluateXQuery is using for getting accession and alternativeName tags. EvaluateXPath is using for getting name, recommendedName (fullName) tags.
Is it possible to use both and combine them to only xml or hust necessary to use EvaluateXQuery processor, like this:
<accessions>Q96IY4, A8K464, Q15114,...</accessions>
<name>CBPB2_HUMAN</name>
<recommendedName>Carboxypeptidase B2</recommendedName>
<alternativeNames>Carboxypeptidase U-CPU, Plasma carboxypeptidase B-pCPB,...</alternativeNames>
Can you help me, please?
Thank you!
Hi you may use processor execute groovy script and parse xml with a script to get your values and put them into attributes.

Questions about xPath structure with SoapUI and Property Transfer - some basics needed

I am now trying another xpath property transfer in SoapUI, and am struggling again.
Rather than specifics I thought I would ask some general questions.
If the response I am wanting to extract from has a line like this:
<ns2:getApprovedPortChangeRequestsResponse xmlns:ns2="http://transferobjects.abc.abc.org">
Oddly, when I click the ns: button in SoapUI it generates the following:
declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/';
declare namespace ns1='http://transferobjects.abc.abc.org';
declare namespace ns2='http://abc.abc.org/api/serviceorder';
But the message response is pretty clear with the ns2 line above
Then I think that I can safely assume that I should do this:
declare namespace ns2='getApprovedPortChangeRequestsResponse';
Now, when I do the part describing what I want to capture, I am using the ns2 tag, following by the section names that follow as I go in to the message, in this case two layers:
//ns2:return/approvedPortChangeRequests/#version
The value I want is the value of the field called version, don't know if I want the # symbol this time, it's a numeric value, but I get null regardless of whether the # is there or not. I have thoroughly checked the response message and pretty certain I have it right. There are a couple of other sections in the response above the field, but they are at the same level as , from what I can see.
I have tried including the getApprovedPortChangeRequestsResponse as a parent layer in the last line, with no effect.
Only when I use getApprovedPortChangeRequestsResponse in the ns2 declaration can I get anything other than Null, and then only verbose errors like this:
[net.sf.saxon.trans.XPathException: XPath syntax error at char 7 on line 2 in {\n//ns2:/return}:
QName cannot end with colon: {ns2:}]
Basically, I am utterly ignorant and my googlefu hasn't shown me any resource where I can build any sort of understanding of what I am doing, so any suggestion on that front would be appreciated. I just need a couple of examples of doing this in SoapUI, and I should be sweet.
Thanks in advance.
EDIT- Full Response here:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getApprovedPortChangeRequestsResponse xmlns:ns2="http://transferobjects.abc.abc.org">
<return>
<success>true</success>
<approvedPortChangeRequests>
<lspOverride>false</lspOverride>
<numbers>
<complete>false</complete>
<gainingCompanyId>11667</gainingCompanyId>
<losingCompanyId>11657</losingCompanyId>
<notRequired>false</notRequired>
<phoneNumber>
<phoneNumber>098453509</phoneNumber>
</phoneNumber>
</numbers>
<DateTimeStart>2018-03-07T08:00:00+13:00</DateTimeStart>
<som>6001309</som>
<category>Simple</category>
<requestDateTime>2018-03-07T12:05:25+13:00</requestDateTime>
<requesterResellerId>21</requesterResellerId>
<responderResellerId>1</responderResellerId>
<responseDue>
<actualDays>0</actualDays>
<actualHours>1</actualHours>
<actualMinutes>47</actualMinutes>
<businessDays>0</businessDays>
<businessHours>1</businessHours>
<businessMinutes>47</businessMinutes>
<negative>false</negative>
</responseDue>
<status>
<status>Awaiting APC Approval</status>
</status>
<version>1</version>
</approvedPortChangeRequests>
</return>
</ns2:getApprovedPortChangeRequestsResponse>
</soap:Body>
</soap:Envelope>
In your payload, version is an element rather than an attribute, so you don't need the #.
soapUI tries to help you out by declaring namespaces, but you can go simpler. If version is all you need and there's only one, you can navigate directly to it regardless of namespaces by using //version. The // notation will select a node no matter where it is in the payload.

camel:when on header value using blueprint

I have camel routes that make rest calls based on header values.
I had been using xpath to read values from xml and set them as the header and used xpath in a block as so:
<camel:setHeader headerName="clear">
<xpath>/TicketInfo/TicketData/Clear/text()</xpath>
</camel:setHeader>
<camel:choice>
<camel:when>
<camel:xpath>$clear='CLEARED'</camel:xpath>
<camel:doTry>
...
but now I am forced to use json so xpath will not work. I now have:
<camel:setHeader headerName="clear">
<camel:jsonpath>$.ticket.Type</camel:jsonpath>
</camel:setHeader>
<camel:choice>
<camel:when>
<camel:xpath>$clear='CLEARED'</camel:xpath>
<camel:doTry>
...
but obviously the <camel:xpath>$clear='CLEARED'</camel:xpath> part won't work anymore. Is there another way I can check the value of $clear header to restrict when the <camel:doTry> and following execute?
Try the simple language :
<camel:when>
<camel:simple>${in.header.clear} == 'CLEARED'</camel:simple>
<camel:doTry>
See this documentation

How to move property value from Properties file to payload object using spring integration elements

Sample.properties
=================
http.header.amisys.accept.value=arun/vnd.dsths.services-v1+xml
1)Above XSL automatically loaded when my server starts.
2)I have tried <int:enricher> element but it is not helped me.
Sample Code : Below is bit of code I have tried, Can any one suggest me on this.
<int:channel id="PQLegacySecurity-InputChannel" />
<int:chain input-channel="PQLegacySecurity-InputChannel" >
<!-- Split the Search Request Params from Xml -->
<int-xml:xpath-splitter>
<int-xml:xpath-expression expression="//LegacySecurity" namespace map="xmlMessageNamespace" />
</int-xml:xpath-splitter>
<int:enricher >
<int:payload name="testPayload" expression="${http.header.amisys.accept.value}"/>
</int:enricher>
</int:chain>
Actual Payload Object:Below is the xml which does not contain testPayload property.
<?xml version="1.0" encoding="UTF-8"?><LegacySecurity>
<businessArea>%%%%%%</businessArea>
<LegacySystem>%%%%%</LegacySystem>
<LegacyUserID>%%%%%</LegacyUserID>
<LegacyPassword>%%%%%</LegacyPassword>
<OtherLogin/>
<OtherPassword/>
<AddSecurLogin/>
<AddSecurPassword/>
</LegacySecurity>
Expected Payload Object: Below Object contains new element testPayload node which I should able to add
<?xml version="1.0" encoding="UTF-8"?><LegacySecurity>
<businessArea>%%%%%%</businessArea>
<LegacySystem>%%%%%</LegacySystem>
<LegacyUserID>%%%%%</LegacyUserID>
<LegacyPassword>%%%%%</LegacyPassword>
<OtherLogin/>
<OtherPassword/>
**<testPayload>arun/vnd.dsths.services-v1+xml</testPayload>**
<AddSecurLogin/>
<AddSecurPassword/>
</LegacySecurity>
You can use an xslt transformer. Something like the below, though you will need to figure out correct use of the transformer from the spring docs.
Notice you can pass a parameter through to the XSLT
<int-xml:xslt-transformer result-transformer="toDocumentTransformer" result-type="StringResult" xsl-resource="/xslt/addTestPayload.xslt">
<int-xml:xslt-param name="testPayload" value="${http.header.amisys.accept.value}"/>
</int-xml:xslt-transformer>
In the XSLT file, use this to pick up the parameter:
<xsl:param name="testPayload" />
If you have other changes you need to make to the message you can use the same xslt.

Resources