How to do for-each in xsl? I get it goes to each element but I would really appreciate an in dept example.
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 /
.
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.
This is pretty straightforward and you don't need <xsl:for-each>
at all. 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" . Note the use of the current()
function to reference the <database>
in mid-XPath.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.