簡體   English   中英

通過XSLT,我可以通過哪種方式使用apply-templates而不是for-each循環

[英]With XSLT, in which way can I use the apply-templates instead of the for-each loop

//我需要這樣做,因此頁面上只會顯示價格低於25的CD。 但是,我僅限於使用apply-templates命令,不能使用for-each。 注意:我無法使用for-each,因為我的演示者想評估我們使用apply templates命令的能力

<?xml version = "1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="show.xsl"?> 

<cdstore>
<cd>
<title>The bests</title>
<price>23.90 </price>
<year>2005 </year>
</cd>

<cd>
<title>The leasts</title>
<price>29.90 </price>
<year>2008 </year>
</cd>

<cd>
<title>The middles</title>
<price>15.90 </price>
<year>2011 </year>
</cd>
</cdstore>

<!--
<?xml version ="1.1" encoding = "ISO-8859-1"?>
<xml: stylesheet version = "1.0" xmlns:xsl="http://www.w3.org">

<xsl: template match = "cdstore">
<html>
<body>
<h3> CDs sale </h3>
<xsl:apply-templates/>
</body>
</html>
<xsl:template match="cd">

<p>
<xsl:apply-templates select = "title"/>
<xsl:apply-templates select = "year"/>
</p>

</xsl:template>

<xsl:template match = "title">
Title: <span><xsl:value-of select="."/></span><br/>
</xsl:template>

<xsl:template match = "year">
Year: <span><xsl:value-of select="."/></span><br />
</xsl:template>

</xsl:stylesheet>

-->

//如何使用xsd:apply模板命令來制作它,以便僅顯示標題和年份,且費用低於25? 不使用for-each循環。 我不確定是否將命令放在apply-templates或value-of的一部分中。 任何幫助是極大的贊賞

您可以執行任一操作-您可以更改apply-templates來限制要將模板應用到的元素:

<h3> CDs sale </h3>
<xsl:apply-templates select="cd[price &lt; 25]"/>

或者,您也可以保留該模板,將模板應用於所有cd元素,但在現有的match="cd"模板旁邊添加第二個模板,以匹配不需要的元素,並忽略它們

<xsl:template match="cd[price &gt;= 25]" />

當您有兩個可以匹配同一節點的不同模板時,所選擇的模板取決於XSLT規范中的特定規則 ,但是本質上,匹配模式"elementname[predicate]"總是會勝過一個匹配的只是"elementname" ,依次贏得一個匹配的"*""node()"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM