使用XSLT将XML响应日期格式从YYYY-MM-DDT00:00:00转换为DD / MM / YYYY

时间:2019-04-03 13:57:05

标签: xml xslt xslt-1.0

我的 XML 响应版本“ 1.0”在元素数组中表示不同的收集日期,格式为2019-04-04T00:00:00,我想使用 XSLT < / strong>版本“ 1.0”,以将每个重复元素中的日期格式转换为04/04/2019(DD / MM / YYYY)。我尝试寻找一些解决方案或将其与我正在执行的其他转换合并,但是这会使它们出错,因此我只希望样式表按如下所示进行转换,然后我将分别转换其余的内容

使用不正确的日期格式来响应XML

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <GetFullSiteCollectionsResponse xmlns="http://webservices.whitespacews.com/">
         <GetFullSiteCollectionsResult>
            <ErrorCode>0</ErrorCode>
            <ErrorDescription>Success</ErrorDescription>
            <SuccessFlag>true</SuccessFlag>
            <SiteServices>
               <ApiSiteServiceWithExtraDetails>
                  <SiteService>                     
                     <SiteServiceItemQuantity>1.0000</SiteServiceItemQuantity>
                     <SiteServiceItemID>17222</SiteServiceItemID>
                     <NextCollectionDate>2019-04-04T00:00:00</NextCollectionDate>
                  </SiteService>
                  <RRASSContractRounds>
                     <RRASSContractRound>                                                   
                        <RoundCodeOnly>GDN2</RoundCodeOnly>
                        <DefaultDeviceType>STREET</DefaultDeviceType>
                     </RRASSContractRound>
                  </RRASSContractRounds>
               </ApiSiteServiceWithExtraDetails>
               <ApiSiteServiceWithExtraDetails>
                  <SiteService>                     
                     <SiteServiceItemQuantity>1.0000</SiteServiceItemQuantity>
                     <SiteServiceItemID>17220</SiteServiceItemID>
                     <NextCollectionDate>2019-04-11T00:00:00</NextCollectionDate>
                  </SiteService>
                  <RRASSContractRounds>
                     <RRASSContractRound>                                                
                        <RoundCodeOnly>REC3</RoundCodeOnly>
                        <DefaultDeviceType>STREET</DefaultDeviceType>
                     </RRASSContractRound>
                  </RRASSContractRounds>
               </ApiSiteServiceWithExtraDetails>                              
            </SiteServices>
         </GetFullSiteCollectionsResult>
      </GetFullSiteCollectionsResponse>
   </soap:Body>
</soap:Envelope>

期望的结果

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <GetFullSiteCollectionsResponse xmlns="http://webservices.whitespacews.com/">
         <GetFullSiteCollectionsResult>
            <ErrorCode>0</ErrorCode>
            <ErrorDescription>Success</ErrorDescription>
            <SuccessFlag>true</SuccessFlag>
            <SiteServices>
               <ApiSiteServiceWithExtraDetails>
                  <SiteService>                     
                     <SiteServiceItemQuantity>1.0000</SiteServiceItemQuantity>
                     <SiteServiceItemID>17222</SiteServiceItemID>
                     <NextCollectionDate>04/04/2019</NextCollectionDate>
                  </SiteService>
                  <RRASSContractRounds>
                     <RRASSContractRound>                                
                        <RoundCodeOnly>GDN2</RoundCodeOnly>
                        <DefaultDeviceType>STREET</DefaultDeviceType>
                     </RRASSContractRound>
                  </RRASSContractRounds>
               </ApiSiteServiceWithExtraDetails>
               <ApiSiteServiceWithExtraDetails>
                  <SiteService>                     
                     <SiteServiceItemQuantity>1.0000</SiteServiceItemQuantity>
                     <SiteServiceItemID>17220</SiteServiceItemID>
                     <NextCollectionDate>11/04/2019</NextCollectionDate>
                  </SiteService>
                  <RRASSContractRounds>
                     <RRASSContractRound>                                                                          
                        <RoundCodeOnly>REC3</RoundCodeOnly>
                        <DefaultDeviceType>STREET</DefaultDeviceType>
                     </RRASSContractRound>
                  </RRASSContractRounds>
               </ApiSiteServiceWithExtraDetails>                              
            </SiteServices>
         </GetFullSiteCollectionsResult>
      </GetFullSiteCollectionsResponse>
   </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:0)

XSLT 1.0没有日期概念。您需要将其视为文本操作中的一个(简单)问题:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ws="http://webservices.whitespacews.com/"
exclude-result-prefixes="ws">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="ws:NextCollectionDate">
    <xsl:copy>
        <xsl:value-of select="substring(., 9, 2)"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring(., 6, 2)"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring(., 1, 4)"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>