0

私はまだ Visual Basic と XML に慣れていないので、これが正しい言い回しであるかどうかわかりません。私は XSLT の表面をかじっただけですが、これまで見てきたことから、XSLT がこれを実行できるかどうかはわかりません。

Visual Basic で XML ファイルをタグのアルファベット順に再帰的に並べ替えようとしています。たとえば、次のような XML ドキュメントの場合:

<catalog>
  <game>
      <title>Role-playing EXTREME Adventure</title>
      <platform>PC</platform>
      <type>Action/Adventure</type>
      <price>60.00</price>
  </game>
  <cd>
      <title>Music Notes</title>
      <artist>Susan Dylan</artist>
      <genre>Rock</genre>
      <company>Columbia</company>
      <year>1995</year>
      <price>10.95</price>
  </cd>
  <book>
      <title>Pictures of Things</title>
      <author>Bob Smith</author>
      <type>paper-back</type>
      <price>5.99</price>
  </book>
</catalog>

...次のようになります:

<catalog>
  <book>
      <author>Bob Smith</author>
      <price>5.99</price>
      <title>Pictures of Things</title>
      <type>paper-back</type>
  </book>
  <cd>
      <artist>Susan Dylan</artist>
      <company>Columbia</company>
      <genre>Rock</genre>
      <price>10.95</price>
      <title>Music Notes</title>
      <year>1995</year>
  </cd>
  <game>
      <platform>PC</platform>
      <price>60.00</price>
      <title>Role-playing EXTREME Adventure</title>
      <type>Action/Adventure</type>
  </game>
</catalog>

このソートされたバージョンは、XML ファイルの内容を置き換えて保存する必要があります。これは、複数の長い XML ファイルに対して行われます。私は職場のコンピューターにいないので、いじっていた Visual Basic コードの混乱にアクセスできません。とにかく自分がしてきたことを捨てるべきだと思い始めています。

これがどこかで答えられたら、私はお詫び申し上げます。私は一日のほとんどをこれの検索と作業に費やしました。そのため、すでに回答がある場合は、その回答へのリンクをお待ちしています。:)

これを見ましが、私のニーズを満たしていないようです。これは、コマンド ラインではなく、Visual Basic 内から実行する必要があります。

助けていただければ幸いです。

4

2 に答える 2

1

XML の並べ替えに使用できるプログラムを次に示します (LINQ to XML と拡張メソッドを使用するには、少なくとも .NET 3.0 が必要ですが、これは古いスタイルの XML 解析ライブラリと通常のメソッドでも実現できます)。

Imports System.Xml.Linq
Imports System.Runtime.CompilerServices

Module Module1

Sub Main()
    Dim xe As XElement = XElement.Load("test.xml")
    Console.WriteLine("----Original XML----")
    Console.WriteLine(xe)
    xe.SortRecursive()
    Console.WriteLine("----Sorted XML----")
    Console.WriteLine(xe)
End Sub

''' <summary>
''' Sorts the subelements of an XML element, and their subelements recursively.
''' </summary>
''' <param name="xe"></param>
''' <remarks></remarks>
<Extension()>
Sub SortRecursive(xe As XElement)
    xe.Sort()
    For Each subelement As XElement In xe.Elements()
        subelement.SortRecursive()
    Next
End Sub

''' <summary>
''' Sorts the subelements of an XML element.
''' </summary>
''' <param name="xe">The element whose subelements are to be sorted.</param>
<Extension()>
Sub Sort(xe As XElement)
    ' Save off the subelements into a list, and remove them from the main element
    Dim subelements As New List(Of XElement)
    For Each subelement As XElement In xe.Elements().ToArray()
        subelement.Remove()
        subelements.Add(subelement)
    Next

    ' Sort the list of subelements
    subelements.Sort(AddressOf CompareElementNames)

    ' Add the elements back and return when done
    For Each subelement As XElement In subelements
        xe.Add(subelement)
    Next
End Sub

''' <summary>
''' Compares two XML elements by their names.
''' </summary>
''' <param name="e1">The first element.</param>
''' <param name="e2">The second element.</param>
''' <returns>positive/negative/zero depending on comparison (same as string comparison)</returns>
Function CompareElementNames(e1 As XElement, e2 As XElement) As Integer
    Return String.Compare(e1.Name.ToString(), e2.Name.ToString())
End Function
End Module
于 2012-08-13T23:33:32.713 に答える
0

これは私にとってはうまくいくようです:

Dim recurse As Action(Of XElement) = Nothing
recurse = _
    Sub (x)
        For Each e in x _
                .Elements() _
                .OrderBy(Function (y) y.Name.LocalName) _
                .ToArray()
            recurse(e)
            e.Remove()
            x.Add(e)
        Next
    End Sub

私はこのデータでテストしました:

Dim xml = _
    <catalog>
        <game>
            <title>Role-playing EXTREME Adventure</title>
            <platform>PC</platform>
            <type>Action/Adventure</type>
            <price>60.00</price>
        </game>
        <cd>
            <title>Music Notes</title>
            <artist>Susan Dylan</artist>
            <genre>Rock</genre>
            <company>Columbia</company>
            <year>1995</year>
            <price>10.95</price>
        </cd>
        <book>
            <title>Pictures of Things</title>
            <author>Bob Smith</author>
            <type>paper-back</type>
            <price>5.99</price>
        </book>
    </catalog>

次のように呼び出します。

recurse(xml)

そして、この結果を得ました:

<catalog>
  <book>
    <author>Bob Smith</author>
    <price>5.99</price>
    <title>Pictures of Things</title>
    <type>paper-back</type>
  </book>
  <cd>
    <artist>Susan Dylan</artist>
    <company>Columbia</company>
    <genre>Rock</genre>
    <price>10.95</price>
    <title>Music Notes</title>
    <year>1995</year>
  </cd>
  <game>
    <platform>PC</platform>
    <price>60.00</price>
    <title>Role-playing EXTREME Adventure</title>
    <type>Action/Adventure</type>
  </game>
</catalog>
于 2012-08-14T02:10:04.167 に答える