gpt4 book ai didi

c# - 在 XSLT 1.0 和分组方面需要帮助

转载 作者:太空宇宙 更新时间:2023-11-03 20:42:03 25 4
gpt4 key购买 nike

我有以下 XML 文件:

<Promotions>
<Promotion>
<Category>Arts &amp; Entertainment</Category>
<Client>Client Five</Client>
<Title>Get your Free 2</Title>
</Promotion>
<Promotion>
<Category>Arts &amp; Entertainment</Category>
<Client>Client 5</Client>
<Title>Get your Free 4</Title>
</Promotion>
<Promotion>
<Category>Arts &amp; Entertainment</Category>
<Client>Client five</Client>
<Title>Get your Free 5</Title>
</Promotion>
<Promotion>
<Category>Community &amp; Neighborhood</Category>
<Client>Client 1</Client>
<Title>Get your Free 1</Title>
</Promotion>
<Promotion>
<Category>Education</Category>
<Client>Client 3</Client>
<Title>Get Your Free 3</Title>
</Promotion>

我想按类别分组。我尝试了以下并不断收到错误:

string  xslmarkup = @"
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='html' />

<xsl:key name='Categories' match='Promotions/Promotion' use='Category'/>

<xsl:template match='/'>
<xsl:apply-templates select='
/Promotions/Promotion[
generate-id()
=
generate-id(key ('Categories',Category)[1])
]
'/>
</xsl:template>

<xsl:template match='Promotion'>
<xsl:value-of select='Title'/>
</xsl:template>
</xsl:stylesheet>
"

我想要这样的输出:

    <h1>Arts &amp; Entertainment</h1>
<ul>Client Five</ul>
<ul>Get your Free 2</ul>

<ul>Client 5</ul>
<ul>Get your Free 4</ul>

<ul>Client five</ul>
<ul>Get your Free 5</ul>

<h1>Community &amp; Neighborhood</h1>
<ul>Client 1</ul>
<ul>Get your Free 1</ul>

<h1>Education</h1>
<ul>Client 3</ul>
<ul>Get Your Free 3</ul>

最佳答案

我认为错误是你的引用,但逻辑​​似乎也有缺陷。这不是一个很好的解决方案,但它应该能让您走上正轨。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:key name="categories" match="Category" use="." />
<xsl:template match="/">
<xsl:for-each select="/Promotions/Promotion/Category[
generate-id(.) = generate-id(key('categories', .)[1])
]">
<xsl:variable name="cname" select="." />
<Category title="{.}">
<xsl:for-each select="/Promotions/Promotion[Category=$cname]">
<Title>
<xsl:value-of select="Title" />
</Title>
</xsl:for-each>
</Category>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

给你这个:

<Category title="Arts &amp; Entertainment">
<Title>Get your Free 2</Title>
<Title>Get your Free 4</Title>
<Title>Get your Free 5</Title>
</Category>
<Category title="Community &amp; Neighborhood">
<Title>Get your Free 1</Title>
</Category>
<Category title="Education">
<Title>Get Your Free 3</Title>
</Category>

关于c# - 在 XSLT 1.0 和分组方面需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2176450/

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