开发者

XSLT : Reorder attributes in a XML using XSLT

开发者 https://www.devze.com 2023-03-03 01:37 出处:网络
Is there a way of reordering attributes in a XML using XSLT? For example I have a XML like <?xml version=\"1.0\" encoding=\"UTF-8\"?>

Is there a way of reordering attributes in a XML using XSLT?

For example I have a XML like

<?xml version="1.0" encoding="UTF-8"?>
 <books>
    <book author="Anonymous" qty="10" price="100" title="Basic XML"/>
    <book author="Anonymous" qty="20" price="200" title="Basic XLST"/>
 </books>

After the transformation, i expect it to like:

<?xml version="1.0" encoding="UTF-8"?>
 <books>
    <boo开发者_StackOverflow中文版k title="Basic XML" author="Anonymous" price="100" qty="10"/>
    <book title="Basic XLST" author="Anonymous" price="200" qty="20"/>
 </books>

Thanks in advance.


There is no order of attributes in XML. The xml files you have shown are slightly different representations of exactly the same thing and anything that sees a difference between them is not (behaving as) an xml parser.

Honestly. A correct xml parser does not see any difference between the two.

There may be individual xslt implementations which can create this difference, I don't know. But in general, xslt only talks about xml, not about the textual representation of that xml, and since at that level, there is no difference, it is hardly surprising that there is no standard xslt way of achieving the result you are looking for.

What you can do, of course, is to take any xml parser/writer and change the writing part to emit the attributes in the order you want. Personally, I'd use something like ruby's rexml, simply because ruby allows redefining methods at runtime and without modifying the original code.


I think your xml should be something like this:

<books>
    <book>
        <author>Anonymous</author>
        <qty>10</qty>
        <price>100</price>
        <title>Basic XML</title>
    </book>
    <book>
        <author>Anonymous</author>
        <qty>20</qty>
        <price>200</price>
        <title>Basic XsLt</title>
    </book>
</books>

And then you can reorder as you wish. Here you'll find loads of info and samples.

0

精彩评论

暂无评论...
验证码 换一张
取 消