基本上发生的事情是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: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()
答案 0 :(得分:2)
提供的代码有两个明显的问题:
代码中唯一的模板匹配root
,但在提供的XML元素中根本没有root
元素 - 因此根本不会选择执行此模板
提供的XML文档中的元素没有文本节点子节点。由于提供的XSLT代码中没有任何其他模板,因此在处理XML文档时仅使用内置的XSLT模板。它们的作用是仅将文本节点复制到输出 - 因此不会产生任何输出。
<强>解决方案强>:
将match='root'
更改为match='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文档:
<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>
生成了想要的,正确的(已排序的)结果:
<?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>
答案 1 :(得分: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()
变量“markup”是一个XmlDocument。在写入XmlWriter之后,我发现我无法做markup.Save。它会再次以错误的顺序将数据写入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>