在 XSLT 1.0 中使用通配符搜索进行分组
Grouping with wildcard search in XSLT 1.0
我想在 xslt 1.0 中做一个简单的 'Muenchian grouping',但也想从不同的节点添加通配符搜索。
我熟悉分组方法,它的 awlays 运行良好,但我遇到了通配符问题,因为它总是显示没有通配符搜索的组标题。
这是我的 xml:
<objects>
<object>
<output_title>This is a title</output_title>
<output_year>2011</output_year>
</object>
<object>
<output_title>This is my title</output_title>
<output_year>2012</output_year>
</object>
<object>
<output_title>This is also my title</output_title>
<output_year>2012</output_year>
</object>
<object>
<output_title>This is another my title</output_title>
<output_year>2012</output_year>
</object>
<object>
<output_title>This is my title</output_title>
<output_year>2014</output_year>
</object>
<object>
<output_title>This is our title</output_title>
<output_year>2015</output_year>
</object>
</objects>
给定字符串 'my':
的通配符搜索,我想输出以下内容
<h4>2012</h4>
<ol>
<li>This is my title</li>
<li>This is also my title</li>
<li>This is another my title</li>
</ol>
<h4>2014</h4>
<ol>
<li>This is my title</li>
</ol>
所以标题“2011”和“2015”不应该出现(但它们出现了,这就是问题所在)
这是我的 XSLT 1.0:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0">
<xsl:param name="search"/>
<!--Key for unique years-->
<xsl:key name="uniqueOutputYearHeading"
match="contains(translate(objects/object/output_title,$uppercase,$lowercase),translate($search,$uppercase,$lowercase))"
use="output_year"/>
<xsl:template match="/">
<!-- Generate the unique year headings-->
<xsl:for-each select="//object[generate-id(.)=generate-id(key('uniqueOutputYearHeading',output_year))]">
<xsl:sort select="output_year" data-type="number" order="descending"/>
<h4>
<xsl:value-of select="output_year"/>
</h4>
<ol>
<xsl:for-each select="key('uniqueOutputYearHeading',output_year)">
<xsl:for-each select="output_citation">
<xsl:if test="contains(translate(current(),$uppercase,$lowercase),translate($search,$uppercase,$lowercase))">
<li class="margin-bottom">
<xsl:copy-of select="current()"/>
</li>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</ol>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
但我得到:
<h4>2011</h4>
<h4>2012</h4>
<ol>
<li>This is my title</li>
<li>This is also my title</li>
<li>This is another my title</li>
<h4>2014</h4>
<ol>
<li>This is my title</li>
</ol>
<h4>2015</h4>
我似乎无法让 'contains' 条件适用于年份,只有实际标题。我试过在年度标题之前移动和复制它,但它 returns 什么都没有。此外,我不能在原始密钥结构中包含搜索变量,这将解决问题(我认为)。
我相信这样就可以了:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="search">my</xsl:param>
<xsl:key name="uniqueOutputYearHeading" match="object" use="output_year"/>
<xsl:template match="objects">
<xsl:copy>
<xsl:for-each select="object[contains(output_title,$search)][generate-id() = generate-id(key('uniqueOutputYearHeading', output_year)[contains(output_title,$search)][1])]">
<h4>
<xsl:value-of select="output_year"/>
</h4>
<ol>
<xsl:for-each select="key('uniqueOutputYearHeading', output_year)[contains(output_title,$search)]">
<li>
<xsl:value-of select="output_title"/>
</li>
</xsl:for-each>
</ol>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
您需要分两次完成此操作。首先,找到符合搜索条件的节点。然后对结果应用 Muenchian 分组:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:param name="search" />
<xsl:key name="object-by-year" match="object" use="output_year"/>
<xsl:template match="/objects">
<!-- first pass -->
<xsl:variable name="found-objects">
<xsl:copy-of select="object[contains(., $search)]"/>
</xsl:variable>
<xsl:variable name="found-objects-set" select="exsl:node-set($found-objects)" />
<!-- second (final) pass -->
<html>
<!-- switch context to the variable -->
<xsl:for-each select="$found-objects-set">
<!-- for each distinct year ... -->
<xsl:for-each select="object[generate-id(.)=generate-id(key('object-by-year', output_year))]">
<xsl:sort select="output_year" data-type="number"/>
<h4>
<xsl:value-of select="output_year"/>
</h4>
<!-- ... list the found objects in this year -->
<ol>
<xsl:for-each select="key('object-by-year', output_year)">
<li><xsl:value-of select="output_title" /></li>
</xsl:for-each>
</ol>
</xsl:for-each>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例,参数为字符串"my",结果为:
<html>
<h4>2012</h4>
<ol>
<li>This is my title</li>
<li>This is also my title</li>
<li>This is another my title</li>
</ol>
<h4>2014</h4>
<ol>
<li>This is my title</li>
</ol>
</html>
注意:不能在键的匹配模式中使用变量。
我想在 xslt 1.0 中做一个简单的 'Muenchian grouping',但也想从不同的节点添加通配符搜索。
我熟悉分组方法,它的 awlays 运行良好,但我遇到了通配符问题,因为它总是显示没有通配符搜索的组标题。
这是我的 xml:
<objects>
<object>
<output_title>This is a title</output_title>
<output_year>2011</output_year>
</object>
<object>
<output_title>This is my title</output_title>
<output_year>2012</output_year>
</object>
<object>
<output_title>This is also my title</output_title>
<output_year>2012</output_year>
</object>
<object>
<output_title>This is another my title</output_title>
<output_year>2012</output_year>
</object>
<object>
<output_title>This is my title</output_title>
<output_year>2014</output_year>
</object>
<object>
<output_title>This is our title</output_title>
<output_year>2015</output_year>
</object>
</objects>
给定字符串 'my':
的通配符搜索,我想输出以下内容<h4>2012</h4>
<ol>
<li>This is my title</li>
<li>This is also my title</li>
<li>This is another my title</li>
</ol>
<h4>2014</h4>
<ol>
<li>This is my title</li>
</ol>
所以标题“2011”和“2015”不应该出现(但它们出现了,这就是问题所在)
这是我的 XSLT 1.0:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0">
<xsl:param name="search"/>
<!--Key for unique years-->
<xsl:key name="uniqueOutputYearHeading"
match="contains(translate(objects/object/output_title,$uppercase,$lowercase),translate($search,$uppercase,$lowercase))"
use="output_year"/>
<xsl:template match="/">
<!-- Generate the unique year headings-->
<xsl:for-each select="//object[generate-id(.)=generate-id(key('uniqueOutputYearHeading',output_year))]">
<xsl:sort select="output_year" data-type="number" order="descending"/>
<h4>
<xsl:value-of select="output_year"/>
</h4>
<ol>
<xsl:for-each select="key('uniqueOutputYearHeading',output_year)">
<xsl:for-each select="output_citation">
<xsl:if test="contains(translate(current(),$uppercase,$lowercase),translate($search,$uppercase,$lowercase))">
<li class="margin-bottom">
<xsl:copy-of select="current()"/>
</li>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</ol>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
但我得到:
<h4>2011</h4>
<h4>2012</h4>
<ol>
<li>This is my title</li>
<li>This is also my title</li>
<li>This is another my title</li>
<h4>2014</h4>
<ol>
<li>This is my title</li>
</ol>
<h4>2015</h4>
我似乎无法让 'contains' 条件适用于年份,只有实际标题。我试过在年度标题之前移动和复制它,但它 returns 什么都没有。此外,我不能在原始密钥结构中包含搜索变量,这将解决问题(我认为)。
我相信这样就可以了:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="search">my</xsl:param>
<xsl:key name="uniqueOutputYearHeading" match="object" use="output_year"/>
<xsl:template match="objects">
<xsl:copy>
<xsl:for-each select="object[contains(output_title,$search)][generate-id() = generate-id(key('uniqueOutputYearHeading', output_year)[contains(output_title,$search)][1])]">
<h4>
<xsl:value-of select="output_year"/>
</h4>
<ol>
<xsl:for-each select="key('uniqueOutputYearHeading', output_year)[contains(output_title,$search)]">
<li>
<xsl:value-of select="output_title"/>
</li>
</xsl:for-each>
</ol>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
您需要分两次完成此操作。首先,找到符合搜索条件的节点。然后对结果应用 Muenchian 分组:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:param name="search" />
<xsl:key name="object-by-year" match="object" use="output_year"/>
<xsl:template match="/objects">
<!-- first pass -->
<xsl:variable name="found-objects">
<xsl:copy-of select="object[contains(., $search)]"/>
</xsl:variable>
<xsl:variable name="found-objects-set" select="exsl:node-set($found-objects)" />
<!-- second (final) pass -->
<html>
<!-- switch context to the variable -->
<xsl:for-each select="$found-objects-set">
<!-- for each distinct year ... -->
<xsl:for-each select="object[generate-id(.)=generate-id(key('object-by-year', output_year))]">
<xsl:sort select="output_year" data-type="number"/>
<h4>
<xsl:value-of select="output_year"/>
</h4>
<!-- ... list the found objects in this year -->
<ol>
<xsl:for-each select="key('object-by-year', output_year)">
<li><xsl:value-of select="output_title" /></li>
</xsl:for-each>
</ol>
</xsl:for-each>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例,参数为字符串"my",结果为:
<html>
<h4>2012</h4>
<ol>
<li>This is my title</li>
<li>This is also my title</li>
<li>This is another my title</li>
</ol>
<h4>2014</h4>
<ol>
<li>This is my title</li>
</ol>
</html>
注意:不能在键的匹配模式中使用变量。