我有一个要求,我必须将每个<troubleticketalarm>
详细信息连接到每个警报一个字符串,然后将其映射到我的xslt中的Description字段。
描述是我的xslt中的数组类型。
这是我的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>
以下是xslt的一部分:
<ns:Description type="Array">
<ns:Description type="String" mandatory="" readonly=""></ns:Description>
</ns:Description>
我想要的是连接由&#39; |&#39;分隔的值。并将其映射到ns:
的每个连接值的描述连接字符串看起来像这样
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|
这3个字符串应添加到Description [0],Description [1],Description [1]中。
在xml中可能有多个<troubleticketalarm>
标记,因此xslt描述应该是动态的。
我如何实现这一目标?如果问题不明确,请告诉我。
修改
我的XSLT文件类似于
<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>
答案 0 :(得分:3)
如果您不知道子元素的数量和名称,但是您希望将troubleticketalarm
与大多数子元素一起用作列表的定义,那么
<?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>
可能有所帮助。它在http://xsltransform.net/gWmuiJY在线并输出
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||
答案 1 :(得分:2)
我的理解是您尝试连接<troubleticketalarm>
下的节点值并将其映射到xslt文件中的一个字段(<ns:Description>
)。
可能您可以尝试使用for-each
循环并在循环内部连接值:
<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>