2011-11-03 84 views
1

基本上,發生的事情是XML節點本身沒有改變位置。所以我覺得我的XSLT錯過了一些東西。這是使用XSLT進行排序的正確方法嗎?

XSLT:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:asp="http://test.com/asp" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 

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

    <xsl:template match="root"> 
     <root> 
      <xsl:apply-templates select="asp:TableRow"> 
       <xsl:sort select="@ID" data-type="text" order="ascending"/> 
      </xsl:apply-templates> 
     </root> 
    </xsl:template> 
</xsl:stylesheet> 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<Root xmlns:asp="http://test.com/asp" xmlns:meta="http://test.com/meta" xmlns:cc1="http://test.com/cc1"> 
    <asp:TableRow ID="A02"> 
    <asp:TableCell> 
     <asp:Localize ID="tagthreeCtrlNumberRes" meta:resourcekey="tagthreeCtrlNumberRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagthreeCtrlDescRes" meta:resourcekey="tagthreeCtrlDescRes" runat="server" /> 
     <asp:Localize ID="tagthreeCtrlNoteRes" meta:resourcekey="tagthreeCtrlNoteRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:RadioButtonList ID="rblthreeCtrlRes0" RepeatDirection="Horizontal" runat="server"> 
     <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res0" Value="1" /> 
     <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res0" Value="0" /> 
     <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res0" Value="2" /> 
     </asp:RadioButtonList> 
     <asp:RadioButtonList ID="rblthreeCtrlRes1" RepeatDirection="Horizontal" runat="server"> 
     <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res1" Value="1" /> 
     <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res1" Value="0" /> 
     <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res1" Value="2" /> 
     </asp:RadioButtonList> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Button ID="cmdthreeCtrlRes" meta:resourcekey="cmdthreeCtrlRes" runat="server" OnClick="FormDataSave_Click" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblAssmthreeCtrlRes" meta:resourcekey="lblAssmthreeCtrlRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblQualthreeCtrlRes" meta:resourcekey="lblQualthreeCtrlRes" runat="server" /> 
     <asp:Button ID="cmdQualAcceptthreeCtrlRes" meta:resourcekey="cmdQualAcceptthreeCtrlRes" OnClick="cmdQualAccept_Click" runat="server" Text="Accept" Visible="True" /> 
    </asp:TableCell> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    </asp:TableRow> 
    <asp:TableRow ID="A01"> 
    <asp:TableCell> 
     <asp:Localize ID="tagtwoCtrlNumberRes" meta:resourcekey="tagtwoCtrlNumberRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagtwoCtrlDescRes" meta:resourcekey="tagtwoCtrlDescRes" runat="server" /> 
     <asp:Localize ID="tagtwoCtrlNoteRes" meta:resourcekey="tagtwoCtrlNoteRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagtxttwoCtrlRes0" meta:resourcekey="tagtxttwoCtrlRes0" runat="server" /> 
     <asp:Textbox ID="txttwoCtrlRes0" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Button ID="cmdtwoCtrlRes" meta:resourcekey="cmdtwoCtrlRes" runat="server" OnClick="FormDataSave_Click" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblAssmtwoCtrlRes" meta:resourcekey="lblAssmtwoCtrlRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    </asp:TableRow> 
    <asp:TableRow ID="A03"> 
    <asp:TableCell> 
     <asp:Localize ID="tagoneCtrlNumberRes" meta:resourcekey="tagoneCtrlNumberRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagoneCtrlDescRes" meta:resourcekey="tagoneCtrlDescRes" runat="server" /> 
     <asp:Localize ID="tagoneCtrlNoteRes" meta:resourcekey="tagoneCtrlNoteRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagtxtoneCtrlRes0" meta:resourcekey="tagtxtoneCtrlRes0" runat="server" /> 
     <asp:Textbox ID="txtoneCtrlRes0" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Button ID="cmdoneCtrlRes" meta:resourcekey="cmdoneCtrlRes" runat="server" OnClick="FormDataSave_Click" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblAssmoneCtrlRes" meta:resourcekey="lblAssmoneCtrlRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    </asp:TableRow> 
</Root> 

代碼隱藏:

For Each deleteChild As XmlNode In rootDoc.DocumentElement.ChildNodes 
    rootDoc.DocumentElement.RemoveChild(deleteChild) 
Next 

'List (Of XMLNode)' 
For Each markupElem As XmlElement In allNodes 
    rootDoc.DocumentElement.AppendChild(markupElem) 
Next 

'List (Of XMLNode)' 
For Each markupElem As XmlElement In nodesToMove 
    rootDoc.DocumentElement.AppendChild(markupElem) 
Next 

Dim sortXmlDoc As Xsl.XslCompiledTransform = New Xsl.XslCompiledTransform(True) 
sortXmlDoc.Load(xsltPath) 

Dim saveWriter As XmlTextWriter = New XmlTextWriter(truePath, Nothing) 
sortXmlDoc.Transform(rootDoc, saveWriter) 
saveWriter.Close() 

回答

2

有與所提供的代碼兩個明顯的問題:

  1. 代碼中唯一的模板與root匹配,但在提供的XML元素中根本沒有root元素 - 因此該模板根本沒有被選中執行。

  2. 提供的XML文檔中的元素沒有文本節點子元素。由於提供的XSLT代碼中沒有任何其他模板,因此在處理XML文檔時僅使用內置的XSLT模板。它們的效果是僅將文本節點複製到輸出 - 因此沒有產生任何輸出。

解決方案

  1. 變化match='root'match='Root'

  2. 添加標識規則,以便每個節點都將被複制。

已經應用這兩個修正你的代碼變得

<Root xmlns:asp="http://test.com/asp" xmlns:meta="http://test.com/meta" xmlns:cc1="http://test.com/cc1"> 
    <asp:TableRow ID="A02"> 
    <asp:TableCell> 
     <asp:Localize ID="tagthreeCtrlNumberRes" meta:resourcekey="tagthreeCtrlNumberRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagthreeCtrlDescRes" meta:resourcekey="tagthreeCtrlDescRes" runat="server" /> 
     <asp:Localize ID="tagthreeCtrlNoteRes" meta:resourcekey="tagthreeCtrlNoteRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:RadioButtonList ID="rblthreeCtrlRes0" RepeatDirection="Horizontal" runat="server"> 
     <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res0" Value="1" /> 
     <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res0" Value="0" /> 
     <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res0" Value="2" /> 
     </asp:RadioButtonList> 
     <asp:RadioButtonList ID="rblthreeCtrlRes1" RepeatDirection="Horizontal" runat="server"> 
     <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res1" Value="1" /> 
     <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res1" Value="0" /> 
     <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res1" Value="2" /> 
     </asp:RadioButtonList> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Button ID="cmdthreeCtrlRes" meta:resourcekey="cmdthreeCtrlRes" runat="server" OnClick="FormDataSave_Click" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblAssmthreeCtrlRes" meta:resourcekey="lblAssmthreeCtrlRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblQualthreeCtrlRes" meta:resourcekey="lblQualthreeCtrlRes" runat="server" /> 
     <asp:Button ID="cmdQualAcceptthreeCtrlRes" meta:resourcekey="cmdQualAcceptthreeCtrlRes" OnClick="cmdQualAccept_Click" runat="server" Text="Accept" Visible="True" /> 
    </asp:TableCell> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    </asp:TableRow> 
    <asp:TableRow ID="A01"> 
    <asp:TableCell> 
     <asp:Localize ID="tagtwoCtrlNumberRes" meta:resourcekey="tagtwoCtrlNumberRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagtwoCtrlDescRes" meta:resourcekey="tagtwoCtrlDescRes" runat="server" /> 
     <asp:Localize ID="tagtwoCtrlNoteRes" meta:resourcekey="tagtwoCtrlNoteRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagtxttwoCtrlRes0" meta:resourcekey="tagtxttwoCtrlRes0" runat="server" /> 
     <asp:Textbox ID="txttwoCtrlRes0" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Button ID="cmdtwoCtrlRes" meta:resourcekey="cmdtwoCtrlRes" runat="server" OnClick="FormDataSave_Click" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblAssmtwoCtrlRes" meta:resourcekey="lblAssmtwoCtrlRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    </asp:TableRow> 
    <asp:TableRow ID="A03"> 
    <asp:TableCell> 
     <asp:Localize ID="tagoneCtrlNumberRes" meta:resourcekey="tagoneCtrlNumberRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagoneCtrlDescRes" meta:resourcekey="tagoneCtrlDescRes" runat="server" /> 
     <asp:Localize ID="tagoneCtrlNoteRes" meta:resourcekey="tagoneCtrlNoteRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagtxtoneCtrlRes0" meta:resourcekey="tagtxtoneCtrlRes0" runat="server" /> 
     <asp:Textbox ID="txtoneCtrlRes0" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Button ID="cmdoneCtrlRes" meta:resourcekey="cmdoneCtrlRes" runat="server" OnClick="FormDataSave_Click" /> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblAssmoneCtrlRes" meta:resourcekey="lblAssmoneCtrlRes" runat="server" /> 
    </asp:TableCell> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    <asp:TableCell /> 
    </asp:TableRow> 
</Root> 

想要的,正確的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:asp="http://test.com/asp" 
    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()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Root"> 
     <root> 
      <xsl:apply-templates select="asp:TableRow"> 
       <xsl:sort select="@ID" data-type="text" order="ascending"/> 
      </xsl:apply-templates> 
     </root> 
    </xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔應用(排序)結果產生

<?xml version="1.0" encoding="UTF-16"?> 
<root xmlns:asp="http://test.com/asp"> 
<asp:TableRow ID="A01" xmlns:meta="http://test.com/meta" xmlns:cc1="http://test.com/cc1"> 
    <asp:TableCell> 
     <asp:Localize ID="tagtwoCtrlNumberRes" meta:resourcekey="tagtwoCtrlNumberRes" runat="server"></asp:Localize> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagtwoCtrlDescRes" meta:resourcekey="tagtwoCtrlDescRes" runat="server"></asp:Localize> 
     <asp:Localize ID="tagtwoCtrlNoteRes" meta:resourcekey="tagtwoCtrlNoteRes" runat="server"></asp:Localize> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagtxttwoCtrlRes0" meta:resourcekey="tagtxttwoCtrlRes0" runat="server"></asp:Localize> 
     <asp:Textbox ID="txttwoCtrlRes0" runat="server"></asp:Textbox> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Button ID="cmdtwoCtrlRes" meta:resourcekey="cmdtwoCtrlRes" runat="server" OnClick="FormDataSave_Click"></asp:Button> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblAssmtwoCtrlRes" meta:resourcekey="lblAssmtwoCtrlRes" runat="server"></asp:Localize> 
    </asp:TableCell> 
    <asp:TableCell></asp:TableCell> 
    <asp:TableCell></asp:TableCell> 
    <asp:TableCell></asp:TableCell> 
    </asp:TableRow> 
<asp:TableRow ID="A02" xmlns:meta="http://test.com/meta" xmlns:cc1="http://test.com/cc1"> 
    <asp:TableCell> 
     <asp:Localize ID="tagthreeCtrlNumberRes" meta:resourcekey="tagthreeCtrlNumberRes" runat="server"></asp:Localize> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagthreeCtrlDescRes" meta:resourcekey="tagthreeCtrlDescRes" runat="server"></asp:Localize> 
     <asp:Localize ID="tagthreeCtrlNoteRes" meta:resourcekey="tagthreeCtrlNoteRes" runat="server"></asp:Localize> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:RadioButtonList ID="rblthreeCtrlRes0" RepeatDirection="Horizontal" runat="server"> 
     <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res0" Value="1"></asp:ListItem> 
     <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res0" Value="0"></asp:ListItem> 
     <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res0" Value="2"></asp:ListItem> 
     </asp:RadioButtonList> 
     <asp:RadioButtonList ID="rblthreeCtrlRes1" RepeatDirection="Horizontal" runat="server"> 
     <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res1" Value="1"></asp:ListItem> 
     <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res1" Value="0"></asp:ListItem> 
     <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res1" Value="2"></asp:ListItem> 
     </asp:RadioButtonList> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Button ID="cmdthreeCtrlRes" meta:resourcekey="cmdthreeCtrlRes" runat="server" OnClick="FormDataSave_Click"></asp:Button> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblAssmthreeCtrlRes" meta:resourcekey="lblAssmthreeCtrlRes" runat="server"></asp:Localize> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblQualthreeCtrlRes" meta:resourcekey="lblQualthreeCtrlRes" runat="server"></asp:Localize> 
     <asp:Button ID="cmdQualAcceptthreeCtrlRes" meta:resourcekey="cmdQualAcceptthreeCtrlRes" OnClick="cmdQualAccept_Click" runat="server" Text="Accept" Visible="True"></asp:Button> 
    </asp:TableCell> 
    <asp:TableCell></asp:TableCell> 
    <asp:TableCell></asp:TableCell> 
    </asp:TableRow> 
<asp:TableRow ID="A03" xmlns:meta="http://test.com/meta" xmlns:cc1="http://test.com/cc1"> 
    <asp:TableCell> 
     <asp:Localize ID="tagoneCtrlNumberRes" meta:resourcekey="tagoneCtrlNumberRes" runat="server"></asp:Localize> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagoneCtrlDescRes" meta:resourcekey="tagoneCtrlDescRes" runat="server"></asp:Localize> 
     <asp:Localize ID="tagoneCtrlNoteRes" meta:resourcekey="tagoneCtrlNoteRes" runat="server"></asp:Localize> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="tagtxtoneCtrlRes0" meta:resourcekey="tagtxtoneCtrlRes0" runat="server"></asp:Localize> 
     <asp:Textbox ID="txtoneCtrlRes0" runat="server"></asp:Textbox> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Button ID="cmdoneCtrlRes" meta:resourcekey="cmdoneCtrlRes" runat="server" OnClick="FormDataSave_Click"></asp:Button> 
    </asp:TableCell> 
    <asp:TableCell> 
     <asp:Localize ID="lblAssmoneCtrlRes" meta:resourcekey="lblAssmoneCtrlRes" runat="server"></asp:Localize> 
    </asp:TableCell> 
    <asp:TableCell></asp:TableCell> 
    <asp:TableCell></asp:TableCell> 
    <asp:TableCell></asp:TableCell> 
    </asp:TableRow> 
</root> 
0

@Dimitre:再次感謝你,幫助我。你有書還是什麼?我會買它。我必須對代碼隱藏進行更改。這是變化:

Dim xsltPath As String = Server.MapPath(xsltFileName3) 

Dim sortXmlDoc As Xsl.XslCompiledTransform = New Xsl.XslCompiledTransform(True) 
sortXmlDoc.Load(xsltPath) 
Dim output = sortXmlDoc.OutputSettings 

Dim saveWriter As XmlWriter = XmlWriter.Create(truePath, output) 
sortXmlDoc.Transform(markup, saveWriter) 
saveWriter.Close() 

變量「標記」是一個XmlDocument。在寫入XmlWriter後,我發現我無法做標記。保存。它會將數據再次寫入XML文件,並且順序也是錯誤的。由於代碼隱藏的行爲方式,我對XSLT做了一些小改動。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:asp="http://test.com/asp" 
    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()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Root"> 
     <Root xmlns:asp="http://test.com/asp" xmlns:meta="http://test.com/meta" xmlns:cc1="http://test.com/cc1"> 
      <xsl:apply-templates select="asp:TableRow"> 
       <xsl:sort select="@ID" data-type="text" order="ascending"/> 
      </xsl:apply-templates> 
     </Root> 
    </xsl:template> 

</xsl:stylesheet> 
+0

@ _dotnetN00b:不客氣。 (我有我在這裏的XSLT/XPAth書的建議:http://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589 –

相關問題