按 xslt 中的属性分组

发布于 2024-10-14 03:21:39 字数 1328 浏览 2 评论 0原文

嗨,我有点陷入这个问题,我的 xml 是因为

<album>
     <title>album name</title>    
     <tracks>
           <track tracksequence="1" disksequence="1">track 1 - disc 1</track>
           <track tracksequence="2" disksequence="1">track 2 - disc 1</track>
           <track tracksequence="3" disksequence="1">track 3 - disc 1</track>
           <track tracksequence="1" disksequence="2">track 1 - disc 2</track>
           <track tracksequence="2" disksequence="2">track 2 - disc 2</track>
           <track tracksequence="3" disksequence="2">track 3 - disc 2</track>
           <track tracksequence="4" disksequence="2">track 4 - disc 2</track>
           <track tracksequence="1" disksequence="3">track 1 - disc 3</track>
           <track tracksequence="2" disksequence="3">track 2 - disc 3</track>
     </tracks>
</album>

我希望能够输出,因为

<div>
<span>album name disc 1</span>
track 1 disc 1
track 2 disc 1
track 3 disc 1
</div>

<div>
<span>album name disc 2</span>
track 1 disc 2
track 2 disc 2
track 3 disc 2
track 4 disc 2 
</div>

ect...

我确信我需要使用某种 Muenchian 分组,但我很难理解它以适应我的特定用途。

Hi i am a little stuck with this problem my xml is as

<album>
     <title>album name</title>    
     <tracks>
           <track tracksequence="1" disksequence="1">track 1 - disc 1</track>
           <track tracksequence="2" disksequence="1">track 2 - disc 1</track>
           <track tracksequence="3" disksequence="1">track 3 - disc 1</track>
           <track tracksequence="1" disksequence="2">track 1 - disc 2</track>
           <track tracksequence="2" disksequence="2">track 2 - disc 2</track>
           <track tracksequence="3" disksequence="2">track 3 - disc 2</track>
           <track tracksequence="4" disksequence="2">track 4 - disc 2</track>
           <track tracksequence="1" disksequence="3">track 1 - disc 3</track>
           <track tracksequence="2" disksequence="3">track 2 - disc 3</track>
     </tracks>
</album>

i would like to be able to output as

<div>
<span>album name disc 1</span>
track 1 disc 1
track 2 disc 1
track 3 disc 1
</div>

<div>
<span>album name disc 2</span>
track 1 disc 2
track 2 disc 2
track 3 disc 2
track 4 disc 2 
</div>

ect...

im sure that i need to use some sort of Muenchian grouping but im having difficulty getting my head around it for my particular usage.

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

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

发布评论

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

评论(1

差↓一点笑了 2024-10-21 03:21:39
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>

<xsl:key name="sortByDisk" match="track" use="@disksequence"/>

<xsl:template match="/*">
    <body>
        <xsl:apply-templates select="tracks/track[
            count(. | key('sortByDisk', @disksequence)[1]) = 1
        ]"/>
    </body>
</xsl:template>

<xsl:template match="track">
    <div>
        <span>
            <xsl:value-of select="../../title"/>
            <xsl:text> disc</xsl:text>
            <xsl:value-of select="@disksequence"/>
        </span>
        <xsl:apply-templates select="key('sortByDisk', @disksequence)"
            mode="inner"/>
    </div>
</xsl:template>

<xsl:template match="track" mode="inner">
    <p><xsl:value-of select="."/></p>
</xsl:template>

</xsl:stylesheet>

输出(使用您的示例):

<body>
    <div>
        <span>album name disc1</span>
        <p>track 1 - disc 1</p>
        <p>track 2 - disc 1</p>
        <p>track 3 - disc 1</p>
    </div>
    <div>
        <span>album name disc2</span>
        <p>track 1 - disc 2</p>
        <p>track 2 - disc 2</p>
        <p>track 3 - disc 2</p>
        <p>track 4 - disc 2</p>
    </div>
    <div>
        <span>album name disc3</span>
        <p>track 1 - disc 3</p>
        <p>track 2 - disc 3</p>
    </div>
</body>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>

<xsl:key name="sortByDisk" match="track" use="@disksequence"/>

<xsl:template match="/*">
    <body>
        <xsl:apply-templates select="tracks/track[
            count(. | key('sortByDisk', @disksequence)[1]) = 1
        ]"/>
    </body>
</xsl:template>

<xsl:template match="track">
    <div>
        <span>
            <xsl:value-of select="../../title"/>
            <xsl:text> disc</xsl:text>
            <xsl:value-of select="@disksequence"/>
        </span>
        <xsl:apply-templates select="key('sortByDisk', @disksequence)"
            mode="inner"/>
    </div>
</xsl:template>

<xsl:template match="track" mode="inner">
    <p><xsl:value-of select="."/></p>
</xsl:template>

</xsl:stylesheet>

Output (using your sample):

<body>
    <div>
        <span>album name disc1</span>
        <p>track 1 - disc 1</p>
        <p>track 2 - disc 1</p>
        <p>track 3 - disc 1</p>
    </div>
    <div>
        <span>album name disc2</span>
        <p>track 1 - disc 2</p>
        <p>track 2 - disc 2</p>
        <p>track 3 - disc 2</p>
        <p>track 4 - disc 2</p>
    </div>
    <div>
        <span>album name disc3</span>
        <p>track 1 - disc 3</p>
        <p>track 2 - disc 3</p>
    </div>
</body>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文