通过XSL增加特定的导出HTML标记

时间:2015-07-22 15:45:30

标签: xml xslt xslt-1.0

我正在尝试将XML导出为HTML,以便可以以精美的表格格式显示信息。这是我的XSL部分

<xsl:template match="Step">
    <tr>
        <xsl:choose>
            <xsl:when test="starts-with(Obj, '▬▬ UC_')">
                <td>NUMBERING HERE</td>
                <td class="Level0">
                    <xsl:value-of select="Obj" disable-output-escaping="no" />
                </td>
            </xsl:when>
            <xsl:otherwise>
                <td></td>
                <td class="Level{count(ancestor::*)-3}">
                    <xsl:value-of select="Obj" disable-output-escaping="no" />
                </td>
            </xsl:otherwise>
        </xsl:choose>
        <td>
            <pre>
                <xsl:value-of select="Details" disable-output-escaping="no" />
            </pre>
        </td>
    <xsl:apply-templates select="Step"/>
</xsl:template>

基本上,我想要的是如果Obj的TD是Level0,那么我想在之前的TD中插入编号(1,2,3,4等)。我知道XSL变量是只读的,但有没有办法可以为每个<TD class="Level0">添加增量数?

示例XML

<Action rID="T4">
    <Step rID="T5">
        <Obj><![CDATA[▬▬ UC_P1_BOND_010 ▬▬]]></Obj>
        <Details><![CDATA[Product = Multiple; Event = NEW]]></Details>
        <Time><![CDATA[01/04/2015 - 10:43:54]]></Time>
        <TimeTick>1427881434</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="8" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[▬▬ UC_P1_BOND_010 ▬▬]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T6">
        <Obj><![CDATA[●●● TD_SETUPBOND]]></Obj>
        <Details><![CDATA[Executing script steps of TD_SETUPBOND]]></Details>
        <Time><![CDATA[01/04/2015 - 10:43:54]]></Time>
        <TimeTick>1427881434</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="9" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[●●● TD_SETUPBOND]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T7">
        <Obj><![CDATA[Statement Skipped: #NEWAMEND]]></Obj>
        <Details><![CDATA[0]]></Details>
        <Time><![CDATA[01/04/2015 - 10:43:55]]></Time>
        <TimeTick>1427881435</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="10" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[Statement Skipped: #NEWAMEND]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T8">
        <Obj><![CDATA[Statement Skipped: #OUTBOUND]]></Obj>
        <Details><![CDATA[2]]></Details>
        <Time><![CDATA[01/04/2015 - 10:43:55]]></Time>
        <TimeTick>1427881435</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="11" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[Statement Skipped: #OUTBOUND]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T9">
        <Obj><![CDATA[●●● TD_VERIFYBONDNEWAMEND]]></Obj>
        <Details><![CDATA[Executing script steps of TD_VERIFYBONDNEWAMEND]]></Details>
        <Time><![CDATA[01/04/2015 - 10:44:01]]></Time>
        <TimeTick>1427881441</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="12" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[●●● TD_VERIFYBONDNEWAMEND]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T10">
        <Obj plainTxt="False"><![CDATA[Home Window]]></Obj>
        <Details plainTxt="False"><![CDATA[JavaWindow]]></Details>
        <Time><![CDATA[01/04/2015 - 10:44:01]]></Time>
        <TimeTick>1427881441</TimeTick>
        <Step rID="T11">
            <Obj plainTxt="False"><![CDATA[Quick Search.Click]]></Obj>
            <Details plainTxt="False"><![CDATA["LEFT"]]></Details>
            <Time><![CDATA[01/04/2015 - 10:44:01]]></Time>
            <TimeTick>1427881441</TimeTick>
            <NodeArgs eType="Replay" icon="7" nRep="15" Source="Action1" SourceLine="-1">
                <Disp><![CDATA[Quick Search.Click]]></Disp>
            </NodeArgs>
        </Step>
        <NodeArgs eType="Context" icon="6" nRep="14" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[Home Window]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T160">
        <Obj><![CDATA[▬▬ UC_P1_BOND_011 ▬▬]]></Obj>
        <Details><![CDATA[Product Multiple; Event = AMEND]]></Details>
        <Time><![CDATA[01/04/2015 - 10:45:14]]></Time>
        <TimeTick>1427881514</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="972" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[▬▬ UC_P1_BOND_011 ▬▬]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T161">
        <Obj><![CDATA[●●● TD_SETUPBOND]]></Obj>
        <Details><![CDATA[Executing script steps of TD_SETUPBOND]]></Details>
        <Time><![CDATA[01/04/2015 - 10:45:14]]></Time>
        <TimeTick>1427881514</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="973" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[●●● TD_SETUPBOND]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T162">
        <Obj><![CDATA[Statement Skipped: #NEWAMEND]]></Obj>
        <Details><![CDATA[0]]></Details>
        <Time><![CDATA[01/04/2015 - 10:45:14]]></Time>
        <TimeTick>1427881514</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="974" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[Statement Skipped: #NEWAMEND]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T163">
        <Obj><![CDATA[Statement Skipped: #OUTBOUND]]></Obj>
        <Details><![CDATA[2]]></Details>
        <Time><![CDATA[01/04/2015 - 10:45:14]]></Time>
        <TimeTick>1427881514</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="975" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[Statement Skipped: #OUTBOUND]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T164">
        <Obj><![CDATA[●●● TD_VERIFYBONDNEWAMEND]]></Obj>
        <Details><![CDATA[Executing script steps of TD_VERIFYBONDNEWAMEND]]></Details>
        <Time><![CDATA[01/04/2015 - 10:45:20]]></Time>
        <TimeTick>1427881520</TimeTick>
        <NodeArgs eType="User" icon="5" nRep="976" Source="Action1" SourceLine="-1">
            <Disp><![CDATA[●●● TD_VERIFYBONDNEWAMEND]]></Disp>
        </NodeArgs>
    </Step>
    <Step rID="T165">
        <Obj plainTxt="False"><![CDATA[Home Window]]></Obj>
        <Details plainTxt="False"><![CDATA[JavaWindow]]></Details>
        <Time><![CDATA[01/04/2015 - 10:45:21]]></Time>
        <TimeTick>1427881521</TimeTick>
        <Step rID="T166">
            <Obj plainTxt="False"><![CDATA[Quick Search.Click]]></Obj>
            <Details plainTxt="False"><![CDATA["LEFT"]]></Details>
            <Time><![CDATA[01/04/2015 - 10:45:21]]></Time>
            <TimeTick>1427881521</TimeTick>
            <NodeArgs eType="Replay" icon="7" nRep="979" Source="Action1" SourceLine="-1">
                <Disp><![CDATA[Quick Search.Click]]></Disp>
            </NodeArgs>
        </Step>
    </Step>
    .
    .
    .
    and so on
</Action>

示例HTML输出

enter image description here

1 个答案:

答案 0 :(得分:1)

我们有一个minimized example

<强> XML

<Action rID="T4">
    <Step rID="T5">
        <Obj><![CDATA[▬▬ UC_A]]></Obj>
    </Step>
    <Step rID="T6">
        <Obj><![CDATA[abc]]></Obj>
    </Step>
    <Step rID="T7">
        <Obj><![CDATA[bcd]]></Obj>
    </Step>
    <Step rID="T8">
        <Obj><![CDATA[cde]]></Obj>
    </Step>
    <Step rID="T9">
        <Obj><![CDATA[▬▬ UC_B]]></Obj>
    </Step>
    <Step rID="T10">
        <Obj><![CDATA[def]]></Obj>
    </Step>
    <Step rID="T11">
        <Obj><![CDATA[efg]]></Obj>
    </Step>
</Action>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/Action">
    <table border="1">
        <xsl:apply-templates/>  
    </table>
</xsl:template>

<xsl:template match="Step">
    <tr>
        <td>
            <xsl:if test="starts-with(Obj, '▬▬ UC_')">
                <xsl:number count="Step[starts-with(Obj, '▬▬ UC_')]"/>
            </xsl:if>
        </td>
        <td>
            <xsl:value-of select="Obj" />
        </td>
    </tr>
</xsl:template>

</xsl:stylesheet>

输出(已呈现)

enter image description here