1

ctrlname列を除いて、すべてが機能しました(empoに感謝)。構文がよくわかりません。私がやろうとしているのは、xsltを使用して、グリッドビューのxmlを列名で並べ替えることです。ctrlname列以外はすべて機能しています。XSLTに属性を渡すにはどうすればよいですか?私が試したのは、@ name、Data / @ name、Data [@name]、ctrlnameです。何も機能しません。

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

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:param name="sortby"></xsl:param>
    <xsl:param name="orderas"></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="*[name()=$sortby]" data-type="text" order="{$orderas}"/>
            </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入力

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

3 に答える 3

2

現在受け入れられている回答には 1 つの欠陥dataがあります。の子要素と同じ名前のの属性がある場合dataは常に、属性の値をキーとして使用して並べ替えが実行されます。また、長すぎます。

この解決策は問題を解決し(より短く)、並べ替えを属性名または要素名のどちらで行うかを指定できるようにします。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="sortby" select="'attrib!name'"/>
    <xsl:param name="orderas" select="'ascending'"/>
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="data">
                <xsl:sort select=
                "*[name()=substring-after($sortby, 'elem!')]
                |
                 @*[name()=substring-after($sortby, 'attrib!')]"
                data-type="text" order="{$orderas}"/>
            </xsl:apply-templates>
        </root>
    </xsl:template>
    <xsl:template match="data">
        <data ctrlname="{@name}" value="{value}"
              comment="{comment}"/>
    </xsl:template>
</xsl:stylesheet>

この XML ドキュメントに適用すると(提供されたものに基づいていますが、もう少し興味深いものになっています):

<root>
    <data name="Test3.Text" xml:space="preserve">
        <value>Please Pick Bare Pump</value>
        <comment>Tab - Pump Configuration</comment>
        <name>X</name>
    </data>
    <data name="Test2.Text" xml:space="preserve">
        <value>Complete</value>
        <comment>A07</comment>
        <name>Z</name>
    </data>
    <data name="Test1.Text" xml:space="preserve">
        <value>Confirmed</value>
        <comment>A01</comment>
        <name>Y</name>
    </data>
</root>

正しい結果 (name属性でソート) が生成されます。

<root>
   <data ctrlname="Test1.Text" value="Confirmed" comment="A01"/>
   <data ctrlname="Test2.Text" value="Complete" comment="A07"/>
   <data ctrlname="Test3.Text" value="Please Pick Bare Pump" comment="Tab - Pump Configuration"/>
</root>

<xsl:param name="sortby" select="'attrib!name'"/>を次のように置き換えます

<xsl:param name="sortby" select="'elem!name'"/>

同じ XML ドキュメントに変換を再度適用します。今回は、子要素 nameの値によって正しくソートされた結果が得られます。

<root>
   <data ctrlname="Test3.Text" value="Please Pick Bare Pump" comment="Tab - Pump Configuration"/>
   <data ctrlname="Test1.Text" value="Confirmed" comment="A01"/>
   <data ctrlname="Test2.Text" value="Complete" comment="A07"/>
</root>

説明:

  1. element-child でソートするか、属性でソートするかを区別するために、elem!someNameという名前の子要素の値でソートする必要があるという規則を使用しますsomeName。同様に、attrib!someNameは、 という名前の属性の値によって並べ替える必要があることを意味しますsomeName

  2. 命令は、属性または子要素の<xsl:sort>いずれかをキーとして正しく選択するように適宜変更されます。sortbyパラメータの開始部分文字列は、キーが属性であるか子要素であるかを一意に識別するため、あいまいさは許されません。

于 2011-05-19T04:13:41.217 に答える
1

はい、申し訳ありませんが、属性で並べ替えたいと思っていたことに気づきませんでした。xsl:paramの構文を変更したため、そのように正しくないことにも注意してください。単一引用符を二重引用符の中に入れることが非常に重要です。最終的なテンプレートは次のとおりです。

<?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" select="'value'"/>
        <xsl:param name="orderas" select="'ascending'"/>

    <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="*[name()=$sortby]|@*[name()=$sortby]" data-type="text" order="{$orderas}"/>
            </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>
于 2011-05-18T19:58:46.383 に答える
0

$sortby パラメータで属性名または要素名を指定できるので、これでうまくいくはずです。

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

($sortby パラメーターの値として「名前」を渡すだけです)

問題は、ソート値ノードの選択が一致する要素*のみ (要素のみに一致) であったことです。

于 2011-05-18T19:46:23.623 に答える