将字母添加到xslt值

时间:2014-03-05 16:27:53

标签: xml xslt

我目前正在使用xlst来操作xml中的数据。

在这个XML中,我有一个日期字段,如下所示:

<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>

以下是我创建的xlst的副本:

<?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>

2 个答案:

答案 0 :(得分:1)

使用translate()功能将所有空格字符替换为T。请注意,这是一个非常微不足道的问题,你可以通过谷歌搜索解决自己。

<强>样式表

<?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>

答案 1 :(得分:1)

只需替换:

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

使用:

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