gpt4 book ai didi

xslt - 按xslt中的属性分组

转载 作者:行者123 更新时间:2023-12-03 17:19:44 26 4
gpt4 key购买 nike

嗨,我对此问题有些困惑,我的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分组,但是对于我的特殊用途,我难以理解。

最佳答案

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

关于xslt - 按xslt中的属性分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4781959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com