XSLT,如何为具有数字循环的查找表交替 key() 函数



我制作了一个查找表,将月份的名称转换为相应的月份编号。

它有点像:

<lookup:months>
<lookup:month><lookup:nr>1</lookup:nr> <lookup:name>January</lookup:name></lookup:month>
<lookup:month><lookup:nr>2</lookup:nr> <lookup:name>February</lookup:name></lookup:month> 
<lookup:month><lookup:nr>3</lookup:nr> <lookup:name>March</lookup:name></lookup:month>

它有效,从以下数据开始:

<Months>
<Month>March</Month>

转换为:

<li class="active">3</li>

但是在这种情况下(以及在所有情况下),我想得到一个最终的标记,例如:

<li>1</li>
<li>2</li>
<li class="active">3</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>

我怎样才能得到这个结果?

我想我必须使用类似的东西:

<xsl:for-each select="1 to 12">
[pseudo code] when key match, value of "key(args)/lookup:nr"
otherwise value-of select="."

我已经尝试过,但我无法让它工作...

如果您的输入中只有一个月份值,但想要生成所有月份数字的列表,那么假设 XSLT 2.0,则使用 XSLT 就足够

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:param name="months"
select="'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'"/>
<xsl:output indent="yes"/>
<xsl:template match="Months">
<xsl:variable name="month" select="Month"/>
<xsl:for-each select="1 to 12">
<li>
<xsl:if test="$months[current()] = $month">
<xsl:attribute name="class">active</xsl:attribute>
</xsl:if>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</xsl:template>
</xsl:transform>

http://xsltransform.net/jz1PuQm

@Martin Honnen 的答案肯定是最精致和简短的,但如果你想使用带有 key() 的查找表系统,可以扩展到其他参数,我设法找到了这个经过测试的解决方案:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<!-- lookup table -->
<xsl:variable name="months-lookup-table">
<month><nr>1</nr> <name>January</name></month>
<month><nr>2</nr> <name>February</name></month> 
<month><nr>3</nr> <name>March</name></month> 
<month><nr>4</nr> <name>April</name></month> 
<month><nr>5</nr> <name>May</name></month> 
<month><nr>6</nr> <name>June</name></month> 
<month><nr>7</nr>  <name>July</name></month> 
<month><nr>8</nr>  <name>August</name></month> 
<month><nr>9</nr>  <name>September</name></month> 
<month><nr>10</nr> <name>October</name></month> 
<month><nr>11</nr> <name>November</name></month> 
<month><nr>12</nr> <name>December</name></month> 
</xsl:variable>
<!-- key to convert name month (in month number) -->
<xsl:key name="from-name-of-month-to-nr-of-month" match="month" use="name"/>

<xsl:template match="Months">

<!-- save the selected month context -->
<xsl:variable name="selected-month" select="Month"/>
<!-- loop all months name in the lookup table, THIS is the active loop -->
<xsl:for-each select="$months-lookup-table/month">
<!-- print the current looped month transformed from name to number, if the current looped month ($month-in-loop) name matches with the month name in XML source ($selected-month), add  an attribute of "active" -->
<xsl:element name="li">
<xsl:if test="$selected-month = name">
<xsl:attribute name="class">active</xsl:attribute>
</xsl:if>
<xsl:value-of select="key('from-name-of-month-to-nr-of-month', name, $months-lookup-table)/nr"/>
</xsl:element>
</xsl:for-each>        

</xsl:template>

http://xsltransform.net/3NSSEwa/2

最新更新