如何正确地将 XSLT 参数传递给 XmlDataSource?

发布于 2024-11-08 16:47:04 字数 4280 浏览 0 评论 0原文

我相信我的所有代码都是正确的,但我无法让它工作。 GridView 具有allowSorting = true。因此理论上,当我单击列标题时,gridview 中的 xml 应按该列排序。 XML 显示在 GridView 中,但根本不排序。我很困惑。

DST_Test.Xml

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <data name="Test1.Text" xml:space="preserve">
        <value>Please Pick Bare Pump</value>
        <comment>Tab - Pump Configuration</comment>
    </data>
    <data name="Test2.Text" xml:space="preserve">
        <value>Complete</value>
        <comment>A07</comment>
    </data>
    <data name="Test3.Text" xml:space="preserve">
        <value>Confirmed</value>
        <comment>A01</comment>
    </data>
</root>

DataSrcTransform.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">

    <xsl:param name="sortby"></xsl:param>

    <xsl:output method="xml" indent="yes"/>

    <!--<xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>-->
    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="data">
                <xsl:sort select="$sortby"/>
            </xsl:apply-templates>
        </root>
    </xsl:template>
    <xsl:template match="data">
        <data>
            <xsl:attribute name="ctrlname">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <xsl:attribute name="value">
                <xsl:value-of select="value" />
            </xsl:attribute>
            <xsl:attribute name="comment">
                <xsl:value-of select="comment" />
            </xsl:attribute>
        </data>
    </xsl:template>
</xsl:stylesheet>

代码隐藏

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        If Not IsPostBack Then
            XmlDataSource1.DataFile = "~/App_LocalResources/DST_Test.xml"
            XmlDataSource1.XPath = "//data"
            XmlDataSource1.TransformFile = xsltFileName
            GridView1.DataSource = XmlDataSource1
            GridView1.DataBind()
        End If
End Sub

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles GridView1.Sorting

        Select Case e.SortExpression

            Case "ctrlname"
                sortAttr = "@name"
            Case "value"
                sortAttr = "value"
            Case "comment"
                sortAttr = "comment"
        End Select

        Dim xslTrnsform As System.Xml.Xsl.XsltArgumentList = New System.Xml.Xsl.XsltArgumentList
        xslTrnsform.AddParam("sortby", "", sortAttr)
        XmlDataSource1.EnableCaching = False
        XmlDataSource1.TransformArgumentList = xslTrnsform
        XmlDataSource1.DataFile = "~/App_LocalResources/DST_Test.xml"
        XmlDataSource1.XPath = "//data"
        XmlDataSource1.TransformFile = xsltFileName
        GridView1.DataSource = XmlDataSource1
        GridView1.DataBind()
End Sub

HTML

<div>
            <asp:GridView ID="GridView1" runat="server"  AllowPaging="True" AllowSorting="True" 
                PageSize="25"
                AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="ctrlname" HeaderText="ctrlname" 
                        SortExpression="ctrlname" />
                    <asp:BoundField DataField="value" HeaderText="value" SortExpression="value" />
                    <asp:BoundField DataField="comment" HeaderText="comment" 
                        SortExpression="comment" />
                </Columns>
            </asp:GridView>
</div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server">            
</asp:XmlDataSource>

I believe I have all the code correct but I can't get it to work. The GridView has allowSorting = true. So in theory, when I click on the column header the xml in the gridview should sort by that column. The XML shows in the GridView, but doesn't sort at all. I'm stumped.

DST_Test.Xml

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <data name="Test1.Text" xml:space="preserve">
        <value>Please Pick Bare Pump</value>
        <comment>Tab - Pump Configuration</comment>
    </data>
    <data name="Test2.Text" xml:space="preserve">
        <value>Complete</value>
        <comment>A07</comment>
    </data>
    <data name="Test3.Text" xml:space="preserve">
        <value>Confirmed</value>
        <comment>A01</comment>
    </data>
</root>

DataSrcTransform.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">

    <xsl:param name="sortby"></xsl:param>

    <xsl:output method="xml" indent="yes"/>

    <!--<xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>-->
    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="data">
                <xsl:sort select="$sortby"/>
            </xsl:apply-templates>
        </root>
    </xsl:template>
    <xsl:template match="data">
        <data>
            <xsl:attribute name="ctrlname">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <xsl:attribute name="value">
                <xsl:value-of select="value" />
            </xsl:attribute>
            <xsl:attribute name="comment">
                <xsl:value-of select="comment" />
            </xsl:attribute>
        </data>
    </xsl:template>
</xsl:stylesheet>

Code-behind

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        If Not IsPostBack Then
            XmlDataSource1.DataFile = "~/App_LocalResources/DST_Test.xml"
            XmlDataSource1.XPath = "//data"
            XmlDataSource1.TransformFile = xsltFileName
            GridView1.DataSource = XmlDataSource1
            GridView1.DataBind()
        End If
End Sub

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles GridView1.Sorting

        Select Case e.SortExpression

            Case "ctrlname"
                sortAttr = "@name"
            Case "value"
                sortAttr = "value"
            Case "comment"
                sortAttr = "comment"
        End Select

        Dim xslTrnsform As System.Xml.Xsl.XsltArgumentList = New System.Xml.Xsl.XsltArgumentList
        xslTrnsform.AddParam("sortby", "", sortAttr)
        XmlDataSource1.EnableCaching = False
        XmlDataSource1.TransformArgumentList = xslTrnsform
        XmlDataSource1.DataFile = "~/App_LocalResources/DST_Test.xml"
        XmlDataSource1.XPath = "//data"
        XmlDataSource1.TransformFile = xsltFileName
        GridView1.DataSource = XmlDataSource1
        GridView1.DataBind()
End Sub

HTML

<div>
            <asp:GridView ID="GridView1" runat="server"  AllowPaging="True" AllowSorting="True" 
                PageSize="25"
                AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="ctrlname" HeaderText="ctrlname" 
                        SortExpression="ctrlname" />
                    <asp:BoundField DataField="value" HeaderText="value" SortExpression="value" />
                    <asp:BoundField DataField="comment" HeaderText="comment" 
                        SortExpression="comment" />
                </Columns>
            </asp:GridView>
</div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server">            
</asp:XmlDataSource>

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

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

发布评论

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

评论(1

七堇年 2024-11-15 16:47:04

我不知道其他方面,但我确信您的 XSL 存在一些问题,无法正确排序数据。尝试使用此 XSL(默认按注释排序):

<?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:param name="sortby" select="'comment'"/>

    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="data">
                <xsl:sort select="*[name()=$sortby]" order="ascending"/>
            </xsl:apply-templates>
        </root>
    </xsl:template>

    <xsl:template match="data">
        <data>
            <xsl:attribute name="ctrlname">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <xsl:attribute name="value">
                <xsl:value-of select="value" />
            </xsl:attribute>
            <xsl:attribute name="comment">
                <xsl:value-of select="comment" />
            </xsl:attribute>
        </data>
    </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:param name="sortby" select="'comment'"/>
  <xsl:param name="order" select="'ascending'"/>

    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="data">
                <xsl:sort select="*[name()=$sortby]" order="{$order}"/>
            </xsl:apply-templates>
        </root>
    </xsl:template>

    <xsl:template match="data">
        <data>
            <xsl:attribute name="ctrlname">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <xsl:attribute name="value">
                <xsl:value-of select="value" />
            </xsl:attribute>
            <xsl:attribute name="comment">
                <xsl:value-of select="comment" />
            </xsl:attribute>
        </data>
    </xsl:template>
</xsl:stylesheet>

说明:

  • *[name()=$sortby] 选择其名称的所有节点后代等于我们的参数$sortby
  • order="{$order}" 是用来使用参数设置order属性的值。值可以是升序降序

请注意,默认情况下,假设 text 数据类型执行排序。

I don't know about other sides, but I'm sure you have some problem in your XSL which does not properly sort data. Try using this XSL (default sort by comment):

<?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:param name="sortby" select="'comment'"/>

    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="data">
                <xsl:sort select="*[name()=$sortby]" order="ascending"/>
            </xsl:apply-templates>
        </root>
    </xsl:template>

    <xsl:template match="data">
        <data>
            <xsl:attribute name="ctrlname">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <xsl:attribute name="value">
                <xsl:value-of select="value" />
            </xsl:attribute>
            <xsl:attribute name="comment">
                <xsl:value-of select="comment" />
            </xsl:attribute>
        </data>
    </xsl:template>
</xsl:stylesheet>

Edited now to include the sort order as a parameter (default ascending):

<?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:param name="sortby" select="'comment'"/>
  <xsl:param name="order" select="'ascending'"/>

    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="data">
                <xsl:sort select="*[name()=$sortby]" order="{$order}"/>
            </xsl:apply-templates>
        </root>
    </xsl:template>

    <xsl:template match="data">
        <data>
            <xsl:attribute name="ctrlname">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <xsl:attribute name="value">
                <xsl:value-of select="value" />
            </xsl:attribute>
            <xsl:attribute name="comment">
                <xsl:value-of select="comment" />
            </xsl:attribute>
        </data>
    </xsl:template>
</xsl:stylesheet>

Explanation:

  • *[name()=$sortby] selects all nodes descendant whose name is equal to our parameter $sortby
  • order="{$order}" is used to set the value of the order attribute using the parameter. Value can be ascending or descending.

Note that sorting is performed by default assuming text data type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文