How to access jcr:root with XPath JCR Query - xpath

I would like to apply some java function on CQ5 dialogs. In the first step I search for dialog xml files in myComponent folder as follow:
NodeIterator tabRequiredFields = getQueryResult("/jcr:root/apps/myProject/pages/myComponent/dialog/jcr:root")
But this Query does not supply any results. crx Xpath tool does not show any result too see the follwoing picture:
my /jcr:root/apps/myProject/pages/myComponent/dialog.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="cq:Dialog"
stateful="false"
title="Test"
.....>
<items
jcr:primaryType="cq:Widget"
xtype="panel">
...
......
</items>
.....
</jcr:root>
I can access items as follow
NodeIterator tabRequiredFields = getQueryResult("/jcr:root/apps/myProject/pages/myComponent/dialog/items")
This works fine. My Question is: why for jcr:root? how to check, if jcr:root exists?

XML element named jcr:root from the dialog.xml doesn't create jcr:root node in the repository. It's a special, reserved identifier and CRX Package Manager puts all properties and subnodes of this element into a node which name is the same as name of the file without extension (in your case it'll be dialog).
If it's not clear, use CRX DE, open /apps/myProject/pages/myComponent and see what you can find there. That's why you should add /dialog rather than /jcr:root to the end of your path.
If you want to find all dialogs, use the primary type cq:Dialog, as rakhi4110 suggests. Following query:
/jcr:root/apps/myProject/pages//element(*, cq:Dialog)
will return all dialogs from /apps/myProject/pages (and descendants).

Related

getting first node of xml in logicapps

I need to have first node of below xml that is inside of FIToFICstmrCdtTrf. However, xpath returns binary data.
xpath(xml(triggerBody()),'/')
xpath(xml(triggerBody())x,'/FIToFICstmrCdtTrf')
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
<FIToFICstmrCdtTrf>
<GrpHdr>
<MsgId>x</MsgId>
<CreDtTm>x</CreDtTm>
<NbOfTxs>2</NbOfTxs>
....
Your XML is bound to the namespace urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02, so unless you register the namespace in order to be able to use a namespace-prefix in your XPath, you need to address the elements by their local-name().
For instance: /*[local-name()="Document"]/*[local-name()="FIToFICstmrCdtTrf"]
and then to select the first child element (GrpHdr), you can append a generic expression selecting any element * and apply a predicate filter to select the first: /*[1]
Putting it all together:
xpath(xml(triggerBody()),
'/*[local-name()="Document"]/*[local-name()="FIToFICstmrCdtTrf"]/*[1]')
After reproducing from my end, I was able to get this working by applying the below expressions in the flow of my logic app:
xpath(xml(variables('XML')),'/*[local-name()="Document"]/*[local-name()="FIToFICstmrCdtTrf"]/*')?[0]
OR
xpath(xml(variables('XML')),'/*[name()="Document"]/*[name()="FIToFICstmrCdtTrf"]/*')?[0]
Below is the flow of my logic app
RESULTS:

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.

Loading multiple packages in a single kie session?

I have two packages a and b having different rule files. Right now I am using two sessions to load the rules. Is there a way I can load rules from both the packages in a single session?
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="rules_a" packages="rules_a">
<ksession name="ksession1"/>
</kbase>
<kbase name="rules_b" packages="rules_b">
<ksession name="ksession2"/>
</kmodule>
Can I pass something like: packages = {"rules_a", "rules_b"} ??
As stated in this section of the documentation, you can pass a comma separated list of packages when building a KieBase.
Another possibility is to create a KieBase that includes others. That same section of the documentation shows how to do it.
Hope it helps,
There is a packages attribute of kiebase, below is an example of how to include the packages rules1, rules2 in kiebase.
<kbase name="rules_12" packages="rules1,rules2">
From drools documentation:

Replace element by XPath

I try to replace some element of my input XML in Citrus Framework.
My Spring context contains:
<citrus:namespace-context>
<citrus:namespace prefix="def" uri="http://sample.com/xmlns/2005"/>
</citrus:namespace-context>
My input file starts with:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<ns0:canonicalMessageHeader xmlns:ns0="http://sample.com/xmlns/2005">
<ns0:headerVersion>1.0</ns0:headerVersion>
<ns0:bodyVersion>1.0</ns0:bodyVersion>
<ns0:trackingInfo>
<ns0:eventHandlerInitInfo>
<ns0:processInfo>
<ns0:adapterTrackingId>214F27DF-E1FB-4E84-9122-390C5876ABD2:1</ns0:adapterTrackingId>
...
My endpoint is configured in that way:
<send endpoint="jms:topic:Order.Request?timeout=10000&connectionFactory=DEVconnectionFactoryFrom">
<message>
<resource file="com/sample/citrus/messages/input/SalesOrderTo.xml"/>
<element value="${track}" path="SOAP-ENV:Envelope/SOAP-ENV:Header/def:canonicalMessageHeader/def:trackingInfo/def:eventHandlerInitInfo/def:processInfo/def:adapterTrackingId"/>
I have the following error:
Can not evaluate xpath expression 'SOAP-ENV:Envelope/SOAP-ENV:Header/def:canonicalMessageHeader/def:trackingInfo/def:eventHandlerInitInfo/def:processInfo/def:adapterTrackingId'
at com/sample/citrus/SalesOrderToIT(sequential:45)
at com/sample/citrus/SalesOrderToIT(send:48-82)
Caused by: javax.xml.xpath.XPathExpressionException: org.apache.xpath.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: def
What's the possible cause of this error?
Best Regards
Global namespace declaration support is missing in Citrus when overwriting message elements in a send operation via XPath. This issue has been tracked: https://github.com/christophd/citrus/issues/331
In the meantime you have to use the exact same namespace prefix as in the message template file - in your case ns0:
Also you could throw away XPath overwrite and use the dot notated Node overwrite like this:
<send endpoint="jms:topic:Order.Request?timeout=10000&connectionFactory=DEVconnectionFactoryFrom">
<message>
<resource file="com/sample/citrus/messages/input/SalesOrderTo.xml"/>
<element value="${track}" path="Envelope.Header.canonicalMessageHeader.trackingInfo.eventHandlerInitInfo.processInfo.adapterTrackingId"/>
</message>
</send>
The dot notation is not based on namespaces but uses the local element names for finding the element in the message template. Obviously not as powerful as XPath but it works with current version of the framework.
You defined nso as namespace prefix in XML but then use def on the XPath, should be nso.

Correct Xpath -

I have a xml like -
<?xml version="1.0" encoding="UTF-8"?>
<oslc_cm:Collection xmlns:oslc_cm="http://open-services.net/xmlns/cm/1.0/"
oslc_cm:totalCount="7"
xmlns:dc="http://purl.org/dc/terms/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rtc_cm="http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/">
<rtc_cm:Action rdf:resource="https://rtc.gsissc.myatos.net:9443/ccm/oslc/workflows/_ezLt4PJ7EeGRg6GNKwqw9g/actions/com.ibm.team.workitem.defectWorkflow/com.ibm.team.workitem.defectWorkflow.action.resolve">
<dc:identifier>com.ibm.team.workitem.defectWorkflow.action.resolve</dc:identifier>
<rtc_cm:resultState rdf:resource="https://rtc.gsissc.myatos.net:9443/ccm/oslc/workflows/_ezLt4PJ7EeGRg6GNKwqw9g/states/com.ibm.team.workitem.defectWorkflow/3"/>
<dc:title>Resolve</dc:title>
<rtc_cm:iconUrl>https://rtc.gsissc.myatos.net:9443/ccm/service/com.ibm.team.workitem.common.internal.model.IImageContentService/processattachment/_ezLt4PJ7EeGRg6GNKwqw9g/workflow/resolve.gif</rtc_cm:iconUrl>
</rtc_cm:Action>
From it I have to fetch rtc_cm:resultState rdf:resource.
What would be the correct XPath for it. I am using XPathExpression xPathExpressionDescription = xpath.compile("../rtc_cm:resultState/#rdf:resource"); which is giving me null pointer.
Please guide me .
../rtc_cm:resultState/#rdf:resource
That's assuming the context node – the node to which your XPath expression is relative – is a sibling of rtc_cm:resultState.
You might be better with an absolute path, based on the document root. Something like:
/oslc_cm:Collection/rtc_cm:Action/rtc_cm:resultState/#rdf:resource
(and, of course, assuming you are passing an implementation of IXmlNamespaceResolver set up with all the applicable namespaces).
The absolute path would be:
/oslc_cm:Collection/rtc_cm:Action/rtc_cm:resultState/#rdf:resource
If you know there is only one instance of resultState or if you want to query for all instances:
//rtc_cm:resultState/#rdf:resource
Please make sure that your namespace manager has definitions for the namespaces you use in your XPath expression.

Resources