2016-03-23 60 views
2

SOAP応答xmlからJavaオブジェクトに変換されるXSLTがあります。XSLT 2.0の文字列を分割、置換、連結します

soap応答には、コンマ(、)で区切られた文字列を持つurn:Statuesというノードがあります。

urn:Statuesノードの値を分割して値の一部を置き換え、それらをコンマ(、)で区切られた文字列に再度連結したいとします。

SOAPレスポンス:

マイXSLT:

<?xml version='1.0' encoding='UTF-8'?> 
<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:urn="urn:HPD_Incident_Query_WS"> 
    <xsl:output method='xml' indent='yes' /> 

    <xsl:template match="urn:Incident_Query_ServiceResponse" name="split"> 
     <xsl:for-each select="tokenize(./urn:Statuses,',')"> 
      <xsl:if test="(normalize-space(.) eq 'Cerrado')"> 
       <xsl:value-of select="replace(normalize-space(.), 'Cerrado', 'CLOSED')"/> 
      </xsl:if> 
      <xsl:if test="(normalize-space(.) eq 'CANCELLED')"> 
       <xsl:value-of select="replace(normalize-space(.), 'CANCELLED', 'CLOSED')"/> 
      </xsl:if> 
      <xsl:if test="(normalize-space(.) eq 'En Curso')"> 
       <xsl:value-of select="replace(normalize-space(.), 'En Curso', 'OPENACTIVE')"/> 
      </xsl:if> 
      <xsl:if test="(normalize-space(.) eq 'Pendiente')"> 
       <xsl:value-of select="replace(normalize-space(.), 'Pendiente', 'CLOSED.PENDING')"/> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:template> 

    <xsl:template match='/'> 
     <getTicketInfoResponse> 
      <responseCode> 
       <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ResponseCode' /> 
      </responseCode> 
      <responseMessage> 
       <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ResponseMsg' /> 
      </responseMessage> 
      <errorCode> 
       <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ErrorCode' /> 
      </errorCode> 
      <errorMsg> 
       <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ErrorMsg' /> 
      </errorMsg> 
      <ticketDetails> 
       <troubleTicketState> 
        <xsl:apply-templates select="/soapenv:Envelope/soapenv:Body"/> 
       </troubleTicketState> 
       <troubleTicketId> 
        <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:Incidents_Number' /> 
       </troubleTicketId> 
      </ticketDetails> 
     </getTicketInfoResponse> 
    </xsl:template> 
</xsl:stylesheet> 

ここでの問題は、値が値をカンマ(、)で区切られていないurn:Statusesに設定されている場合には、 。

現在の結果は、変換後:

<?xml version="1.0" encoding="UTF-8"?> 
<getTicketInfoResponse xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:urn="urn:HPD_Incident_Query_WS"> 
    <responseCode/> 
    <responseMessage/> 
    <errorCode>0</errorCode> 
    <errorMsg/> 
    <ticketDetails> 
     <troubleTicketState> CLOSEDCLOSEDOPENACTIVECLOSED</troubleTicketState> 
     <troubleTicketId>INC80167842,INC77752907,INC20954581,INC20954533</troubleTicketId> 
    </ticketDetails> 
</getTicketInfoResponse> 

期待される結果:

<?xml version="1.0" encoding="UTF-8"?> 
<getTicketInfoResponse xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:urn="urn:HPD_Incident_Query_WS"> 
    <responseCode/> 
    <responseMessage/> 
    <errorCode>0</errorCode> 
    <errorMsg/> 
    <ticketDetails> 
     <troubleTicketState>CLOSED,CLOSED,OPENACTIVE,CLOSED</troubleTicketState> 
     <troubleTicketId>INC80167842,INC77752907,INC20954581,INC20954533</troubleTicketId> 
    </ticketDetails> 
</getTicketInfoResponse> 

誰かが結果タグ<troubleTicketState>にカンマ(、)で文字列を分離する方法を教えてもらえます。

これを行うにはもっと良い方法はありますか?

ありがとうございます。

答えて

5

は、あなたは、単に行うことができませんでした。

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' 
xmlns:urn='urn:HPD_Incident_Query_WS'> 

<xsl:output method="xml" indent="yes" /> 

<xsl:template match="/soapenv:Envelope"> 
    <getTicketInfoResponse> 
     <responseCode> 
      <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ResponseCode" /> 
     </responseCode> 
     <responseMessage> 
      <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ResponseMsg" /> 
     </responseMessage> 
     <errorCode> 
      <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ErrorCode" /> 
     </errorCode> 
     <errorMsg> 
      <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ErrorMsg" /> 
     </errorMsg> 
     <ticketDetails> 
      <troubleTicketState> 
       <xsl:apply-templates select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:Statuses" /> 
      </troubleTicketState> 
      <troubleTicketId> 
       <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:Incidents_Number" /> 
      </troubleTicketId> 
     </ticketDetails> 
    </getTicketInfoResponse> 
</xsl:template> 

<xsl:template match="urn:Statuses"> 
    <xsl:value-of select="replace(replace(replace(replace(., 'Cerrado', 'CLOSED'), 'CANCELLED', 'CLOSED'), 'En Curso', 'OPENACTIVE'), 'Pendiente', 'CLOSED.PENDING')"/> 
</xsl:template> 

</xsl:stylesheet> 
+0

ありがとうございました!!!私はこのように考えなかった。それは完全に動作します。 – dev

+0

@ amit4497置き換えられた値が他の値の部分文字列として現れないことを前提としています。 –

+0

申し訳ありませんが、最後の部分を取得していません。 – dev

関連する問題