Reputation: 1355
I have a requirement where I have to concatenate each of the <troubleticketalarm>
details into a one string per alarm, then map it to Description field in my xslt.
Description is of type array in my xslt.
Here is my xml,
<ticketDetails>
<alarms>
<troubleticketalarm>
<alarmId>3117</alarmId>
<alarmName>OSS</alarmName>
<amo>amo</amo>
<alarmedEquipment>alarmedEquipment</alarmedEquipment>
<severity>1</severity>
<specificProblem>specificProblem</specificProblem>
<probableCause>probableCause</probableCause>
<activationDate>2015-03-17T17:33:04</activationDate>
<associationDate>2015-03-17T17:33:04</associationDate>
<clearDate>2015-03-17T17:33:04</clearDate>
</troubleticketalarm>
<troubleticketalarm>
<alarmId>3118</alarmId>
<alarmName>OSS123</alarmName>
<activationDate>2015-03-17T17:33:0405:30</activationDate>
<asociationDate>2015-03-17T17:33:0405:30</asociationDate>
<clearDate></clearDate>
</troubleticketalarm>
<troubleticketalarm>
<alarmId>3119</alarmId>
<alarmName>OSS124</alarmName>
<amo>amo</amo>
<alarmedEquipment>alarmedEquipment</alarmedEquipment>
<severity>1</severity>
<activationDate>2015-03-17T17:33:0405:30</activationDate>
<asociationDate>2015-03-17T17:33:0405:30</asociationDate>
<clearDate></clearDate>
</troubleticketalarm>
</alarms>
</ticketDetails>
Here is the part of xslt :
<ns:Description type="Array">
<ns:Description type="String" mandatory="" readonly=""></ns:Description>
</ns:Description>
What I want is to concatenate values for separated by '|' and map it to the ns:Description for each concatenated value of
Concatenated string looks something like this
3117|OSS|amo|alarmedEquipment|1|specificProblem|probableCause|2015-03-17T17:33:04+05:30|2015-03-17T17:33:04+05:30|2015-03-17T17:33:04+05:30
3118|OSS123|||||||2015-03-17T17:33:04+05:30|
3119|OSS124|amo|alarmedEquipment|1|||2015-03-17T17:33:04+05:30|2015-03-17T17:33:04+05:30|
These 3 strings should be added to Description[0],Description[1],Description[1].
There could be multiple <troubleticketalarm>
tags in xml so the xslt Description should be dynamic.
How do I achieve this? Please let me know if question is not clear.
Edit
My XSLT file looks something like
<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match='/'>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://schemas.hp.com/SM/7" xmlns:com="http://schemas.hp.com/SM/7/Common" xmlns:xm="http://www.w3.org/2005/05/xmlmime">
<soapenv:Header/>
<soapenv:Body>
<ns:CloseIncidentRequest attachmentInfo="" attachmentData="" ignoreEmptyElements="true" updateconstraint="-1">
<ns:model query="">
<ns:keys query="" updatecounter="1">
<ns:IncidentID type="String" mandatory="" readonly=""></ns:IncidentID>
</ns:keys>
<ns:instance query="" uniquequery="" recordid="" updatecounter="1">
<ns:IncidentID type="String" mandatory="" readonly=""></ns:IncidentID>
<ns:Description type="Array">
<ns:Description type="String" mandatory="" readonly=""></ns:Description>
</ns:Description>
......
......
</ns:instance>
</ns:model>
</ns:CloseIncidentRequest>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2
Views: 7152
Reputation: 166
What I understand is you are trying to concatenate the values of the node under <troubleticketalarm>
and map it with one of the field (<ns:Description>
) in your xslt file.
May be you can try using for-each
loop and inside the loop you can concatenate the values:
<ns:Description type="Array">
<xsl:for-each select="/ticketDetails/alarms/troubleticketalarm">
<ns:Description type="String" mandatory="" readonly="">
<xsl:value-of select="concat(alarmId,'|',alarmName,'|',amo,'|',alarmedEquipment,'|',severity,'|',specificProblem,
'|',probableCause,'|',associationDate,'|',activationDate,'|',clearDate)" />
</ns:Description>
</xsl:for-each>
</ns:Description>
Upvotes: 2
Reputation: 167401
If you don't know the number and names of child elements but you want to use the troubleticketalarm
with most child elements as the definition of the list then
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" />
<xsl:strip-space elements="*"/>
<xsl:variable name="alarms" select="//troubleticketalarm"/>
<xsl:variable name="max" select="max($alarms/count(*))"/>
<xsl:variable name="col-names" select="$alarms[count(*) eq $max]/*/local-name()"/>
<xsl:key name="k1" match="troubleticketalarm/*" use="local-name()"/>
<xsl:template match="troubleticketalarm">
<xsl:value-of select="for $name in $col-names return (if (key('k1', $name, current())) then key('k1', $name, current()) else '')" separator="|"/>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:transform>
might help. It is online at http://xsltransform.net/gWmuiJY and outputs
3117|OSS|amo|alarmedEquipment|1|specificProblem|probableCause|2015-03-17T17:33:04|2015-03-17T17:33:04|2015-03-17T17:33:04
3118|OSS123||||||2015-03-17T17:33:0405:30||
3119|OSS124|amo|alarmedEquipment|1|||2015-03-17T17:33:0405:30||
Upvotes: 4