是否有任何工具可以(漂亮地打印)格式化XML文件以及對其元素和屬性進行排序?XML排序/格式化工具
回答
我發現這個職位: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做格式化爲了我。
更多有用這是偉大的,訂購xml屬性,我怎樣才能訂購一個標籤的孩子? – Natim 2016-04-04 12:13:32
我正在尋找一個類似的工具,並沒有真正找到我在找什麼,所以我只是幾乎沒有寫一個。這非常簡單(並且不包括節點排序中的屬性),但是起作用。
也許這對別人有用..它在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>
我喜歡這個工具:https://xmlsorter.codeplex.com/
您可以通過標籤名稱和屬性進行排序。在比較一些XML文件之前,我喜歡使用它。
這甚至不會加載XML文件。出現「無效的XML」錯誤。無法處理doctype標記。一旦刪除它工作。 – ScrappyDev 2017-01-26 17:00:59
這對我來說可以快速測試一對超過7K行的文件,並且具有半複雜的xml結構,所以我最初留下了深刻的印象。根本沒有錯誤。平穩/快速下載,啓動和執行。有一點值得注意的是,它允許「排序屬性」和「排序特定屬性」,然後選擇你想要的。您可以檢查看起來重疊的兩個選項。 – 2017-03-23 22:30:50
它是「排序屬性」和「按*特定屬性排序」,非常不同。第一個命令每行的屬性,第二行按特定屬性的內容排序所有行。後者是非常實際的,我認爲。 – Gertsen 2017-06-23 08:21:55
出與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,這人在同一時間其他寫道。我認爲它在每個人的構建過程中都更容易/更好。 但也許/希望這對某人有用。
你最終使用了什麼xslt? – 2015-09-18 14:02:49
恩,看這個 - 它從類似節點'
我試圖弄清楚如何排序和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));
}
}
- 1. 排序不動,格式化
- 2. C代碼格式化/美化工具
- 3. 格式化XML
- 4. Mac上的免費XML格式化(縮進)工具?
- 5. 重新格式化xml註釋的工具(Visual Studio 2008)
- 6. OSX的源格式化工具?
- 7. 格式化工具崇高文本
- 8. 用於CSS格式化的工具
- 9. 在Zedgraph上格式化工具提示
- 10. 免費SQL格式化工具
- 11. 格式化餅圖工具提示
- 12. VBscript自動格式化工具
- 13. XML/XSL表格格式化
- 14. XML可視化工具?
- 15. 排序和格式化日期角
- 16. XML反序列化的DateTime格式
- 17. Highcharts:動態更改工具提示格式化程序?
- 18. jqGrid工具欄過濾與自定義格式化程序
- 19. PHP - 格式化XML輸出
- 20. 格式化XML響應VB.NET
- 21. XML生成未格式化
- 22. 使用xmltextwriter格式化xml
- 23. 如何格式化xml?
- 24. XSLT格式化XML文檔
- 25. 以XML格式化數據
- 26. 使用XmlDocument格式化XML
- 27. Perl格式化輸出xml
- 28. 在Emacs中格式化XML
- 29. 代碼排序工具.NET
- 30. WPF工具包Datagrid排序
重複:https://stackoverflow.com/q/9161934/492 ..但這裏的答案對我來說 – 2018-02-10 21:24:36