I have the following SOAP Request and i need to extract the IP Address parameter value inside the XSLT template.
SOAP REQUEST:
<soapenv:Envelope
xmlns:ws="http://diamondip.com/ipcontrol/ws/"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<soapenv:Header />
<soapenv:Body>
<ws:deleteDevice soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<inpDev xsi:type="ser:WSDevice"
xmlns:ser="http://service.ipcontrol.diamondip.com"
>
<ipAddress xsi:type="soapenc:string"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
>xxx.xxx.xx.xx</ipAddress>
</inpDev>
</ws:deleteDevice>
</soapenv:Body>
</soapenv:Envelope>
I tried using the following to get the value,but it didn't work
<xsl:variable name="ipAddress" select="soapenv:Envelope/soapenv:Body/ws:deleteDevice/inpDev/ipAddress/text()"/>
Appreciate any advise!
The one thing we don't know from your question is what the current context node is when you do the xsl:variable. It should work if your current context node is the root node.
Try changing the XPath to an absolute path (put a forward slash at the beginning). If that doesn't work, make sure all the namespaces are defined correctly in your XSLT.
You can use below code to navigate and get the output.
<xsl:stylesheet version ="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match ='/'>
<xsl:variable name="ipAddress" select="/*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[local-name() = 'deleteDevice']/*[local-name() = 'inpDev']/*[local-name() = 'ipAddress']/text()"/>
IP Adress: <xsl:value-of select = "$ipAddress"/>
</xsl:template>
</xsl:stylesheet>
Related
Im using wso2 EI 6.5.0
in my proxy service i need to get element between these 2 tags <aa></aa>
xpath //tem:Request is working but /tem:Request/xDoc/aa does not work
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:Request>
<xDoc>
<aa>
<bb>
....
</bb>
<cc>
<Parameter>
......
</Parameter>
</cc>
</aa>
</xDoc>
</tem:Request>
</soapenv:Body>
</soapenv:Envelope>
You have to use either /soapenv:Envelope/soapenv:Body/tem:Request/xDoc/aa or $body/tem:Request/xDoc/aa.
The purpose of using "//" in an xpath is to directly access a particular element. But if we use "/", we need to specify the entire path to traverse through the XML tags and reach a particular element.
On a different note, if you want to access the tag <aa/> then you can directly use the xpath //aa.
I have a SOAP response message which contains a node value
similar to string A_B_C_D. I need to split each
value based on the underscore (_) and set them in
separate properties.
I checked with Xpath tokenize function but could not find a
way to get the values like array[1], array[2].. separately.
I also did some reading on XSLT mediator but not sure whether
it will help me to achieve this.
Please guide me on how to achieve this objective
Thanks
You can get value like "array[1]" but can't apply tokenize directly on soap:Body with ESB 4.8.1 :
<property xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fn="http://www.w3.org/2005/xpath-functions" name="MY_VALUE" expression="//soapenv:Body/myNode"/>
<property xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fn="http://www.w3.org/2005/xpath-functions" name="Element1" expression="fn:tokenize(syn:get-property('MY_VALUE'),'_')[1]"/>
With this message :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<myNode>A_B_C</myNode>
</soapenv:Body>
</soapenv:Envelope>
property 'Element1' contain : 'A'
so I have this hl7 type message that I have to transform using either regex or xslt or combination of two.
Format of this message is DateTime(as in YYYYMMDDHHMMSS)^UnitName^room^bed|). Each location is separated with a pipe, so each person can have one or multiple locations.
And the messages looks like this( when a patient has only one location):
20130602201605^Some Hospital^ABFG^411|
End xml result should look like this:
<Location>
<item>
<when>20130602201605</when>
<UnitName>Some Hospital</UnitName>
<room>ABFG</room>
<bed>411</bed>
</item>
</Location>
I would probably use substring type of function if it was only one location.
The problem I am running into is when there is more than one. I am relatively new to xslt and regex in general so I don't know how to use recursion in these instances.
So if I have a message like this with multiple locations:
20130601003203^GBMC^XXYZ^110|20130602130600^Sanai^ABC^|20130602150003^John Hopkins^J615^A|
The end result should be:
<Location>
<item>
<when>0130601003203</when>
<UnitName>GBMC</UnitName>
<room>XXYZ</room>
<bed>110</bed>
</item>
<item>
<when>20130602130600</when>
<UnitName>Sanai</UnitName>
<room>ABC</room>
<bed></bed>
</item>
<item>
<when>20130602150003</when>
<UnitName>John Hopkins</UnitName>
<room>J615</room>
<bed>A</bed>
</item>
</Location>
So how would I solve this? Thanks in advance.
Given that your Hl7 message is "|^~\&" encoded and not in an XML format, it is not clear how you will be using an XSLT 1.0 processor for your task. Can you describe your processing pipeline in greater detail? Your snippets are not complete messages, and it is not clear whether you will be starting with complete messages or attempting to parse isolated fields handed to a larger processing task through parameters or something.
If your processing starts with a complete HL7 message, I would suggest looking into the HAPI project, or a similar set of libraries, to have the messages converted from |^~\& to </> format, then invoking your XSLT on that version of the data. (You could also use the HAPI libraries in a full-Java solution. In either case, there are code examples at the HAPI site and at an Apache site on HL7.) If you are not interested in using Java at all, but are open to partial non-XSLT solutions, there are other projects that provide similar serialization options (e.g., Net::HL7 for Perl, nHAPI for VB/C#, etc.).
If you have isolated "|^~\&" encoded data in an otherwise XML formatted file, then I would suggest looking into the str:tokenize function in the XSLT 1.0 exslt functions. (XSLT 2.0 has a built-in tokenize function.) You can have str:tokenize split your data on the field or component separators, then create elements using the tokenized substrings.
Here is a stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="data">
<Location>
<xsl:for-each select="str:tokenize(.,'|')">
<xsl:call-template name="handle-field">
<xsl:with-param name="field" select="."/>
</xsl:call-template>
</xsl:for-each>
</Location>
</xsl:template>
<xsl:template name="handle-field">
<xsl:param name="field"/>
<xsl:variable name="components" select="str:tokenize($field,'^')"/>
<item>
<when><xsl:value-of select="$components[1]"/></when>
<UnitName><xsl:value-of select="$components[2]"/></UnitName>
<room><xsl:value-of select="$components[3]"/></room>
<bed><xsl:value-of select="$components[4]"/></bed>
</item>
</xsl:template>
</xsl:stylesheet>
that runs over this input
<?xml version="1.0" encoding="UTF-8"?>
<data>20130601003203^GBMC^XXYZ^110|20130602130600^Sanai^ABC^|20130602150003^John Hopkins^J615^A|</data>
to produce this output with xsltproc:
<?xml version="1.0"?>
<Location>
<item>
<when>20130601003203</when>
<UnitName>GBMC</UnitName>
<room>XXYZ</room>
<bed>110</bed>
</item>
<item>
<when>20130602130600</when>
<UnitName>Sanai</UnitName>
<room>ABC</room>
<bed/>
</item>
<item>
<when>20130602150003</when>
<UnitName>John Hopkins</UnitName>
<room>J615</room>
<bed>A</bed>
</item>
</Location>
Your source message is in a string form, you need to create a parser that uses regex to split the message based on first pipes and then carat. refer to Unable to parse ^ character which has my original code for the parser and the solution gives a different approach to it.
After you have individual elements you need to add it to your xml as nodes.
I am using the below shown snippet of choice element in my Mule 3.3 flow. XSL Transformer feeds the choice element. XSL Transformer is supposed to return a String (name of an entity) and on the basis of string value, I use choice router to push it to different jms queues.
<flow name="ProcessOrder">
.
.
<xm:xslt-transformer xsl-file="xsl/getEntity.xslt" returnClass="java.lang.String"/>
<choice>
<when expression="payload.contains('ABC')">
<jms:outbound-endpoint queue="order.queue1" />
</when>
<when>
</when>
<otherwise>
</otherwise>
</choice>
</flow>
XSL Transformer returns this payload
<?xml version="1.0" encoding="UTF-8"?>ABC
My question is how do I compare the String returned. I don't think payload.contains() is the best way to do this, though it solves my purpose and also we won't have matching entities returned which are ever like ABCxy but still this is not a full proof solution.
Add the omit-xml-declaration part in your xslt as shown below. This will give you the raw string without the prolog.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
...
...
This will give
"ABC" as output instead of "<?xml version="1.0" encoding="UTF-8"?>ABC"
Then in the expression use it like
<when expression="#[message.payload.contains('ABC')]">
This way it should work.
Maybe this is what you are looking for:
<when evaluator="xpath" expression="/result/" ...
Obviously, your XSLT will need to retur a well-formed XML document with the desired result in an XML element that is neatly accessible by XPath.
On Mule website they suggest to use expression-splitter-router evaluator this is an example from mule website of how to use it:
FruitBowl containing an apple, an orange, and two bananas. When Mule receives this object, we want to route the fruit to different locations: the AppleService, BananaService, and OrangeService.
<service name="Distributor">
<inbound>
<jms:inbound-endpoint queue="distributor.queue"/>
</inbound>
<outbound>
<!-- FruitBowl.getFruit() List -->
<expression-splitter-router evaluator="bean" expression="fruit">
<vm:outbound-endpoint path="apple.service.queue">
<payload-type-filter expectedType="org.mule.tck.testmodels.fruit.Apple"/>
</vm:outbound-endpoint>
<vm:outbound-endpoint path="banana.service.queue">
<payload-type-filter expectedType="org.mule.tck.testmodels.fruit.Banana"/>
</vm:outbound-endpoint>
<vm:outbound-endpoint path="orange.service.queue">
<payload-type-filter expectedType="org.mule.tck.testmodels.fruit.Orange"/>
</vm:outbound-endpoint>
</expression-splitter-router>
</outbound>
</service>
Hope that helps
I'm kind of new to XSLT, and I've gotten basic transformation done. Next I want to try out date manipulations, since my data will have timestamps. However, I can't seem to get any date functions to work, and it greatly frustrates me. I'm testing using Firefox 3.5, xsltproc 1.1.24, xalan 1.10, and XMLSpy 2009, and they all say that the functions I'm trying to use don't exist.
My xml looks like so:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="datetime.xsl"?>
<watcher>
<event id="1" date="2009-09-04T13:49:10-0500" type="ABCD">This is a test </event>
</watcher>
</code>
My xsl looks like so:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="event[#type='ABCD']">
<!-- Date: <xsl:value-of select="day-from-dateTime(xs:dateTime(#date))"/> -->
<!-- Date: <xsl:value-of select="day-from-dateTime(#date)"/> -->
Date: <xsl:value-of select="fn:day-from-dateTime(#date)"/>
</xsl:template>
</xsl:stylesheet>
If I make the stylesheet version 2, XMLSpy complains that it can't cast my date: XSLT 2.0 Debugging Error: Error in XPath 2.0 expression (Cast failed, invalid lexical value - xs:dateTime '2009-09-04T13:49:10-0500')
If I leave it as version 1, it complains about a different error: XSLT 1.0 Debugging Error: Error in XPath expression (Unknown function - Name and number of arguments do not match any function signature in the static context - 'day-from-dateTime')
Anytime I try to change the XSL to use a namespace, such as fn:day-from-dateTime, it refuses to work at all, with all of my parsers saying that The function number 'http://www.w3.org/2005/02/xpath-functions:day-from-dateTime' is not available and variants thereof. I know from other tests that I can use the substring() function perfectly, without needing any namespace prefix, and I believe it's in the same namespace as day-from-dateTime.
I feel like it's something incredibly easy, since all of the tutorials show functions being used, but something seems to be eluding me. Could someone show me what I'm missing?
Ouch, nasty versions thing going on here. A lot of the issues you're seeing will be because the XSLT processor you're using doesn't support XPath 2.0, which is where that day-from-dateTime function comes from.
I can get what you're trying to do to work, with a Saxon processor - Saxon-B 9.1.0.6 as my processor instead of Xalan. (Xalan appears to support XPath 1.0 only, according to the documentation)
There are a few errors in your documents:
The source document should have the timezone as 05:00, not 0500
<?xml version="1.0" encoding="UTF-8"?>
<watcher>
<event id="1" date="2009-09-04T13:49:10-05:00" type="ABCD">This is a test </event>
</watcher>
The XSLT should cast the string 2009-09-04T13:49:10-05:00 into a xs:dateTime, which is what type the argument of day-from-dateTime needs to be.
Date: <xsl:value-of select="day-from-dateTime(xs:dateTime(#date))"/>
And then it works
<?xml version="1.0" encoding="UTF-8"?>
Date: 4
Hope that helps,