XML节点排序的一种方法
前几天遇到了将XML按节点属性排序后输出的需求,开始想用C#写一个WinFrom应用程序实现,后来没怎么弄明白,后来看网上说XSL有排序的功能,就试了试,结果真的满足需求了,方法还很简单。
思路是用XSL将现有的XML文档转换为成排序后的格式,然后将结果另存为新的XML。
排序前的XML:
下面是XSL的代码:
test.xsl
将XML和XSL放在一个文件夹里,然后在XML的第二行加上一句:
这样XML就会按照XSL里的约束显示了,打开XML文件(我用的是IE,6、7皆可)。
下面就是排序后的XML:
另存为一个新的XML,排序就完成了。
思路是用XSL将现有的XML文档转换为成排序后的格式,然后将结果另存为新的XML。
排序前的XML:
<?
xml version="1.0" encoding="utf-8"
?>
< xxmx >
< options id ="2" >
< option lable ="请选择
"
value
=""
/>
< option lable ="或有负债" value ="8" />
< option lable ="长期负债" value ="7" />
< option lable ="流动负债" value ="6" />
< option lable ="无形动产" value ="5" />
< option lable ="无形不动产" value ="4" />
< option lable ="不动产" value ="3" />
< option lable ="有形动产" value ="2" />
< option lable ="财务资产" value ="1" />
</ options >
< options id ="10" >
< option lable ="请选择
"
value
=""
/>
< option lable ="文件" value ="9" />
< option lable ="自定义选择" value ="8" />
< option lable ="从库中选择" value ="7" />
< option lable ="密码域" value ="6" />
< option lable ="文本区域" value ="5" />
< option lable ="单选域" value ="4" />
< option lable ="单选域(手工)" value ="41" />
< option lable ="复选域" value ="3" />
< option lable ="选择域" value ="2" />
< option lable ="文本字段" value ="1" />
< option lable ="隐藏域" value ="99" />
< option lable ="从库中复选域" value ="32" />
< option lable ="自定复选域" value ="31" />
< option lable ="选择域(可选)" value ="21" />
< option lable ="选择域(手工)" value ="22" />
</ options >
</ xxmx >
< xxmx >
< options id ="2" >
< option lable ="请选择
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
< option lable ="或有负债" value ="8" />
< option lable ="长期负债" value ="7" />
< option lable ="流动负债" value ="6" />
< option lable ="无形动产" value ="5" />
< option lable ="无形不动产" value ="4" />
< option lable ="不动产" value ="3" />
< option lable ="有形动产" value ="2" />
< option lable ="财务资产" value ="1" />
</ options >
< options id ="10" >
< option lable ="请选择
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
< option lable ="文件" value ="9" />
< option lable ="自定义选择" value ="8" />
< option lable ="从库中选择" value ="7" />
< option lable ="密码域" value ="6" />
< option lable ="文本区域" value ="5" />
< option lable ="单选域" value ="4" />
< option lable ="单选域(手工)" value ="41" />
< option lable ="复选域" value ="3" />
< option lable ="选择域" value ="2" />
< option lable ="文本字段" value ="1" />
< option lable ="隐藏域" value ="99" />
< option lable ="从库中复选域" value ="32" />
< option lable ="自定复选域" value ="31" />
< option lable ="选择域(可选)" value ="21" />
< option lable ="选择域(手工)" value ="22" />
</ options >
</ xxmx >
下面是XSL的代码:
test.xsl
<?
xml version="1.0"
?>
< xsl:stylesheet xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" version ="1.0" >
< xsl:template match ="/" >
< html >
< title >
排序后文件
</ title >
< body >
<![CDATA[ <?xml version="1.0" encoding="utf-8"?> ]]>
< br />
<![CDATA[ <xxmx> ]]>
< br />
< xsl:for-each select ="/xxmx/options" >
<![CDATA[ <options id=" ]]> < xsl:value-of select ="@id" /> <![CDATA[ "> ]]> < br />
< xsl:for-each select ="option" >
< xsl:sort data-type ="number" order ="ascending" select ="@value" />
<![CDATA[ <option lable =" ]]> < xsl:value-of select ="@lable" /> <![CDATA[ " value =" ]]> < xsl:value-of select ="@value" /> <![CDATA[ "/> ]]>
< br />
</ xsl:for-each >
<![CDATA[ </options> ]]>
< br />
</ xsl:for-each >
<![CDATA[ </xxmx> ]]>
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
< xsl:stylesheet xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" version ="1.0" >
< xsl:template match ="/" >
< html >
< title >
排序后文件
</ title >
< body >
<![CDATA[ <?xml version="1.0" encoding="utf-8"?> ]]>
< br />
<![CDATA[ <xxmx> ]]>
< br />
< xsl:for-each select ="/xxmx/options" >
<![CDATA[ <options id=" ]]> < xsl:value-of select ="@id" /> <![CDATA[ "> ]]> < br />
< xsl:for-each select ="option" >
< xsl:sort data-type ="number" order ="ascending" select ="@value" />
<![CDATA[ <option lable =" ]]> < xsl:value-of select ="@lable" /> <![CDATA[ " value =" ]]> < xsl:value-of select ="@value" /> <![CDATA[ "/> ]]>
< br />
</ xsl:for-each >
<![CDATA[ </options> ]]>
< br />
</ xsl:for-each >
<![CDATA[ </xxmx> ]]>
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
将XML和XSL放在一个文件夹里,然后在XML的第二行加上一句:
<?
xml-stylesheet type="text/xsl" href="test.xsl"
?>
这样XML就会按照XSL里的约束显示了,打开XML文件(我用的是IE,6、7皆可)。
下面就是排序后的XML:
<?
xml version="1.0" encoding="utf-8"
?>
< xxmx >
< options id ="2" >
< option lable ="请选择
"
value
=""
/>
< option lable ="财务资产" value ="1" />
< option lable ="有形动产" value ="2" />
< option lable ="不动产" value ="3" />
< option lable ="无形不动产" value ="4" />
< option lable ="无形动产" value ="5" />
< option lable ="流动负债" value ="6" />
< option lable ="长期负债" value ="7" />
< option lable ="或有负债" value ="8" />
</ options >
< options id ="10" >
< option lable ="请选择
"
value
=""
/>
< option lable ="文本字段" value ="1" />
< option lable ="选择域" value ="2" />
< option lable ="复选域" value ="3" />
< option lable ="单选域" value ="4" />
< option lable ="文本区域" value ="5" />
< option lable ="密码域" value ="6" />
< option lable ="从库中选择" value ="7" />
< option lable ="自定义选择" value ="8" />
< option lable ="文件" value ="9" />
< option lable ="选择域(可选)" value ="21" />
< option lable ="选择域(手工)" value ="22" />
< option lable ="自定复选域" value ="31" />
< option lable ="从库中复选域" value ="32" />
< option lable ="单选域(手工)" value ="41" />
< option lable ="隐藏域" value ="99" />
</ options >
</ xxmx >
< xxmx >
< options id ="2" >
< option lable ="请选择
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
< option lable ="财务资产" value ="1" />
< option lable ="有形动产" value ="2" />
< option lable ="不动产" value ="3" />
< option lable ="无形不动产" value ="4" />
< option lable ="无形动产" value ="5" />
< option lable ="流动负债" value ="6" />
< option lable ="长期负债" value ="7" />
< option lable ="或有负债" value ="8" />
</ options >
< options id ="10" >
< option lable ="请选择
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
< option lable ="文本字段" value ="1" />
< option lable ="选择域" value ="2" />
< option lable ="复选域" value ="3" />
< option lable ="单选域" value ="4" />
< option lable ="文本区域" value ="5" />
< option lable ="密码域" value ="6" />
< option lable ="从库中选择" value ="7" />
< option lable ="自定义选择" value ="8" />
< option lable ="文件" value ="9" />
< option lable ="选择域(可选)" value ="21" />
< option lable ="选择域(手工)" value ="22" />
< option lable ="自定复选域" value ="31" />
< option lable ="从库中复选域" value ="32" />
< option lable ="单选域(手工)" value ="41" />
< option lable ="隐藏域" value ="99" />
</ options >
</ xxmx >
另存为一个新的XML,排序就完成了。