2015-02-23 128 views
0

我想在xslt 1.0中执行sipmle'Muenchian分组',但也要从不同节点添加通配符搜索。在XSLT 1.0中使用通配符搜索进行分组

我很熟悉分组方法,它的工作很好,但在通配符问题上遇到问题,因为它总是显示没有通配符搜索的组标题。

这里是我的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> 

我想下面给出字符串 '我' 的通配符SEACH输出:

<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> 

我似乎无法得到“包含”条件对多年工作,只有实际的称号。我试过在年头前移动并复制它,但它什么也没有返回。另外,我不能在原来的关键结构中包含一个搜索变量来解决这个问题(我认为)。

回答

1

你需要做的这两次传球。首先,找到符合您的搜索条件的节点。然后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> 

应用到你的输入例如,使用参数是字符串 “我的”,该结果是:

<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> 

注意:您不能在密钥的匹配模式中使用变量。

0

这会做,我相信:

<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> 
+0

这两种方法都有效。非常感谢 – kevmull 2015-02-23 16:57:12