在 XSLT 中缩写 XML 文件的序列号

Abbreviating Serial Number from XML file in XSLT

这可能是相当微不足道的,但我是 XSLT 的新手,并且在尝试找到一种方法来缩短必须在 table 中显示的序列号的长度时被难住了。我有一个 XML 文件,其中包含有关大量电表读数的带时间戳的数据。我的目标是从 XML 文件中获取仪表数据并将其转换为 table,以便通过样式表在 Internet Explorer 中轻松查看。我的解析和 table 设置正确,所以我现在正在做一些小事情来清理 table。

每个仪表都在 XML 文件中以电子序列号 (ESN) 命名。 ESN 是此格式的 22 位唯一标识符 3.54.765.2.233245.4.64352456 ESN 中唯一需要显示在 table 中的部分是最后 8数字(在本示例中为 64352456)。

我已经用 format-number() 命令尝试了各种方法,但这个序列号似乎有问题,因为它不是实数。在我的样式表中,我正在检索 ESN 并将其存储为变量 "ESN",稍后将其显示在 table 中作为完整的 ESN,关于如何将 MeterID 缩短到最后 8 位数字的任何想法都非常有用赞赏!这是我的样式表,由于公司隐私问题,我认为我不能 post XML。谢谢!

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:local="#local-functions"
    xmlns:dt="urn:schemas-microsoft-com:datatypes"

    xmlns="http://www.w3.org/TR/REC-html40" 
                      xmlns:itron="http://schemas.datacontract.org/2004/07/Itron.Ami.AmiServiceTest.Hosting"
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:d2p1="http://www.itron.com/ami/2008/10/data"
    xmlns:d3p1="http://www.itron.com/ami/2008/10/common"
    xmlns:d5p1="http://www.itron.com/ami/2008/10/events"
    xmlns:d7p1="http://www.w3.org/2001/XMLSchema">

<xsl:template match="/">
<!--        Print Title     -->
    <div>AMI DATA</div>
<!--        Create Table and column headings        -->
    <table border="1" cellpadding="0" cellspacing="0">
            <tr style="background-color:#C0C0C0; font-weight:bold">

                <td>Meter ID</td>
                <td>Vh(a)</td>
                <td>Max V(a)</td>
                <td>Min V(a)</td>
                <td>Vh(c)</td>
                <td>Max V(c)</td>
                <td>Min V(c)</td>
                <td>Timestamp</td>
            </tr>
<!--        Loop through each DataSubscriberItem (each a seperate meter)   create  variables to store TimeDataEnd, PulseMultiplier, and Meter Identifier           -->
    <xsl:for-each select="XMLRoot/itron:DataSubscriberItem">
        <div>
            <xsl:variable name="EndTime" select="itron:DataArrivedInput/d2p1:ReadDataCollection/d2p1:ReadData/d2p1:LoadProfileChannels/d2p1:LoadProfileChannel/d2p1:TimeDataEnd"/>
        </div>
        <div>
            <xsl:variable name="PulsMult"   select="itron:DataArrivedInput/d2p1:ReadDataCollection/d2p1:ReadData/d2p1:LoadPr ofileChannels/d2p1:LoadProfileChannel/d2p1:PulseMultiplier"/>
        </div>
        <div>
            <xsl:variable name="ESN" select="itron:DataArrivedInput/d2p1:ReadDataCollection/d2p1:ReadData/d2p1:Identifier"/>
        </div>
<!--        Variable to keep count of entries within each DataSubscriberItem.
    It's later used to assign timestamps based on TimeDataEnd       -->         
            <xsl:variable name="NumPnts" select="count(itron:DataArrivedInput/d2p1:ReadDataCollection/d2p1:ReadData/d2p1:LoadProfileChannels/d2p1:LoadProfileChannel[1]/d2p1:IntervalValues/d2p1:IntervalValue)"/>
            <xsl:for-each   select="itron:DataArrivedInput/d2p1:ReadDataCollection/d2p1:ReadData/d2p1:LoadProfileChannels/d2p1:LoadProfileChannel[1]/d2p1:IntervalValues/d2p1:IntervalValue">
                <xsl:variable name="INum" select="position()"/>
                <xsl:variable name="EndTime2" select="../../d2p1:TimeDataEnd"/>
<!--        Start filling in table with true Voltage and Vh values      -->                 
                <tr>
                    <td>
                        <xsl:value-of select="$ESN"/>
                    </td>
                    <xsl:variable name="Vha" select="d2p1:ChannelValue"/>
                    <td>                    
                        <xsl:value-of select='format-number($Vha*$PulsMult,"0.###")'/>
                    </td>
                    <xsl:variable name="Vmaxa" select="../../../d2p1:LoadProfileChannel[2]/d2p1:IntervalValues/d2p1:IntervalValue[$INum]/d2p1:ChannelValue"/>
                    <td>
                        <xsl:value-of select='format-number($Vmaxa*$PulsMult,"0.###")'/>
                    </td>
                    <xsl:variable name="Vmina" select="../../../d2p1:LoadProfileChannel[3]/d2p1:IntervalValues/d2p1:IntervalValue[$INum]/d2p1:ChannelValue"/>
                    <td>
                        <xsl:value-of select='format-number($Vmina*$PulsMult,"0.###")'/>
                    </td>
                    <xsl:variable name="Vhc" select="../../../d2p1:LoadProfileChannel[4]/d2p1:IntervalValues/d2p1:IntervalValue[$INum]/d2p1:ChannelValue"/>
                    <td>
                        <xsl:value-of select='format-number($Vhc*$PulsMult,"0.###")'/>
                    </td>
                    <xsl:variable name="Vmaxc" select="../../../d2p1:LoadProfileChannel[5]/d2p1:IntervalValues/d2p1:IntervalValue[$INum]/d2p1:ChannelValue"/>
                    <td>
                        <xsl:value-of select='format-number($Vmaxc*$PulsMult,"0.###")'/>
                    </td>
                    <xsl:variable name="Vminc" select="../../../d2p1:LoadProfileChannel[6]/d2p1:IntervalValues/d2p1:IntervalValue[$INum]/d2p1:ChannelValue"/>
                    <td>
                        <xsl:value-of select='format-number($Vminc*$PulsMult,"0.###")'/>
                    </td>
                    <td>
                        <xsl:variable name="SDTS" select="concat(substring($EndTime2, 1,10), ' ', substring($EndTime2, 12,5))"/>
                        <xsl:value-of select="local:timeCalc($SDTS, $INum, $NumPnts)"/>
                    </td>
                </tr>

            </xsl:for-each>
    </xsl:for-each>
    </table>


</xsl:template>
<!--        VBScript to create seperate timestamps from INum variable and  TimeDataEnd      -->
<msxsl:script language="VBScript" implements-prefix="local">
        <![CDATA[
    Function timeCalc(ET, PT, TP) 
        SDT1 = CDate(ET)
        SDT = DateAdd("n", -(TP-PT)*5, SDT1)
        TTR =  PadOut(Month(SDT)) & "/" & PadOut(Day(SDT)) & "/" & Year(SDT)& " " & PadOut(Hour(SDT)-4) & ":" & PadOut(Minute(SDT))
        timeCalc =   TTR
    End Function

    Function PadOut(n)
        If n < 10 Then
            PadOut = "0" & n
        Else
            PadOut = n
        End If

    End Function
]]>
    </msxsl:script>

I've tried various things with the format-number() command, but it seems to have problems with this serial number because it isn't a real number.

不,这不是一个数字 - 它是一个字符串。要显示字符串的最后 8 个字符,可以使用:

<xsl:value-of select="substring($ESN, string-length($ESN) - 7)"/>