简体   繁体   English

xslt 1.0中的for-each

[英]for-each in xslt 1.0

How to do for-each in xsl? 如何在xsl中进行for-each? I get it goes to each element but I would really appreciate an in dept example. 我了解到每个元素,但我真的很感谢indet示例。

You need to change <xsl:for-each select= "database/sims/sim[simID = $callsimID]"> to <xsl:for-each select= "/database/sims/sim[simID = $callsimID]"> to make sure you select the sim elements down from the document node / . 您需要将<xsl:for-each select= "database/sims/sim[simID = $callsimID]">更改为<xsl:for-each select= "/database/sims/sim[simID = $callsimID]">确保您从文档节点/下选择sim元素。

I would however define a key <xsl:key name="sim" match="sims/sim" use="simID"/> and use <xsl:for-each select="key('sim', $callsimID)"> , that should be more efficient. 但是,我将定义一个键<xsl:key name="sim" match="sims/sim" use="simID"/>并使用<xsl:for-each select="key('sim', $callsimID)"> ,那应该更有效率。

This is pretty straightforward and you don't need <xsl:for-each> at all. 这非常简单,您根本不需要<xsl:for-each> Also, you really should modularize your stylesheet. 另外,您确实应该将样式表模块化。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <html>
         <head>
            <title>Customers</title>
         </head>
         <body>
            <xsl:apply-templates select="database" />
         </body>
      </html>
   </xsl:template>

   <xsl:template match="database">
     <table rules="all">
         <caption>Customers</caption>
         <xsl:apply-templates select="
            sims/sim[
               simID = current()/calls/call[
                  countryCodeOfOtherParty = '49' 
                  and areaCodeOfOtherParty = '31' 
                  and numberCodeOfOtherParty ='124567'
               ]/simID
            ]
         " />
      </table>
   </xsl:template>

   <xsl:template match="sim">
      <tr>
         <td><xsl:value-of select="customerID" /></td>
      </tr>
   </xsl:template>
</xsl:stylesheet>

where 哪里

sims/sim[
   simID = current()/calls/call[
      countryCodeOfOtherParty = '49' 
      and areaCodeOfOtherParty = '31' 
      and numberCodeOfOtherParty ='124567'
   ]/simID
]

unsurprisingly means "every SIM that has a call to that particular number" . 毫不奇怪,这意味着“每个呼叫该特定号码的SIM卡” Note the use of the current() function to reference the <database> in mid-XPath. 请注意,使用current()函数来引用XPath中间的<database>

You can improve performance by using an <xsl:key> , but in the end that's only necessary if your <database> is very large and the stylesheet becomes sluggish. 您可以使用<xsl:key>来提高性能,但是最后,仅当<database>很大并且样式表变得缓慢时才需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM