基于XML文件数据的XSLT过滤

时间:2017-03-07 05:05:48

标签: xml xslt

我有以下XSLT:

<xsl:template match="Table">
    <tr>
        <td>
            <font face="Verdana" color="#ffffff" size="2" style="vertical-align: top; white-space: nowrap">
                <xsl:value-of select="PRG_DESCRIPTION"/>
            </font>
        </td>
        <td>
            <font face="Verdana" color="#ffffff" size="2" style="vertical-align: top; white-space: nowrap">
                <xsl:value-of select="DAYS_LEFT"/>
            </font>
        </td>
        <td>&#160;&#160;</td>
        <xsl:if test="ACTIVE = 0">
        <td style="top: 2px">
            <form name="myform"  onsubmit="popupform(this, 'join');"  action="../LogOnSection/SetCourseStart.aspx" method="post">                   
                <input type="hidden" name="CourseID">
                <xsl:attribute name="value">
                    <xsl:value-of select="EEDO_COURSE_ID"/>
                </xsl:attribute></input>
                <input type="hidden" name="UserID"><xsl:attribute name="value">
                    <xsl:value-of select="CLI_RID"/>
                </xsl:attribute></input>
                <input type="hidden" name="EventCode"><xsl:attribute name="value">
                    <xsl:value-of select="EVE_EVENT_CODE"/>
                </xsl:attribute></input>
                <input type="hidden" name="DateCreated">
                <xsl:attribute name="value">
                    <xsl:value-of select="DATE_CREATED"/>
                </xsl:attribute>
                </input>
                <input name="Submit" type="submit" value="Go"/>
                <br>
                </br>
            </form>
        </td>
        </xsl:if>
    </tr>
</xsl:template>

我需要做的是读取一个XML文件,并且只显示EVE_EVENT_CODE中不存在XML文件中的代码ID的记录。

XML文件将是这样的:

<?xml version="1.0"?>
    <program_codes>
        <code id="FUN">CR</code>
        <code id="HAN">CR</code>
        <code id="AO">CR</code>
        <code id="RME">CR</code>
        <code id="AD">CR</code>
    </program_codes>

XSLT是我甚至不擅长的一件事,我绝望地需要帮助。

更新,修改整张表格
xsl:if是否正确?

<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="refDoc" value="document('ListCoursesFilter.xml')" />
<xsl:variable name="refIDs" value="$refDoc/program_codes/code/@id" />
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>National Alliance OnLine Courses</title>
                <script TYPE="text/javascript">

                    function popupform(myform, windowname)
                    {
                        var CourseDT = myform.DateCreated.value
                        var CourseDTtoString = CourseDT.substr(0, 4);

                        if (CourseDTtoString == "2007")
                        {
                            window.open('', windowname, 'height=544,width=790,toolbar=no,menubar=no,titlebar=no,scrollbars=yes');
                            myform.target = windowname;
                            return true;
                        } else {
                            window.open('', windowname, 'height=712,width=1014,toolbar=no,menubar=no,titlebar=no,scrollbars=yes');
                            myform.target = windowname;
                            return true;
                        }
                    }
                </script>
            </head>
            <body style="background-repeat: no-repeat; top: 0px">
                <p>
                <table align="center" border="0" style="; width: 569px; height: 89px">
                    <tbody>
                        <tr>
                            <font face="Verdana" size="2">To allow pop up windows from our site, hold down the control key and click "GO"</font><br /><br />
                            <!-- <font face="Verdana" size="3" color="#660000">The CISR, CSRM and William T. Hold self-paced online courses and examination site will be unavailable due to maintenance between the hours of 6:00 a.m. and 10:00 a.m. Central Time on Tuesday, June 17, 2014.  If you need additional assistance please contact us at online@scic.com.</font><br /> -->
                            <!-- <font face="Verdana" size="3" color="#660000">Self-Paced courses will be unavailable due to maintenance June 4th, 2014 at 5:00 pm Central time till Friday morning June 6th, 2014. We are happy to extend your course timeframe to compensate for this closure. Please email <a href="mailto:cisronline@scic.com">cisronline@scic.com</a> with your request for a timeframe extension.</font> -->
                        </tr>
                        <tr>
                            <th>
                                <p align="left">
                                    <font face="Verdana" size="2">Programs</font>
                                </p>
                            </th>
                            <th>
                                <p align="left">
                                    <font face="Verdana" size="2">Course Available Through</font>
                                </p>
                            </th>
                            <th>
                                <p align="left">
                                    <font face="Verdana"/>
                                </p>
                            </th>
                        </tr>
                        <xsl:apply-templates select="/NewDataSet/Table"/>
                    </tbody>
                </table>
                </p>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Table">
        <tr>
            <td>
                <font face="Verdana" color="#ffffff" size="2" style="vertical-align: top; white-space: nowrap">
                    <xsl:value-of select="PRG_DESCRIPTION"/>
                </font>
            </td>
            <td>
                <font face="Verdana" color="#ffffff" size="2" style="vertical-align: top; white-space: nowrap">
                    <xsl:value-of select="DAYS_LEFT"/>
                </font>
            </td>
            <td>&#160;&#160;</td>
            <xsl:if test="ACTIVE = 0 && count($refIDs[contains(current()/EVE_EVENT_CODE, .)]) = 0">
            <td style="top: 2px">
                <form name="myform"  onsubmit="popupform(this, 'join');"  action="../LogOnSection/SetCourseStart.aspx" method="post">                   
                    <input type="hidden" name="CourseID">
                    <xsl:attribute name="value">
                        <xsl:value-of select="EEDO_COURSE_ID"/>
                    </xsl:attribute></input>
                    <input type="hidden" name="UserID"><xsl:attribute name="value">
                        <xsl:value-of select="CLI_RID"/>
                    </xsl:attribute></input>
                    <input type="hidden" name="EventCode"><xsl:attribute name="value">
                        <xsl:value-of select="EVE_EVENT_CODE"/>
                    </xsl:attribute></input>
                    <input type="hidden" name="DateCreated">
                    <xsl:attribute name="value">
                        <xsl:value-of select="DATE_CREATED"/>
                    </xsl:attribute>
                    </input>
                    <input name="Submit" type="submit" value="Go"/>
                    <br>
                    </br>
                </form>
            </td>
            </xsl:if>
        </tr>
    </xsl:template>
</xsl:stylesheet>

这是从数据库中“加载”的方式,GM.AlasData.ActiveOLCourses是一个Web服务函数,它向我们的数据库查询已传递的ParticipantID的活动课程

s = New StringReader(GM.AlasData.ActiveOLCourses(ParticipantID))
xtr = New XmlTextReader(s)

Dim XMLDoc = New XPathDocument(xtr)
Dim XSLTDoc = New XslCompiledTransform

XSLTDoc.Load(Server.MapPath("ListCourseFormat.xslt"))

'Transform XMLDoc and dump HTML results to stringwriter -> sw
XSLTDoc.Transform(XMLDoc, Nothing, sw)

BTW,看起来好像是在使用v1.0

1 个答案:

答案 0 :(得分:0)

据我所知,您只想输出那些<Table>元素,其中<EVE_EVENT_CODE>值未在该二级&#34; program_codes&#34;中提及。 XML文档。

<!-- open extra XML document -->
<xsl:variable name="refDoc" value="document('path/to/program_codes.xml')" />

<!-- collect your IDs from that document -->
<xsl:variable name="refIDs" value="$refDoc/program_codes/code/@id" />

<xsl:template match="/">
    <table>
        <!-- process only those nodes where the IDs don't match -->
        <xsl:apply-templates select="//Table[not(EVE_EVENT_CODE = $refIDs)]" />
    </table>
</xsl:template>

<xsl:template match="Table">
    <tr>
        <td style="font: 12pt Verdana; color: white; vertical-align: top; white-space: nowrap">
            <xsl:value-of select="PRG_DESCRIPTION" />
        </td>
        <td style="font: 12pt Verdana; color: white; vertical-align: top; white-space: nowrap">
            <xsl:value-of select="DAYS_LEFT" />
        </td>
        <td>&#160;&#160;</td>
        <xsl:if test="ACTIVE = 0">
            <td style="top: 2px">
                <form action="../LogOnSection/SetCourseStart.aspx" method="post" onsubmit="popupform(this, 'join');">
                    <input type="hidden" name="CourseID" value="{EEDO_COURSE_ID}" />
                    <input type="hidden" name="UserID" value="{CLI_RID}" />
                    <input type="hidden" name="EventCode" value="{EVE_EVENT_CODE}" />
                    <input type="hidden" name="DateCreated" value="{DATE_CREATED}" />
                    <input name="Submit" type="submit" value="Go" />
                    <br />
                </form>
            </td>
        </xsl:if>
    </tr>
</xsl:template>

提示:

  • 使用属性值模板(上面的花括号)来节省很多打字。
  • 将所有内联样式放入单独的CSS文件中,并使用CSS类。并且删除那些<font>标记并将其从您的记忆中删除。
  • Spacer列也不再是最先进的。使用CSS获取所需的边距,而不是使用<td>&#160;&#160;</td>

编辑在OP发表评论后,我建议采用以下修改后的解决方案:

<!-- open extra XML document -->
<xsl:variable name="refDoc" value="document('path/to/program_codes.xml')" />

<!-- collect your IDs from that document -->
<xsl:variable name="refIDs" value="$refDoc/program_codes/code/@id" />

<xsl:template match="/">
    <table>
        <xsl:apply-templates select="//Table" />
    </table>
</xsl:template>

<xsl:template match="Table">
    <!-- effectively: if there's no refID which is contained in the current event code -->
    <xsl:if test="count($refIDs[contains(current()/EVE_EVENT_CODE, .)]) = 0">
       ...
    </xsl:if>
</xsl:template>