2

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>
4
  • I don't see any XSLT code in here. Can you please post it? Commented Jun 30, 2015 at 7:57
  • In my xslt file I have this tag, which I have to map <ns:Description type="Array"> <ns:Description type="String" mandatory="" readonly=""></ns:Description> </ns:Description> Commented Jun 30, 2015 at 8:24
  • 2
    But <ns:Description type="Array"> is not XSLT, it might be some custom format to describe data types but it certainly is not something defined in the XSLT 1.0 or 2.0 or 3.0 language. You seem to want to map the elements to a bar | delimited list but it is not clear which item defines the columns in that list. A simple approach would be <xsl:template match="troubleticketalarm"><xsl:value-of select="*" separator="|"/></xsl:template> to create the list but the lists will then have different lengths depending on the number of child elements. Commented Jun 30, 2015 at 8:30
  • Using the above template will concatenate everything, something like 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|2015-03-17T17:33:04+05:30| What I want is the concatenated value of each <troubleticketalarm> tag should be treated as new entry to the field Description in my xslt, since Description is an array. Commented Jun 30, 2015 at 8:39

2 Answers 2

4

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>&#10;</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||
Sign up to request clarification or add additional context in comments.

2 Comments

I have update my xslt file. How do I make sure each concatenated value is getting set to per <ns:Description> array cell.
Consider to post the XML result you want to create with XSLT for your input sample. It is currently not clear to me whether you want to put all values into a single element or whether you want to create one result element for each troubleticketalarm element in the input.
2

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.