排序xml并输出xml [英] Sort xml and output xml
问题描述
我有这个xml文件。我想对它进行排序,并创建一个新的xml文件。
结果应该与输入相同,除了它已经排序。
<?xml version =" 1.0" encoding =" UTF-8"?>
< levelone>
< child ID =" 1" sort =" 5">< name> Paul< / name>< / child>
< child ID =" 2" sort =" 1">< name> Adam< / name>< / child>
< child ID =" 3" sort =" 2">< name>将< / name>< / child>
< root>
我现在想要应用xslt文件并具有以下输出:
<?xml version =" 1.0" encoding =" UTF-8"?>
< levelone>
< child ID =" 2" sort =" 1">< name> Adam< / name>< / child>
< child ID =" 3" sort =" 2">< name>将< / name>< / child>
< child ID =" 1" sort =" 5">< name> Paul< / name>< / child>
< root>
我已经我一直在搜索几个小时,但我看到的所有例子都创建了一个
的HTML文件。
tia
/ jim
Jim Andersen写道:
我有这个XML文件。我想对它进行排序,并创建一个
的新xml文件。结果应该与
输入相同,除了它已经排序。
既然你没有提到你用什么来处理
,我认为它符合XSLT 1.0标准
处理器。
<?xml version =" 1.0" encoding =" UTF-8"?>
< levelone>
< child ID =" 1" sort =" 5">< name> Paul< / name>< / child>
< child ID =" 2" sort =" 1">< name> Adam< / name>< / child>
< child ID =" 3" sort =" 2">< name>将< / name>< / child>
< root>
这不是XML文件。它的格式不是很好。
我一直在寻找几个小时,但是我所看到的所有例子我都看到了一个HTML文件。
这很奇怪,特别是因为XML是
默认输出方法。我记得的所有XSLT教程
涵盖排序,身份转换和控制
你的输出。
假设你的意思是:
< levelone>
< child ID =" 1" sort =" 5">< name> Paul< / name>< / child>
< child ID =" 2" sort =" 1">< name> Adam< / name>< / child>
< child ID =" 3" sort =" 2">< name>将< / name>< / child>
< / levelone>
以下XSLT应该做的诀窍:
< xsl:stylesheet version =" 1.0"
xmlns:xsl =" http://www.w3 .org / 1999 / XSL / Transform">
< xsl:template match =" node()| @ *">
< xsl:复制>
< xsl:apply-templates select =" node()| @ *" />
< / xsl:copy>
< / xsl:template>
< xsl:template
match =" * [@ sort] [not(preceding-sibling :: * [@ sort])]">
< xsl:apply-templates select =" ../* [@ sort]" mode =" copy">
< xsl:sort select =" @ sort" />
< / xsl:apply-templates>
< / xsl:template>
< xsl:template
match =" * [@ sort] [preceding-sibling :: * [@sort]]" />
< xsl:template match =" node()| @ *" mode =" copy">
< xsl:copy>
< xsl:apply-templates select =" node()| @ *" / >
< / xsl:copy>
< / xsl:template>
< / xsl:stylesheet>
-
Pavel Lepin
p。***** @ ctncorp.com 写道:
既然你没提到你的话''正在使用
处理
我正在使用.NET 2.0 XMLDataSource。我把它指向我的xml文件和我的
xslt文件。
>,我认为它是一些XSLT 1.0 -compliant
processor。
><?xml version =" 1.0" encoding =" UTF-8"?>
< levelone>
< child ID =" 1" sort =" 5">< name> Paul< / name>< / child>
< child ID =" 2" sort =" 1">< name> Adam< / name>< / child>
< child ID =" 3" sort =" 2">< name>将< / name>< / child>
< root>
那不是XML文件。它的形式并不完善。
是的。我发帖中的拼写错误。最后一个元素root应该是
levelone
>我去过搜索几个小时,但我看到的所有示例都创建了一个HTML文件。
这很奇怪,特别是因为XML是
默认输出方法。
现在我发现了原因。我(从我发现的一个例子中)学到了
,我还可以在XML文档中放一条指令,说明总是使用特定的XSL文件。类似于:< xsl-stylesheet
output =" htm / txt">
删除该行使输出成为XML。
假设你的意思是:
< levelone>
< child ID =" 1" ; sort =" 5">< name> Paul< / name>< / child>
< child ID =" 2" sort =" 1">< name> Adam< / name>< / child>
< child ID =" 3" sort =" 2">< name>将< / name>< / child>
< / levelone>
我做了。
以下XSLT应该可以解决这个问题:
它确实如此。堆一堆。现在我可以开始找出原因:-)
/ jim
这应该是微不足道的。例如,请参阅 http://www.w3.org/TR/xslt #sorting
除非你告诉它输出HTML,否则XSLT不会输出HTML ...
>
I have this xml-file. I want to sort it, and create a new xml-file. The
result should be identical to the input except that it''s sorted.
<?xml version="1.0" encoding="UTF-8"?>
<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
<root>
I now want to apply an xslt file and have the following output:
<?xml version="1.0" encoding="UTF-8"?>
<levelone>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
<child ID="1" sort="5"><name>Paul</name></child>
<root>
I''ve been searching for hours, but all the examples I have seen creates an
HTML file.
tia
/jim
Jim Andersen wrote:I have this xml-file. I want to sort it, and create a
new xml-file. The result should be identical to the
input except that it''s sorted.Since you failed to mention what you''re using for
processing, I assume it''s some XSLT 1.0-compliant
processor.
<?xml version="1.0" encoding="UTF-8"?>
<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
<root>That''s not an XML file. It''s not well-formed.
I''ve been searching for hours, but all the examples I
have seen creates an HTML file.That''s mighty strange, especially since XML is the
default output method. All the XSLT tutorials I remember
covered sorting, identity transformation and controlling
your output.
Presuming you meant something like:
<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
</levelone>
The following XSLT should do the trick:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template
match="*[@sort][not(preceding-sibling::*[@sort])]">
<xsl:apply-templates select="../*[@sort]" mode="copy">
<xsl:sort select="@sort"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template
match="*[@sort][preceding-sibling::*[@sort]]"/>
<xsl:template match="node()|@*" mode="copy">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
--
Pavel Lepin
p.*****@ctncorp.com wrote:Since you failed to mention what you''re using for
processingI''m using a .NET 2.0 XMLDataSource. I point it to my xml-file and my
xslt-file.
>, I assume it''s some XSLT 1.0-compliant
processor.
><?xml version="1.0" encoding="UTF-8"?>
<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
<root>
That''s not an XML file. It''s not well-formed.Yes. A typo in my posting. The last element "root" should have been
"levelone"
>I''ve been searching for hours, but all the examples I
have seen creates an HTML file.
That''s mighty strange, especially since XML is the
default output method.And now I found out why. I had (from one of the examples I found) learned
that I could also put an instruction in the XML document that said to always
use a specific XSL-file. Something along the lines of : <xsl-stylesheet
output="htm/txt">
Removing that line made the output into XML.
Presuming you meant something like:
<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
</levelone>I did.
The following XSLT should do the trick:And it did. Thx a heap. Now I can start to find out why :-)
/jim
This should be trivial. See, for example, http://www.w3.org/TR/xslt#sorting
XSLT doesn''t output HTML unless you tell it to output HTML...
这篇关于排序xml并输出xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!