2014-03-05 43 views
-1

我目前使用xlst來操縱xml中的數據。將字母添加到xlst值

在這個XML我有的diplayed這樣一個日期字段:

<SalesOrderDate>2014-03-05 07:21:46</SalesOrderDate> 

目前這是在XSLT使用此代碼操作:

<SalesOrderDate><xsl:value-of select="SalesOrderDate"/></SalesOrderDate> 

我需要的XSLT來操縱它所以它出現在T與日期之間例如:

<SalesOrderDate>2014-03-05T07:21:46</SalesOrderDate> 

下面是ac我創建了XLST的OPY:

<?xml version="1.0" encoding="utf-8"?> 

<!-- Key allows us to get unique orders based on the Id from external system --> 
<xsl:key name="UniqueSalesOrder" match="SalesOrders/SalesOrders" use="Id" /> 

<xsl:template match="/"> 
    <!-- Main company node of our object model --> 
    <Company> 

     <!-- Sales order collection of our object model --> 
     <SalesOrders> 

      <!-- Use the key to find unique orders from the list --> 
      <xsl:for-each select="SalesOrders/SalesOrders[generate-id() = generate-id(key('UniqueSalesOrder', Id)[1])]"> 

       <!-- Call template that will write out the order details --> 
       <xsl:call-template name="ProcessOrder" /> 
      </xsl:for-each> 
     </SalesOrders> 
    </Company> 
</xsl:template> 

<xsl:template name="ProcessOrder"> 
    <SalesOrder> 

     <!-- Write out header information --> 
     <SalesOrderNumber><xsl:value-of select="Id" /></SalesOrderNumber> 
     <AccountReference>IO01</AccountReference> 
     <SalesOrderDate><xsl:value-of select="SalesOrderDate"/></SalesOrderDate> 

     <!-- Write out address information --> 
     <SalesOrderAddress> 
      <Forename><xsl:value-of select="Forename" /></Forename> 
      <Surname><xsl:value-of select="Surname" /></Surname> 
      <Address1><xsl:value-of select="Address1"/></Address1> 
      <Address2><xsl:value-of select ="Address2"/></Address2> 
      <Town><xsl:value-of select="City"/></Town> 
      <County><xsl:value-of select="County"/></County> 
      <Postcode><xsl:value-of select="Postcode"/></Postcode> 
      <Telephone><xsl:value-of select="Telephone"/></Telephone> 
      <Notes1><xsl:value-of select="EmailAddress"/></Notes1> 
     </SalesOrderAddress> 

     <!-- write out carriage information--> 
     <Carriage> 
      <UnitPrice><xsl:value-of select="UnitPrice"/></UnitPrice> 
     </Carriage> 


     <!-- Select the id for the current unique order --> 
     <!-- Has to be in variable to use it XSLT filter --> 
     <xsl:variable name="Id" select="Id" /> 

     <!-- Write out all items for this order id --> 
     <SalesOrderItems> 

      <!-- Use the unique id to find all matching lines --> 
      <!-- Note the "$Id" in filter - this is referencing the variable above --> 
      <xsl:for-each select="../SalesOrders[Id=$Id]"> 

       <!-- Call template that will write out the item details --> 
       <xsl:call-template name="ProcessItem" /> 
      </xsl:for-each> 
     </SalesOrderItems> 
    </SalesOrder> 
</xsl:template> 

<xsl:template name="ProcessItem"> 
    <!-- Output item line information --> 
    <Item> 
     <Sku><xsl:value-of select="SKU" /></Sku> 
     <QtyOrdered><xsl:value-of select="Quantity" /></QtyOrdered> 
     <UnitPrice><xsl:value-of select="Price" /></UnitPrice> 
    </Item> 
</xsl:template> 

+0

您使用的是XSLT 1.0還是2.0? –

+0

當您無法使用它時,您需要自己嘗試併發布具體問題。請顯示你的嘗試。 –

+0

我已經添加了我創建的xlst。 T需要在xlst寫出標題信息的地方添加。 – user3384410

回答

1

只需更換:

<SalesOrderDate><xsl:value-of select="SalesOrderDate"/></SalesOrderDate> 

有:

<SalesOrderDate><xsl:value-of select="translate(SalesOrderDate,' ','T')"/></SalesOrderDate> 
+0

此工作非常出色,非常感謝您的幫助 – user3384410

1

使用translate()功能替換爲T任何空格字符。請注意,這是一個非常微不足道的問題,您可以通過Google搜索來解決問題。

樣式

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

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

    <xsl:template match="/SalesOrderDate"> 
    <xsl:copy> 
     <xsl:value-of select="translate(.,' ','T')"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

輸出

<?xml version="1.0" encoding="utf-8"?> 
<SalesOrderDate>2014-03-05T07:21:46</SalesOrderDate> 
+0

嗨Mathias有沒有一種方法可以在xlst寫出標題信息的部分添加此內容我已更新問題以包含我已創建的xlst – user3384410