0

我正在尝试学习 XSLT 并遇到了一些问题。我认为这条线<xsl:template match="response/results">会将 xlst 进程限制为仅元素中的那些<results>元素???

这会输出一些初始值,我不知道为什么。在下面的结果中,您将在结果的开头看到以下内容:

manhattan WMOS
true
false
60
1
10
0

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<response version="2">
    <query>manhattan WMOS</query>
    <location></location>
    <dupefilter>true</dupefilter>
    <highlight>false</highlight>
    <totalresults>60</totalresults>
    <start>1</start>
    <end>10</end>
    <pageNumber>0</pageNumber>
    <results>
        <result>
            <jobtitle>Manhattan Associates WMoS QA Analyst</jobtitle>
            <company>CMAC Inc</company>
            <city>Asheville</city>
            <state>NC</state>
            <country>US</country>
            <formattedLocation>Asheville, NC</formattedLocation>
            <source>JobServe USA</source>
            <date>Thu, 11 Oct 2012 15:20:31 GMT</date>
            <snippet>Manhattan Associtates WMoS Loads of Documentation and detail required for this position Immediate opening for 2 Senior QA Analysts Project: Manhattan (WMOS... </snippet>
            <url>http://www.indeed.com/viewjob?jk=06bef17e206740d3&amp;qd=Cg_V6UykGXt-ccaEWxs0BA1LyorWd-7Tcbya1NtZKa9WZ1DtngXSHw91EGjVCy_rntA_Y_5cpbSuoLd44_xEjXNG-vBD0DC792zHqZRp6lE&amp;indpubnum=1813651566614396&amp;atk=179li835k18b17ae</url>
            <onmousedown>indeed_clk(this, &#039;5585&#039;);</onmousedown>
            <jobkey>06bef17e206740d3</jobkey>
            <sponsored>false</sponsored>
            <expired>false</expired>
            <formattedLocationFull>Asheville, NC</formattedLocationFull>
            <formattedRelativeTime>5 days ago</formattedRelativeTime>
        </result>
        <result>
            <jobtitle>Software Analyst - WMOS</jobtitle>
            <company>Manhattan Associates</company>
            <city>Atlanta</city>
            <state>GA</state>
            <country>US</country>
            <formattedLocation>Atlanta, GA</formattedLocation>
            <source>Manhattan Associates</source>
            <date>Mon, 01 Oct 2012 21:02:50 GMT</date>
            <snippet>innovations include: Manhattan SCOPE® a portfolio of... Position open in Atlanta Office. About Manhattan Associates:  Manhattan Associates is at the forefront of... </snippet>
            <url>http://www.indeed.com/viewjob?jk=a17a7c578a779db8&amp;qd=Cg_V6UykGXt-ccaEWxs0BA1LyorWd-7Tcbya1NtZKa9WZ1DtngXSHw91EGjVCy_rntA_Y_5cpbSuoLd44_xEjXNG-vBD0DC792zHqZRp6lE&amp;indpubnum=1813651566614396&amp;atk=179li835k18b17ae</url>
            <onmousedown>indeed_clk(this, &#039;5585&#039;);</onmousedown>
            <jobkey>a17a7c578a779db8</jobkey>
            <sponsored>false</sponsored>
            <expired>false</expired>
            <formattedLocationFull>Atlanta, GA</formattedLocationFull>
            <formattedRelativeTime>14 days ago</formattedRelativeTime>
        </result>

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:param name="ItemsToShow" select="'2'"/>
<xsl:param name="ShowItemDetails" select="'true'"/>
<xsl:param name="ShowItemDate" select="'true'"/>
<xsl:param name="Locale"/>
<xsl:param name="Target"/>
  <xsl:variable name="apos">'</xsl:variable>

<xsl:template name="google_analytics_event_tracking">
    <xsl:param name="link_category" />
    <xsl:param name="link_sub_category" />
     <xsl:param name="link_data" />

  <xsl:value-of select="concat('_gaq.push([', $apos, '_trackEvent', $apos, ',', $apos, $link_category, $apos, ',', $apos, $link_sub_category, $apos, ',', $apos, $link_data, $apos,')]')" />
</xsl:template>
  <xsl:template match="response/results">
    <xsl:for-each select="result[position()&lt;=$ItemsToShow or $ItemsToShow&lt;1]">
        <xsl:variable name="jobOutboundLinkAction">
            <xsl:call-template name="google_analytics_event_tracking">
                <xsl:with-param name="link_category" select="'indeed.com'" />
                <xsl:with-param name="link_sub_category" select="'none'" />
                <xsl:with-param name="link_data" select="url" />
            </xsl:call-template>
        </xsl:variable>
        <div class="jobWrapper">
            <div class="jobTitle">
                <a href="{url}">
                    <xsl:attribute name="target">
                        <xsl:value-of select="$Target"/>
                    </xsl:attribute>
                    <xsl:attribute name="onclick">
                        <xsl:value-of select="$jobOutboundLinkAction"/>
                    </xsl:attribute>
                    <xsl:value-of select="jobtitle"/>
                </a>
            </div>
            <xsl:if test="$ShowItemDate='true'">
                <h6>
                    <xsl:value-of select="date"/>
                </h6>
            </xsl:if>
            <xsl:if test="$ShowItemDetails='true'">
                Company: <xsl:value-of select="company" disable-output-escaping="yes"/>
                <br />
                <xsl:value-of select="formattedLocation" disable-output-escaping="yes"/>
                <br />
                <p>
                    <xsl:value-of select="snippet" disable-output-escaping="yes"/>
                </p>
            </xsl:if>
            <div class="cleaner"></div>
        </div>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

输出:

manhattan WMOS
true
false
60
1
10
0
<div class="jobWrapper">
    <div class="jobTitle">
        <a href="http://www.indeed.com/viewjob?jk=06bef17e206740d3&amp;qd=Cg_V6UykGXt-ccaEWxs0BA1LyorWd-7Tcbya1NtZKa9WZ1DtngXSHw91EGjVCy_rntA_Y_5cpbSuoLd44_xEjXNG-vBD0DC792zHqZRp6lE&amp;indpubnum=1813651566614396&amp;atk=179li835k18b17ae" target="" onclick="_gaq.push(['_trackEvent','indeed.com','none','http://www.indeed.com/viewjob?jk=06bef17e206740d3&amp;qd=Cg_V6UykGXt-ccaEWxs0BA1LyorWd-7Tcbya1NtZKa9WZ1DtngXSHw91EGjVCy_rntA_Y_5cpbSuoLd44_xEjXNG-vBD0DC792zHqZRp6lE&amp;indpubnum=1813651566614396&amp;atk=179li835k18b17ae')]">Manhattan Associates WMoS QA Analyst</a>
    </div>
    <h6>Thu, 11 Oct 2012 15:20:31 GMT</h6>
    Company: CMAC Inc<br>
        Asheville, NC<br>
            <p>Manhattan Associtates WMoS Loads of Documentation and detail required for this position Immediate opening for 2 Senior QA Analysts Project: Manhattan (WMOS... </p>
            <div class="cleaner"></div>
        </div>
        <div class="jobWrapper">
            <div class="jobTitle">
                <a href="http://www.indeed.com/viewjob?jk=a17a7c578a779db8&amp;qd=Cg_V6UykGXt-ccaEWxs0BA1LyorWd-7Tcbya1NtZKa9WZ1DtngXSHw91EGjVCy_rntA_Y_5cpbSuoLd44_xEjXNG-vBD0DC792zHqZRp6lE&amp;indpubnum=1813651566614396&amp;atk=179li835k18b17ae" target="" onclick="_gaq.push(['_trackEvent','indeed.com','none','http://www.indeed.com/viewjob?jk=a17a7c578a779db8&amp;qd=Cg_V6UykGXt-ccaEWxs0BA1LyorWd-7Tcbya1NtZKa9WZ1DtngXSHw91EGjVCy_rntA_Y_5cpbSuoLd44_xEjXNG-vBD0DC792zHqZRp6lE&amp;indpubnum=1813651566614396&amp;atk=179li835k18b17ae')]">Software Analyst - WMOS</a>
            </div>
            <h6>Mon, 01 Oct 2012 21:02:50 GMT</h6>
            Company: Manhattan Associates<br>
                Atlanta, GA<br>
                    <p>innovations include: Manhattan SCOPE® a portfolio of... Position open in Atlanta Office. About Manhattan Associates:  Manhattan Associates is at the forefront of... </p>
                    <div class="cleaner"></div>
                </div>
4

2 回答 2

0

您只有一个可匹配的模板。因此,与那个不匹配的所有内容都将由 XSLT 内置模板处理。

您需要为其他元素创建额外的模板。像这样的东西...

<xsl:template match="*" />

... 将接收所有未与其他模板特别匹配的元素。

于 2012-10-17T00:08:50.827 回答
0

啊,找到这个链接

添加此以过滤掉不需要的内容:

<xsl:template match="response">
    <xsl:apply-templates select="results"/>
</xsl:template>
于 2012-10-17T00:03:28.487 回答