每组问题的xslt

每组问题的xslt,xslt,Xslt,我需要按类别对xml进行分组 我试着用 <xsl:for-each-group select="ItemList/Item" group-by="CategoryID"> 如何获取CategoryName、CategoryID或任何与类别相关的信息 在转型期间 原始XML <ItemList> <Item> <CategoryID>1</CategoryID> <CategoryName>Book&

我需要按类别对xml进行分组 我试着用

<xsl:for-each-group  select="ItemList/Item"  group-by="CategoryID">

如何获取CategoryName、CategoryID或任何与类别相关的信息 在转型期间

原始XML

<ItemList>
<Item>
    <CategoryID>1</CategoryID>
    <CategoryName>Book</CategoryName>
    <ItemName>ASP.NET</ItemName>
    <ItemDetails>ASP.NET12345</ItemDetails>
</Item>
<Item>
    <CategoryID>1</CategoryID>
    <CategoryName>Book</CategoryName>
    <ItemName>PHP</ItemName>
    <ItemDetails>PHP12345</ItemDetails>
</Item>
<Item>
    <CategoryID>2</CategoryID>
    <CategoryName>Tool</CategoryName>
    <ItemName>ToolAbcde</ItemName>
    <ItemDetails>sth details</ItemDetails>
</Item></ItemList>

1.
书
ASP.NET
ASP.NET12345
1.
书
PHP
PHP12345
2.
工具
工具ABCDE
某物细节
新XML

<NewXML>
<Category>
    <CategoryID>1</CategoryID>
    <CategoryName>Book</CategoryName>
    <ItemList>
        <Item>
            <ItemName>ASP.NET</ItemName>
            <ItemDetails>ASP.NET12345</ItemDetails>
        </Item>
        <Item>
            <ItemName>PHP</ItemName>
            <ItemDetails>PHP12345</ItemDetails>
        </Item>
        <ItemName>PHP</ItemName>
    </ItemList>
</Category>
<Category>
    <CategoryID>2</CategoryID>
    <CategoryName>Tool</CategoryName>
    <ItemList>
        <Item>
            <ItemName>ToolAbcde</ItemName>
            <ItemDetails>sth details</ItemDetails>
        </Item>
    </ItemList>
</Category></NewXML>

1.
书
ASP.NET
ASP.NET12345
PHP
PHP12345
PHP
2.
工具
工具ABCDE
某物细节

xslt规范在分组方面有广泛的双重含义,并在中提供了示例。您可能正在查找
current-group()
current-grouping-key()
函数。也

在序列构造函数中,上下文项是相关组的初始项,上下文位置是该项在按组处理顺序排列的初始项序列(每组一项)中的位置,上下文大小是组数,当前组是正在处理的组,当前分组键是该组的分组键

这意味着这两个表达式在xsl:for-each-group中是等效的:
CategoryId
current-group()[1]/CategoryId
。由于CategoryId是分组键,因此这与
current-grouping-key()
相同。以下是您的用例的完整示例:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output method="xml" indent="yes" encoding="utf-8" />

    <xsl:template match="/">
        <NewXml>
            <xsl:for-each-group select="ItemList/Item" group-by="CategoryID">
                <Category>
                    <CategoryID><xsl:value-of select="CategoryID" /></CategoryID>
                    <CategoryName><xsl:value-of select="CategoryName" /></CategoryName>
                    <ItemList>
                        <xsl:for-each select="current-group()">
                            <Item>
                                <ItemName><xsl:value-of select="ItemName" /></ItemName>
                                <ItemDetails><xsl:value-of select="ItemDetails" /></ItemDetails>
                            </Item>
                        </xsl:for-each>
                    </ItemList>
                </Category>
            </xsl:for-each-group>
        </NewXml>
    </xsl:template>

</xsl:stylesheet>

xslt规范在分组方面有广泛的双重含义,并在中提供了示例。您可能正在查找
current-group()
current-grouping-key()
函数。也

在序列构造函数中,上下文项是相关组的初始项,上下文位置是该项在按组处理顺序排列的初始项序列(每组一项)中的位置,上下文大小是组数,当前组是正在处理的组,当前分组键是该组的分组键

这意味着这两个表达式在xsl:for-each-group中是等效的:
CategoryId
current-group()[1]/CategoryId
。由于CategoryId是分组键,因此这与
current-grouping-key()
相同。以下是您的用例的完整示例:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output method="xml" indent="yes" encoding="utf-8" />

    <xsl:template match="/">
        <NewXml>
            <xsl:for-each-group select="ItemList/Item" group-by="CategoryID">
                <Category>
                    <CategoryID><xsl:value-of select="CategoryID" /></CategoryID>
                    <CategoryName><xsl:value-of select="CategoryName" /></CategoryName>
                    <ItemList>
                        <xsl:for-each select="current-group()">
                            <Item>
                                <ItemName><xsl:value-of select="ItemName" /></ItemName>
                                <ItemDetails><xsl:value-of select="ItemDetails" /></ItemDetails>
                            </Item>
                        </xsl:for-each>
                    </ItemList>
                </Category>
            </xsl:for-each-group>
        </NewXml>
    </xsl:template>

</xsl:stylesheet>


这对于每个组元素都是新的吗?C#XslCompiledTransform不允许我compile@Trio张。这不是新的,但它是xslt2.0,很遗憾,它没有得到C的本地支持,这对于每个组元素来说都是新的吗?C#XslCompiledTransform不允许我compile@Trio张。这不是新的,但它是xslt2.0,令人遗憾的是C本机不支持它#