2009-09-14 199 views
9

是否有任何工具可以(漂亮地打印)格式化XML文件以及對其元素和屬性進行排序?XML排序/格式化工具

+0

重複:https://stackoverflow.com/q/9161934/492 ..但這裏的答案對我來說 – 2018-02-10 21:24:36

回答

3

我發現這個職位:http://www.biglist.com/lists/xsl-list/archives/200106/msg01225.html使用以下XSLT縮進XML並排序屬性:

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="*"> 
    <xsl:copy> 
     <!-- Sort the attributes by name. --> 
     <xsl:for-each select="@*"> 
     <xsl:sort select="name(.)"/> 
     <xsl:copy/> 
     </xsl:for-each> 
     <xsl:apply-templates/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="text()|comment()|processing-instruction()"> 
    <xsl:copy/> 
    </xsl:template> 

</xsl:stylesheet> 

我還沒有嘗試過,但最有可能我會堅持XSLT做格式化爲了我。

+0

更多有用這是偉大的,訂購xml屬性,我怎樣才能訂購一個標籤的孩子? – Natim 2016-04-04 12:13:32

7

我正在尋找一個類似的工具,並沒有真正找到我在找什麼,所以我只是幾乎沒有寫一個。這非常簡單(並且不包括節點排序中的屬性),但是起作用。

也許這對別人有用..它在GitHub

下面是從GitHub的頁面有點...

USAGE: sortxml.exe [options] infile [outfile] 

    infile  The name of the file to sort, etc. 
    outfile  The name of the file to save the output to. 
       If this is omitted, then the output is written to stdout. 

OPTIONS: 

    --pretty Ignores the input formatting and makes the output look nice. 
    --sort  Sort both the nodes and attributes. 
    --sortnode Sort the nodes. 
    --sortattr Sort the attributes. 

(prefix an option with ! to turn it off.) 

缺省設置爲輸出漂亮,分類節點和屬性。這裏有一個例子:

> type sample.xml 
<?xml version="1.0" encoding="utf-8" ?><root><node value="one" attr="name"/></root> 

> sortxml.exe sample.xml 
<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <node attr="name" value="one" /> 
</root> 
10

我喜歡這個工具:https://xmlsorter.codeplex.com/

您可以通過標籤名稱和屬性進行排序。在比較一些XML文件之前,我喜歡使用它。

XML Sorter main window

+0

這甚至不會加載XML文件。出現「無效的XML」錯誤。無法處理doctype標記。一旦刪除它工作。 – ScrappyDev 2017-01-26 17:00:59

+0

這對我來說可以快速測試一對超過7K行的文件,並且具有半複雜的xml結構,所以我最初留下了深刻的印象。根本沒有錯誤。平穩/快速下載,啓動和執行。有一點值得注意的是,它允許「排序屬性」和「排序特定屬性」,然後選擇你想要的。您可以檢查看起來重疊的兩個選項。 – 2017-03-23 22:30:50

+0

它是「排序屬性」和「按*特定屬性排序」,非常不同。第一個命令每行的屬性,第二行按特定屬性的內容排序所有行。後者是非常實際的,我認爲。 – Gertsen 2017-06-23 08:21:55

2

出與Visual Studio的挫折,這似乎重新排序&改寫EDMX-文件(實體框架)所有的時間(另見本Uservoice),我寫了一些Linqpad代碼重新排序的東西。然而,在LinqPad外部使用是很容易的(也是顯而易見的)。

它通過元素類型(標籤),然後通過元素屬性「名稱」的值,然後通過其他一些東西來嘗試使其具有確定性(不同的xml,但意義相同,通常是相同的輸出 - 見代碼)。

它還命令屬性。需要注意的是語義 XML的屬性可以沒有(相關的)命令,但文本上他們這樣做,和版本控制系統仍然認爲他們的純文本...

(請注意,它不能解決不同的別名,在Entity Framework edmx file regenerating differently amongst team提到)

void Main() 
{ 
    XDocument xdoc = XDocument.Load(@"\\filepath1\file1.edmx"); 

    var orderedElements = CopyAndSortElements(xdoc.Elements()); 

    var newDoc = new XDocument(); 
    newDoc.Add(orderedElements); 
    newDoc.Save(@"\\filepath1\file1.Ordered.edmx"); 
} 

public IEnumerable<XElement> CopyAndSortElements(IEnumerable<XElement> elements) 
{ 
    var newElements = new List<XElement>(); 
    // Sort XElements by Tag & name-attribute (and some other properties) 
    var orderedElements = elements.OrderBy(elem => elem.Name.LocalName) // element-tag 
            .ThenByDescending(elem => elem.Attributes("Name").Count()) // can be 0, more than 1 is invalid XML 
            .ThenBy(elem => (elem.Attributes("Name").Any() ? elem.Attributes("Name").First().Value.ToString() : string.Empty)) 
            // in case of no Name-Attributes, try to sort by (number of) children 
            .ThenBy(elem => elem.Elements().Count()) 
            .ThenBy(elem => elem.Attributes().Count()) 
            // next line may vary for textually different but semantically equal input when elem & attr were unordered on input, but I need to restrain myself... 
            .ThenBy(elem => elem.ToString()); 
    foreach (var oldElement in orderedElements) 
    { 
     var newElement = new XElement(oldElement.Name); 
     var orderedAttrs = oldElement.Attributes().OrderBy(attr => attr.Name.LocalName).ThenBy(attr => attr.Value.ToString()); 
     newElement.Add(orderedAttrs); 
     newElement.Add(CopyAndSortElements(oldElement.Elements())); 
     newElements.Add(newElement); 
    } 
    return newElements; 
} 

PS:我們結束了使用XSLT,這人在同一時間其他寫道。我認爲它在每個人的構建過程中都更容易/更好。 但也許/希望這對某人有用。

+0

你最終使用了什麼xslt? – 2015-09-18 14:02:49

+0

恩,看這個 - 它從類似節點'中刪除值' – 2018-02-10 20:58:45

0

我試圖弄清楚如何排序和edmx文件時,來到這篇文章。 我的解決方案是基於帕特保榮溶液中發現 https://stackoverflow.com/a/19324438/212241

void Main() 
{ 
    XDocument xdoc = XDocument.Load(@"C:\git\Nvision\Source\NvisionEntities\NvisionModel.edmx"); 
    Sort(xdoc.Root); 
    xdoc.Save(@"C:\git\Nvision\Source\NvisionEntities\NvisionModel.edmx"); 
} 

public void Sort(XElement source, bool bSortAttributes = true) 
{ 
    //Make sure there is a valid source 
    if (source == null) throw new ArgumentNullException("source"); 

    //Sort attributes if needed 
    if (bSortAttributes) 
    { 
     List<XAttribute> sortedAttributes = source.Attributes().OrderBy(a => a.ToString()).ToList(); 
     sortedAttributes.ForEach(a => a.Remove()); 
     sortedAttributes.ForEach(a => source.Add(a)); 
    } 

    //Sort the children IF any exist 
    List<XElement> sortedChildren = source.Elements().OrderBy(elem => elem.Attributes("Name").Any() ? elem.Attributes("Name").First().Value.ToString() : string.Empty).ToList(); 
    if (source.HasElements) 
    { 
     source.RemoveNodes(); 
     sortedChildren.ForEach(c => Sort(c)); 
     sortedChildren.ForEach(c => source.Add(c)); 
    } 
}