Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按属性值中的第一个单词对XML排序_Xml_Xslt - Fatal编程技术网

按属性值中的第一个单词对XML排序

按属性值中的第一个单词对XML排序,xml,xslt,Xml,Xslt,我有一个XML文件(下面是它的一部分),包含1000多行代码,如下所示 <?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"> <viewpoi

我有一个XML文件(下面是它的一部分),包含1000多行代码,如下所示

<?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>

我希望使用属性“name”对上面的代码进行排序,但我希望它使用属性值中的第一个单词进行排序。在上面的例子中,我希望使用PG/EL


注:请注意,我对XSLT或任何其他工具都不太熟悉,但我可能不需要太多解释就能理解。

您已经给出了一个示例,其中有两个元素需要排序,对于这两个元素,“第一个单词”可以作为
子字符串(@name,6,2)
或作为
子字符串之后(子字符串之前(@name,,),“”)
。如果这些表达式中的任何一个适用于所有数据,则将它们用作
xsl:sort
中的排序键。否则,你需要告诉我们

(a) 你说的“单词”到底是什么意思


(b) 您使用的XSLT的版本是什么(如果您可以使用XSLT2.0,任何更复杂的字符串操作都会变得非常简单)

不管怎样,我已经找到了答案。下面的XLST对XML进行排序

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
    <root>
       <xsl:apply-templates select="viewpoints/view">
          <xsl:sort select="substring-after(substring-before(@name, 'vs'), '.')" order="ascending"/>
       </xsl:apply-templates>
     </root>
 </xsl:template>
 <xsl:template match="view">
     <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

但此代码将删除“
”标记之前的所有标记,并将其替换为“
”。它编辑视图标记如下“
”。以下是输出

<?xml version="1.0"?>
<root>

    <view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>

<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>

</root>

如何纠正这一点


PS:请看上面的XML代码

我认为“后面的子字符串(前面的子字符串(@name,),”)适合我。我得试一下,但只是想确认一下,这对空格(“”)有用吗?i、 例如,(@name,”)这在探测空间方面有效吗?我认为你有责任更仔细地解释这些要求。你还没有说出“第一个词”的意思;我已经猜到了,而且几乎可以肯定我猜错了。事实上,你还没有回答我的任何一个问题。答:我说的第一个单词是指name属性中数字后面的单词(即002/004之后的单词),在本例中是PG/EL。b:我对XSLT非常陌生,我还没有为这个特定问题创建任何样式表。我真的不知道该怎么做。我需要对此做一点工作。要编写提取排序键的逻辑,我们需要知道name属性中可以出现什么类型的值。它总是三位数,然后是句号,然后是空格,然后是两个字母吗?如果不是,可能是什么?如果是别的,提取“第一个单词”的规则是什么?