Это правильный способ сортировки с использованием XSLT? - PullRequest
1 голос
/ 04 ноября 2011

В основном происходит то, что сами узлы 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()

Ответы [ 2 ]

2 голосов
/ 04 ноября 2011

Есть две очевидные проблемы с предоставленным кодом :

  1. Единственный шаблон в коде соответствует root, но нет rootэлемент вообще в предоставленном элементе XML - поэтому этот шаблон вообще не выбран для выполнения.

  2. Элементы в предоставленном документе XML не имеют дочерних текстовых узлов.Так как в предоставленном коде XSLT нет другого шаблона, для обработки документа XML используются только встроенные шаблоны XSLT.Их эффект заключается в копировании на вывод только текстовых узлов - поэтому ничего не выводится в качестве вывода.

Решение :

  1. Измените match='root' на match='Root'.

  2. Добавьте правило идентификации, чтобы копировать каждый узел.

После применения этих двух исправлений к вашему коду он становится :

<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>
0 голосов
/ 04 ноября 2011

@ Димитр: Еще раз спасибо за помощь.У вас есть книга или что-то?Я бы купил это.Я должен был внести изменения в коде позади.Это изменение:

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