2016-07-28 1 views
2

XML 양식의 타임 스탬프에서 밀리 초를 제거하는 XSLT 스타일 시트를 작성하려고합니다. 아래의 샘플 XML은 엔벨로프에 타임 스탬프가 여러 개있을 수있는 샘플입니다. 그래서 나는 타임 스탬프와 패턴을 일치시켜야한다고 생각하고있다.타임 스탬프에서 밀리 초를 제거하는 XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    </soap:Header> 
     <soap:Body> 
     <Staging_Submit_Service xmlns="com.xxx"> 
      <u_From_Partner__c>Our Partner</u_From_Partner__c> 
      <u_To_Partner__c>Us</u_To_Partner__c> 
      <u_Partner_CI__c/> 
      <u_Partner_ID__c>10051</u_Partner_ID__c> 
      <u_Partner_Name__c>ROSEVILLE</u_Partner_Name__c> 
      <u_Partner_Reported_Date__c>2016-07-26T17:38:28.746134Z</u_Partner_Reported_Date__c> 
      <u_Partner_Status_Reason__c>Failure in System</u_Partner_Status_Reason__c> 
      <u_Partner_Submit_Date__c>2016-07-25T18:11:23.5443Z</u_Partner_Submit_Date__c> 
      <u_Partner_Priority__c>Low</u_Partner_Priority__c> 
      <u_Partner_Service_Type>Event</u_Partner_Service_Type> 
     </Staging_Submit_Service> 
    </soap:Body> 
</soap:Envelope> 

내 결과 XML이 같이 할 필요가 :

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
    </soap:Header><soap:Body> 
     <Staging_Submit_Service xmlns="com.xxx"> 
      <u_From_Partner__c>Our Partner</u_From_Partner__c> 
      <u_To_Partner__c>Us</u_To_Partner__c> 
      <u_Partner_CI__c/> 
      <u_Partner_ID__c>10051</u_Partner_ID__c> 
      <u_Partner_Name__c>ROSEVILLE</u_Partner_Name__c> 
      <u_Partner_Reported_Date__c>2016-07-26T17:38:28Z</u_Partner_Reported_Date__c> 
      <u_Partner_Status_Reason__c>Failure in System</u_Partner_Status_Reason__c> 
      <u_Partner_Submit_Date__c>2016-07-25T18:11:23Z</u_Partner_Submit_Date__c> 
      <u_Partner_Priority__c>Low</u_Partner_Priority__c> 
      <u_Partner_Service_Type>Event</u_Partner_Service_Type> 
     </Staging_Submit_Service> 
    </soap:Body> 
</soap:Envelope> 

공지 타임 스탬프 나는 원본 메시지의 모양 XSLT 2.0을 사용할 수 있습니다. 나는이 길을 시작했지만, 내가 있어야 할 곳을 찾지 못하는 것 같습니다.

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ns1="java:com.verizon.webservices.adapter.clecclient"> 
<xsl:output method="xml" indent="no" version="1.0" encoding="UTF-8" omit-xml-declaration="yes"/> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="@*|node()" 
    <xsl:copy> 
    <xsl:value-of select="." /> 
     <xsl:value-of select='matches(.,".*[0-9]\{4\}-[0-9]\{2\}.*:[0-9]\{2\}*")'/> 
    <xsl:value-of select='replace(., "s/.*[0-9]\{4\}-[0-9]\{2\}.*:[0-9]\{2\}\(\.[0-9]*\).*/\1","")'/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

저는 실제로 XSLT에 익숙하지 않고이 특정 문제에 대해서만 사용해야합니다. 사전

답변

0

감사드립니다

<xsl:template match="*[not(*) and . castable as xs:dateTime]"> 
    <xsl:copy> 
    <xsl:value-of select="format-dateTime(., '[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]')"/> 
    </xsl:copy> 
</xsl:template> 

시도 플러스 첫 번째 템플릿입니다.

그래서 전체 스타일 시트는 사실

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*[not(*) and . castable as xs:dateTime]"> 
     <xsl:copy> 
      <xsl:value-of select="format-dateTime(., '[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]')"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

입니다 위의 현재와 그것이 UTC 조금 까다 롭습니다 경우 Z로 출력 보장 어떤 시간대 접미사를 보존하지 보이지만, 당신이 할 경우 생각 value-of

 <xsl:value-of select="if (timezone-from-dateTime(.) eq xs:dayTimeDuration('PT0H')) then format-dateTime(., '[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]Z') else format-dateTime(., '[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01][Z]')"/> 

그렇다면 제대로 작동합니다.

그것은 시간대 Z에 대한 문제가

 <xsl:value-of select="format-dateTime(., '[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01][Z00:00t]')"/> 

수 있도록 https://www.w3.org/TR/xpath-functions-31/#rules-for-datetime-formatting에서 해결했다하지만 난 그 XmlPrime 또는 색슨 이외의 프로세서 작동 여부를 테스트하지 않은 것 같다.

까지 당신의 입력 샘플에 날짜 시간에 두 요소를 식별하고 초 소수 제거하도록 포맷으로 산소 내부의 Xalan와 나를 위해 다음 작품 :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:date="http://exslt.org/dates-and-times" 
    exclude-result-prefixes="xs date" version="1.0"> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*[not(*) and string-length() > 19 and date:date() != '']"> 
     <xsl:copy> 
      <xsl:value-of select="date:format-date(date:date(.), &quot;yyyy-MM-dd'T'HH:mm:ssZ&quot;)"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

그러나 생성 된 출력 날짜가 다를를 입력 <u_Partner_Reported_Date__c>2016-07-26T17:38:28.746134Z</u_Partner_Reported_Date__c><u_Partner_Reported_Date__c>2016-07-25T22:00:00+0000</u_Partner_Reported_Date__c>으로 나타나므로 날짜 구문 분석 또는 서식이 잘못되었습니다.

+0

이것이 효과적입니다. 내 테스터에서는 훌륭하지만 어플라이언스에서 사용하려고하면 오류가 발생합니다. 내 추측은 다른 XML 파서입니다 : –

+0

내 추측은 다른 XML 파서입니다. 오류가 발생했습니다 : 오류 : '구문 오류'* [안 (*) 및. castable as xs : dateTime] '.' 치명적인 오류 : 'com.sun.org.apache.xalan.internal.xsltc.compiler.NotCall은 com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern' 으로 전송할 수 없습니다. com.sun. org.apache.xalan.internal.xsltc.compiler.NotCall은 com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern으로 캐스팅 할 수 없습니다. 다른 매치 패턴을 시도 할 가능성은? –

+0

Java 환경에서 XSLT 2.0을 사용하려면 Saxon 9에서 Saxon 9를 사용해야하며 Xalan은 XSLT 1.0 프로세서이고'xs : dateTime '데이터 형식이나 XPath 2.0'castable as'표현식도 지원하지도 않습니다. 'format-dateTime'이나 당신이 사용하려고했던'matches' 나'replace' 함수가 아닙니다. –

1

의견에서 XSLT 1.0 솔루션 만 사용할 수있는 것으로 보입니다.여기

한 경우 : 원하는 정확한 결과를 제작

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    </soap:Header> 
     <soap:Body> 
     <Staging_Submit_Service xmlns="com.xxx"> 
      <u_From_Partner__c>Our Partner</u_From_Partner__c> 
      <u_To_Partner__c>Us</u_To_Partner__c> 
      <u_Partner_CI__c/> 
      <u_Partner_ID__c>10051</u_Partner_ID__c> 
      <u_Partner_Name__c>ROSEVILLE</u_Partner_Name__c> 
      <u_Partner_Reported_Date__c>2016-07-26T17:38:28.746134Z</u_Partner_Reported_Date__c> 
      <u_Partner_Status_Reason__c>Failure in System</u_Partner_Status_Reason__c> 
      <u_Partner_Submit_Date__c>2016-07-25T18:11:23.5443Z</u_Partner_Submit_Date__c> 
      <u_Partner_Priority__c>Low</u_Partner_Priority__c> 
      <u_Partner_Service_Type>Event</u_Partner_Service_Type> 
     </Staging_Submit_Service> 
    </soap:Body> 
</soap:Envelope> 

:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Header> 
    </soap:Header> 
     <soap:Body> 
     <Staging_Submit_Service xmlns="com.xxx"> 
      <u_From_Partner__c>Our Partner</u_From_Partner__c> 
      <u_To_Partner__c>Us</u_To_Partner__c> 
      <u_Partner_CI__c/> 
      <u_Partner_ID__c>10051</u_Partner_ID__c> 
      <u_Partner_Name__c>ROSEVILLE</u_Partner_Name__c> 
      <u_Partner_Reported_Date__c>2016-07-26T17:38:28Z</u_Partner_Reported_Date__c> 
      <u_Partner_Status_Reason__c>Failure in System</u_Partner_Status_Reason__c> 
      <u_Partner_Submit_Date__c>2016-07-25T18:11:23Z</u_Partner_Submit_Date__c> 
      <u_Partner_Priority__c>Low</u_Partner_Priority__c> 
      <u_Partner_Service_Type>Event</u_Partner_Service_Type> 
     </Staging_Submit_Service> 
    </soap:Body> 
</soap:Envelope> 
이 변환이 제공된 XML 문서에 적용

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:df="com.xxx"> 
<xsl:output omit-xml-declaration="yes"/> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="df:u_Partner_Reported_Date__c/text() 
         |df:u_Partner_Submit_Date__c/text()"> 
    <xsl:value-of select="concat(substring-before(.,'.'), 'Z')"/> 
    </xsl:template> 
</xsl:stylesheet>