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_Xslt 2.0 - Fatal编程技术网

如何使用按优先级排列的值列表选择XML的一部分

如何使用按优先级排列的值列表选择XML的一部分,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我有一个xml,其中包含几个类似数据的大型部分。我希望能够根据优先级列表选择一个节,以及该节是否存在 例如,如果部分A存在,则仅使用A,如果它不存在,则使用B,依此类推,但仅使用它找到的第一个部分,基于优先级,我设置的不是xml中的顺序 以下是迄今为止的xsl: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" x

我有一个xml,其中包含几个类似数据的大型部分。我希望能够根据优先级列表选择一个节,以及该节是否存在

例如,如果部分A存在,则仅使用A,如果它不存在,则使用B,依此类推,但仅使用它找到的第一个部分,基于优先级,我设置的不是xml中的顺序

以下是迄今为止的xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:template match="/">
        <xsl:apply-templates select="sources/source"></xsl:apply-templates>
    </xsl:template>

     <xsl:template match="source">
    <xsl:choose>
                <xsl:when test="@type='C' or @type='B' or @type='A'">
                <xsl:value-of select="name"/>
                <xsl:value-of select="age"/>
            </xsl:when>
        </xsl:choose>

    </xsl:template>


</xsl:stylesheet>

以下是xml示例:

<?xml version="1.0" encoding="UTF-8"?>
<sources>
    <source type='C'>
        <name>Joe</name>
        <age>10</age>
    </source>
    <source type='B'>
        <name>Mark</name>
        <age>20</age>
    </source>
    <source type='A'>
        <name>David</name>
        <age>30</age>
    </source>
</sources>

乔
10
做记号
20
大卫
30

所以我想说的是,C是我的第一选择,然后是B,然后是A。

只需按偏好顺序选择一个序列,然后使用序列中的第一项

XSLT2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:apply-templates select="(sources/source[@type='C'],
                                      sources/source[@type='B'],
                                      sources/source[@type='A'])[1]"/>
    </xsl:template>

    <xsl:template match="source">
        <xsl:value-of select="name,age" separator=" - "/>
    </xsl:template>

</xsl:stylesheet>
注意:我更改
源代码的覆盖仅用于演示目的

Joe - 10