Asp.net 使用XSLT对asp:listview数据源xml进行自定义分组和排序
我有一个listview,它获取一个xmldatasource作为其数据源。xmldatasource是动态创建和分配的。在将数据绑定到listview之前使用xpathAsp.net 使用XSLT对asp:listview数据源xml进行自定义分组和排序,asp.net,xml,xslt,Asp.net,Xml,Xslt,我有一个listview,它获取一个xmldatasource作为其数据源。xmldatasource是动态创建和分配的。在将数据绑定到listview之前使用xpath listview.datasource = myxmlsource listview.DataBind() 使用xpath=“root/node”只选择node元素。因此,我认为应用xpath后的xml结构如下所示: <node name="Albert" age="32" desc="some random des
listview.datasource = myxmlsource
listview.DataBind()
使用xpath=“root/node”
只选择node元素。因此,我认为应用xpath后的xml结构如下所示:
<node name="Albert" age="32" desc="some random description" region="north"/>
<node name="Randy" age="32" desc="some random description" region="south"/>
<node name="Zebra" age="32" desc="some random description"region="east"/>
<node name="Bob" age="32" desc="some random description"region="south"/>
<node name="Carl" age="32" desc="some random description"region="north"/>
<node name="Denver" age="32" desc="some random description"region="east"/>
请注意,它没有根。我正在尝试对这个XSLT进行排序
这里的目的是按区域对xml进行分组,然后按区域对xml进行排序
名字
我对XSLT还不熟悉,它似乎是一个怪兽
到目前为止,我能想到的xslt代码是:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl my" xmlns:my="http://tempuri.org"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template name="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node() >
<xsl:sort select="@region" order="ascending"/>
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
您的XSLT几乎正确-模板应该具有match=“…”
属性,而不是name=“…”
属性-以下是工作版本:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl my" xmlns:my="http://tempuri.org"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:sort select="@region" order="ascending"/>
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="node[@region='north']" >
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
<xsl:copy>
<xsl:apply-templates select="node[@region='east']" >
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
<xsl:copy>
<xsl:apply-templates select="node[@region='south']" >
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl my" xmlns:my="http://tempuri.org"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:sort select="@region" order="ascending"/>
<xsl:sort select="@name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>