Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

xslt - Sort XML by the first word in attribute value

I have an XML file(below is a part of it) with more than 1000 lines of code like below

<?xml version="1.0" encoding="UTF-8" ?>

<exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<viewpoints>    
    <view name="002. PG vs MD - Overview">
            <viewpoint tool="none" render="shaded" lighting="headlight" focal="30.3740807783">
                <camera projection="persp" near="1.0000000000" far="10.0000000000" aspect="1.4551804424" height="0.7853981634">
                    <position>
                        <pos3f x="14.2834498406" y="-183.6741132934" z="35.6151508952"/>
                    </position>
                    <rotation>
                        <quaternion a="0.3979643079" b="0.0563656552" c="0.1284089725" d="0.9066192466"/>
                    </rotation>
                </camera>
            </viewpoint>
        </view>

        <view name="004. EL vs MD - Overview">
            <viewpoint tool="none" render="shaded" lighting="headlight" focal="30.3740807783">
                <camera projection="persp" near="1.0000000000" far="10.0000000000" aspect="1.4551804424" height="0.7853981634">
                    <position>
                        <pos3f x="14.2834498406" y="-183.6741132934" z="35.6151508952"/>
                    </position>
                    <rotation>
                        <quaternion a="0.3979643079" b="0.0563656552" c="0.1284089725" d="0.9066192466"/>
                    </rotation>
                </camera>
            </viewpoint>
        </view>
    </viewpoints>
</exchange>

I would like to sort the code above using the attribute "name" but I would like it to sort using the first word in the attribute value. In the above example, I would like it sore using PG/EL.

P.S: Please note that I'm not very familiar with XSLT or any other tools, but I can probably understand it with little explanation.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You've given an example with two elements to be sorted, and for both elements, the "first word" can be obtained as substring(@name, 6, 2) or as substring-after(substring-before(@name, ' '), ' '). If either of these expressions work for all your data, then use them as the sort key in xsl:sort. Otherwise, you need to tell us

(a) what exactly you mean by a "word"

(b) what version of XSLT you are using (any more complex string manipulation is going to be an awful lot easier if you can use XSLT 2.0)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...