字母索引模板:“xsl:key”匹配问题

发布于 2024-09-14 12:50:16 字数 3312 浏览 3 评论 0原文

XML:

   <?xml version="1.0" encoding="windows-1251"?>
   <mode>
        <term>
            <name>abhdk</name>
        </term>
        <term>
            <name>njhjsu</name>
        </term>

        <term>
            <name>sdasd</name>
        </term>
        <term>
            <name>vbvbcd</name>
        </term>
            ...
    </mode>
    <mode>
        <term>
            <name>asdfd</name>
        </term>
        <term>
            <name>vcbbn</name>
        </term>
        <term>
            <name>bnmbnmb</name>
        </term>
        <term>
            <name>tyutyu</name>
        </term>
        <term>
            <name>ghjghj</name>
        </term>
        <term>
            <name>hjk</name>
        </term>
            ...
    </mode>
    <mode>
        <term>
            <name>asdfd</name>
        </term>
        <term>
            <name>vcbbn</name>
        </term>
    </mode>
    ...

我需要像这样进行字母表索引:

alt text

我从这里获取模板: 链接文本

我的问题:对于每个模式必须有自己的“字母表索引”列表(对于单一模式,它工作得很好)。

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="no" encoding="windows-1251"/>

    <xsl:for-each select="mode">
        <xsl:call-template name="alphabetIndexTmpl">
            <xsl:with-param name="key-name" select="'items-key'" />
            ...
        </xsl:call-template>
    </xsl:for-each>

    <xsl:key name="items-key" match="term" use="substring(., 1, 1)" />

    <xsl:template name="alphabetIndexTmpl">         
        <xsl:param name="key-name"/>
        ...
    </xsl:template>
</xsl:stylesheet>

此代码无法正常工作。 必须这样纠正:

<?xml version="1.0" encoding="windows-1251"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="no" encoding="windows-1251"/>

    <xsl:for-each select="mode">
        <xsl:call-template name="alphabetIndexTmpl">
            <xsl:with-param name="key-name" select="concat('items-key', position())" />
            ...
        </xsl:call-template>
    </xsl:for-each>

    <xsl:key name="items-key1" match="mode[1]/term" use="substring(., 1, 1)" />
    <xsl:key name="items-key2" match="mode[2]/term" use="substring(., 1, 1)" />
    <xsl:key name="items-key3" match="mode[2]/term" use="substring(., 1, 1)" />
    ...

    <xsl:template name="alphabetIndexTmpl">         
        <xsl:param name="key-name"/>
        ...
    </xsl:template>

</xsl:stylesheet>

但这是一个非常错误的决定。

如何更优雅地解决这个问题呢?


迪米特雷·诺瓦切夫,不要因为“不准确”而打倒我,我投降了我的武器))。

XML:

   <?xml version="1.0" encoding="windows-1251"?>
   <mode>
        <term>
            <name>abhdk</name>
        </term>
        <term>
            <name>njhjsu</name>
        </term>

        <term>
            <name>sdasd</name>
        </term>
        <term>
            <name>vbvbcd</name>
        </term>
            ...
    </mode>
    <mode>
        <term>
            <name>asdfd</name>
        </term>
        <term>
            <name>vcbbn</name>
        </term>
        <term>
            <name>bnmbnmb</name>
        </term>
        <term>
            <name>tyutyu</name>
        </term>
        <term>
            <name>ghjghj</name>
        </term>
        <term>
            <name>hjk</name>
        </term>
            ...
    </mode>
    <mode>
        <term>
            <name>asdfd</name>
        </term>
        <term>
            <name>vcbbn</name>
        </term>
    </mode>
    ...

I need to do Alphabet index like this:

alt text

I get template from here: link text

My problem: for each mode there must be its own "Alphabet index" list (for single mode it works perfectly).

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="no" encoding="windows-1251"/>

    <xsl:for-each select="mode">
        <xsl:call-template name="alphabetIndexTmpl">
            <xsl:with-param name="key-name" select="'items-key'" />
            ...
        </xsl:call-template>
    </xsl:for-each>

    <xsl:key name="items-key" match="term" use="substring(., 1, 1)" />

    <xsl:template name="alphabetIndexTmpl">         
        <xsl:param name="key-name"/>
        ...
    </xsl:template>
</xsl:stylesheet>

This code does not work correctly.
It has to be corrected like this:

<?xml version="1.0" encoding="windows-1251"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="no" encoding="windows-1251"/>

    <xsl:for-each select="mode">
        <xsl:call-template name="alphabetIndexTmpl">
            <xsl:with-param name="key-name" select="concat('items-key', position())" />
            ...
        </xsl:call-template>
    </xsl:for-each>

    <xsl:key name="items-key1" match="mode[1]/term" use="substring(., 1, 1)" />
    <xsl:key name="items-key2" match="mode[2]/term" use="substring(., 1, 1)" />
    <xsl:key name="items-key3" match="mode[2]/term" use="substring(., 1, 1)" />
    ...

    <xsl:template name="alphabetIndexTmpl">         
        <xsl:param name="key-name"/>
        ...
    </xsl:template>

</xsl:stylesheet>

But it is very crooked decision.

How to solve this problem more elegantly?


Dimitre Novatchev, do not beat me down for "inaccuracies", I surrender my arms)).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随波逐流 2024-09-21 12:50:16

此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kByMode-StartCharAndModPos" match="term"
  use="concat(count(../preceding-sibling::mode),
              '+',
              substring(name,1,1)
              )"/>

 <xsl:template match="node()|@*" mode="identity">
  <xsl:copy>
   <xsl:apply-templates mode="identity"
    select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/*">
  <modes>
   <xsl:apply-templates/>
  </modes>
 </xsl:template>

 <xsl:template match="mode">
   <mode pos="{position()}">
       <xsl:apply-templates select=
       "term[generate-id()
            =
             generate-id(key('kByMode-StartCharAndModPos',
                              concat(count(../preceding-sibling::mode),
                                     '+',
                                     substring(name,1,1)
                                     )
                             )[1]
                         )
             ]">
     <xsl:sort select="substring(name,1,1)"/>
   </xsl:apply-templates>
  </mode>
 </xsl:template>

 <xsl:template match="term">
   <starting letter="{substring(name,1,1)}">
    <xsl:apply-templates mode="identity" select=
     "key('kByMode-StartCharAndModPos',
             concat(count(../preceding-sibling::mode),
                    '+',
                    substring(name,1,1)
                    )
            )
     "/>
   </starting>
 </xsl:template>
</xsl:stylesheet>

应用于此 XML 文档时

<modes>
    <mode>
        <term>
            <name>Adams</name>
        </term>
        <term>
            <name>Allen</name>
        </term>
        <term>
            <name>Brooks</name>
        </term>
        <term>
            <name>Cameron</name>
            <name>Campbell</name>
        </term>
    </mode>
    <mode>
        <term>
            <name>Apple</name>
        </term>
        <term>
            <name>Egan</name>
            <name>Elkin</name>
        </term>
        <term>
            <name>Fisher</name>
            <name>Foster</name>
        </term>
        <term>
            <name>Hall</name>
        </term>
        <term>
            <name>ghjghj</name>
        </term>
        <term>
            <name>Jackson</name>
        </term>
    </mode>
    <mode>
        <term>
            <name>Lewis</name>
        </term>
        <term>
            <name>Murray</name>
            <name>Myers</name>
        </term>
    </mode>
</modes>

为每个 生成所需的索引(无html 中的格式化正在完成):

<modes>
    <mode pos="1">
        <starting letter="A">
            <term>
                <name>Adams</name>
            </term>
            <term>
                <name>Allen</name>
            </term>
        </starting>
        <starting letter="B">
            <term>
                <name>Brooks</name>
            </term>
        </starting>
        <starting letter="C">
            <term>
                <name>Cameron</name>
                <name>Campbell</name>
            </term>
        </starting>
    </mode>
    <mode pos="2">
        <starting letter="A">
            <term>
                <name>Apple</name>
            </term>
        </starting>
        <starting letter="E">
            <term>
                <name>Egan</name>
                <name>Elkin</name>
            </term>
        </starting>
        <starting letter="F">
            <term>
                <name>Fisher</name>
                <name>Foster</name>
            </term>
        </starting>
        <starting letter="g">
            <term>
                <name>ghjghj</name>
            </term>
        </starting>
        <starting letter="H">
            <term>
                <name>Hall</name>
            </term>
        </starting>
        <starting letter="J">
            <term>
                <name>Jackson</name>
            </term>
        </starting>
    </mode>
    <mode pos="3">
        <starting letter="L">
            <term>
                <name>Lewis</name>
            </term>
        </starting>
        <starting letter="M">
            <term>
                <name>Murray</name>
                <name>Myers</name>
            </term>
        </starting>
    </mode>
</modes>

请注意: Muenchian 分组方法与包含模式位置和术语名称的复合键一起使用。

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kByMode-StartCharAndModPos" match="term"
  use="concat(count(../preceding-sibling::mode),
              '+',
              substring(name,1,1)
              )"/>

 <xsl:template match="node()|@*" mode="identity">
  <xsl:copy>
   <xsl:apply-templates mode="identity"
    select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/*">
  <modes>
   <xsl:apply-templates/>
  </modes>
 </xsl:template>

 <xsl:template match="mode">
   <mode pos="{position()}">
       <xsl:apply-templates select=
       "term[generate-id()
            =
             generate-id(key('kByMode-StartCharAndModPos',
                              concat(count(../preceding-sibling::mode),
                                     '+',
                                     substring(name,1,1)
                                     )
                             )[1]
                         )
             ]">
     <xsl:sort select="substring(name,1,1)"/>
   </xsl:apply-templates>
  </mode>
 </xsl:template>

 <xsl:template match="term">
   <starting letter="{substring(name,1,1)}">
    <xsl:apply-templates mode="identity" select=
     "key('kByMode-StartCharAndModPos',
             concat(count(../preceding-sibling::mode),
                    '+',
                    substring(name,1,1)
                    )
            )
     "/>
   </starting>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<modes>
    <mode>
        <term>
            <name>Adams</name>
        </term>
        <term>
            <name>Allen</name>
        </term>
        <term>
            <name>Brooks</name>
        </term>
        <term>
            <name>Cameron</name>
            <name>Campbell</name>
        </term>
    </mode>
    <mode>
        <term>
            <name>Apple</name>
        </term>
        <term>
            <name>Egan</name>
            <name>Elkin</name>
        </term>
        <term>
            <name>Fisher</name>
            <name>Foster</name>
        </term>
        <term>
            <name>Hall</name>
        </term>
        <term>
            <name>ghjghj</name>
        </term>
        <term>
            <name>Jackson</name>
        </term>
    </mode>
    <mode>
        <term>
            <name>Lewis</name>
        </term>
        <term>
            <name>Murray</name>
            <name>Myers</name>
        </term>
    </mode>
</modes>

produces the wanted indexes for every <mode> (no formatization in html is being done):

<modes>
    <mode pos="1">
        <starting letter="A">
            <term>
                <name>Adams</name>
            </term>
            <term>
                <name>Allen</name>
            </term>
        </starting>
        <starting letter="B">
            <term>
                <name>Brooks</name>
            </term>
        </starting>
        <starting letter="C">
            <term>
                <name>Cameron</name>
                <name>Campbell</name>
            </term>
        </starting>
    </mode>
    <mode pos="2">
        <starting letter="A">
            <term>
                <name>Apple</name>
            </term>
        </starting>
        <starting letter="E">
            <term>
                <name>Egan</name>
                <name>Elkin</name>
            </term>
        </starting>
        <starting letter="F">
            <term>
                <name>Fisher</name>
                <name>Foster</name>
            </term>
        </starting>
        <starting letter="g">
            <term>
                <name>ghjghj</name>
            </term>
        </starting>
        <starting letter="H">
            <term>
                <name>Hall</name>
            </term>
        </starting>
        <starting letter="J">
            <term>
                <name>Jackson</name>
            </term>
        </starting>
    </mode>
    <mode pos="3">
        <starting letter="L">
            <term>
                <name>Lewis</name>
            </term>
        </starting>
        <starting letter="M">
            <term>
                <name>Murray</name>
                <name>Myers</name>
            </term>
        </starting>
    </mode>
</modes>

Do note: Muenchian method for grouping is used with a composite key that contains both the mode's position and the name of the term.

下雨或天晴 2024-09-21 12:50:16

有了正确的输入:

<modes>
    <mode>
        <term>
            <name>abhdk</name>
        </term>
        <term>
            <name>njhjsu</name>
        </term>
        <term>
            <name>sdasd</name>
        </term>
        <term>
            <name>vbvbcd</name>
        </term>
    </mode>
    <mode>
        <term>
            <name>asdfd</name>
        </term>
        <term>
            <name>vcbbn</name>
        </term>
        <term>
            <name>bnmbnmb</name>
        </term>
        <term>
            <name>tyutyu</name>
        </term>
        <term>
            <name>ghjghj</name>
        </term>
        <term>
            <name>hjk</name>
        </term>
    </mode>
    <mode>
        <term>
            <name>asdfd</name>
        </term>
        <term>
            <name>vcbbn</name>
        </term>
    </mode>
</modes>

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="no" encoding="windows-1251"/>
    <xsl:key name="ByModeAndFirst" match="name" use="concat(generate-id(../..),'&',substring(.,1,1))"/>
    <xsl:template match="modes">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="mode">
        <div>
        <xsl:apply-templates>
            <xsl:sort select="name"/>
        </xsl:apply-templates>
        </div>
    </xsl:template>
    <xsl:template match="term"/>
    <xsl:template match="term[name[count(.|key('ByModeAndFirst',concat(generate-id(../..),'&',substring(.,1,1)))[1])=1]]">
        <dl>
            <dt>
                <xsl:value-of select="substring(name,1,1)"/>
            </dt>
            <dd>
                <xsl:apply-templates select="key('ByModeAndFirst',concat(generate-id(..),'&',substring(name,1,1)))"/>
            </dd>
        </dl>
    </xsl:template>
    <xsl:template match="name">
        <p>
            <xsl:value-of select="."/>
        </p>
    </xsl:template>
</xsl:stylesheet>

输出:

<html>
    <body>
        <div>
            <dl>
                <dt>a</dt>
                <dd>
                    <p>abhdk</p>
                </dd>
            </dl>
            <dl>
                <dt>n</dt>
                <dd>
                    <p>njhjsu</p>
                </dd>
            </dl>
            <dl>
                <dt>s</dt>
                <dd>
                    <p>sdasd</p>
                </dd>
            </dl>
            <dl>
                <dt>v</dt>
                <dd>
                    <p>vbvbcd</p>
                </dd>
            </dl>
        </div>
        <div>
            <dl>
                <dt>a</dt>
                <dd>
                    <p>asdfd</p>
                </dd>
            </dl>
            <dl>
                <dt>v</dt>
                <dd>
                    <p>vcbbn</p>
                </dd>
            </dl>
            <dl>
                <dt>b</dt>
                <dd>
                    <p>bnmbnmb</p>
                </dd>
            </dl>
            <dl>
                <dt>t</dt>
                <dd>
                    <p>tyutyu</p>
                </dd>
            </dl>
            <dl>
                <dt>g</dt>
                <dd>
                    <p>ghjghj</p>
                </dd>
            </dl>
            <dl>
                <dt>h</dt>
                <dd>
                    <p>hjk</p>
                </dd>
            </dl>
        </div>
        <div>
            <dl>
                <dt>a</dt>
                <dd>
                    <p>asdfd</p>
                </dd>
            </dl>
            <dl>
                <dt>v</dt>
                <dd>
                    <p>vcbbn</p>
                </dd>
            </dl>
        </div>
    </body>
</html>

注意:现在,您的键涉及 mode 元素祖先,因此您需要将其引用到您的键中。

编辑:错过字母顺序。对不起。

With proper input:

<modes>
    <mode>
        <term>
            <name>abhdk</name>
        </term>
        <term>
            <name>njhjsu</name>
        </term>
        <term>
            <name>sdasd</name>
        </term>
        <term>
            <name>vbvbcd</name>
        </term>
    </mode>
    <mode>
        <term>
            <name>asdfd</name>
        </term>
        <term>
            <name>vcbbn</name>
        </term>
        <term>
            <name>bnmbnmb</name>
        </term>
        <term>
            <name>tyutyu</name>
        </term>
        <term>
            <name>ghjghj</name>
        </term>
        <term>
            <name>hjk</name>
        </term>
    </mode>
    <mode>
        <term>
            <name>asdfd</name>
        </term>
        <term>
            <name>vcbbn</name>
        </term>
    </mode>
</modes>

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="no" encoding="windows-1251"/>
    <xsl:key name="ByModeAndFirst" match="name" use="concat(generate-id(../..),'&',substring(.,1,1))"/>
    <xsl:template match="modes">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="mode">
        <div>
        <xsl:apply-templates>
            <xsl:sort select="name"/>
        </xsl:apply-templates>
        </div>
    </xsl:template>
    <xsl:template match="term"/>
    <xsl:template match="term[name[count(.|key('ByModeAndFirst',concat(generate-id(../..),'&',substring(.,1,1)))[1])=1]]">
        <dl>
            <dt>
                <xsl:value-of select="substring(name,1,1)"/>
            </dt>
            <dd>
                <xsl:apply-templates select="key('ByModeAndFirst',concat(generate-id(..),'&',substring(name,1,1)))"/>
            </dd>
        </dl>
    </xsl:template>
    <xsl:template match="name">
        <p>
            <xsl:value-of select="."/>
        </p>
    </xsl:template>
</xsl:stylesheet>

Output:

<html>
    <body>
        <div>
            <dl>
                <dt>a</dt>
                <dd>
                    <p>abhdk</p>
                </dd>
            </dl>
            <dl>
                <dt>n</dt>
                <dd>
                    <p>njhjsu</p>
                </dd>
            </dl>
            <dl>
                <dt>s</dt>
                <dd>
                    <p>sdasd</p>
                </dd>
            </dl>
            <dl>
                <dt>v</dt>
                <dd>
                    <p>vbvbcd</p>
                </dd>
            </dl>
        </div>
        <div>
            <dl>
                <dt>a</dt>
                <dd>
                    <p>asdfd</p>
                </dd>
            </dl>
            <dl>
                <dt>v</dt>
                <dd>
                    <p>vcbbn</p>
                </dd>
            </dl>
            <dl>
                <dt>b</dt>
                <dd>
                    <p>bnmbnmb</p>
                </dd>
            </dl>
            <dl>
                <dt>t</dt>
                <dd>
                    <p>tyutyu</p>
                </dd>
            </dl>
            <dl>
                <dt>g</dt>
                <dd>
                    <p>ghjghj</p>
                </dd>
            </dl>
            <dl>
                <dt>h</dt>
                <dd>
                    <p>hjk</p>
                </dd>
            </dl>
        </div>
        <div>
            <dl>
                <dt>a</dt>
                <dd>
                    <p>asdfd</p>
                </dd>
            </dl>
            <dl>
                <dt>v</dt>
                <dd>
                    <p>vcbbn</p>
                </dd>
            </dl>
        </div>
    </body>
</html>

Note: Now, your key involves the mode element ancestor, so you need to reference this into your key.

EDIT: Miss the alfabetic order. Sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文