Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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

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
使用xslt根据子值获取xml节点_Xml_Xslt - Fatal编程技术网

使用xslt根据子值获取xml节点

使用xslt根据子值获取xml节点,xml,xslt,Xml,Xslt,我有一个如下所示的XML <BackgroundReports> <BackgroundReportPackage> <Screenings> <Screening type="PEMP"> <ScreeningStatus> <OrderStatus>Completed</OrderStatus&g

我有一个如下所示的XML

<BackgroundReports>
    <BackgroundReportPackage>
        <Screenings>
            <Screening type="PEMP">
                <ScreeningStatus>
                    <OrderStatus>Completed</OrderStatus>
                    <ResultStatus>Review</ResultStatus>
                </ScreeningStatus>
            </Screening>
            <Screening type="PEMP">
                <ScreeningStatus>
                    <OrderStatus>Completed</OrderStatus>
                    <ResultStatus>Review</ResultStatus>
                </ScreeningStatus>
            </Screening>
            <Screening type="FEMP">
                <ScreeningStatus>
                    <OrderStatus>Completed</OrderStatus>
                    <ResultStatus>Fail</ResultStatus>
                </ScreeningStatus>
            </Screening>
            <Screening type="PEMP">
                <ScreeningStatus>
                    <OrderStatus>Completed</OrderStatus>
                    <ResultStatus>Pass</ResultStatus>
                </ScreeningStatus>
            </Screening>
            <Screening type="TEST">
                <ScreeningStatus>
                    <OrderStatus>Completed</OrderStatus>
                    <ResultStatus>Review</ResultStatus>
                </ScreeningStatus>
            </Screening>
        </Screenings>
    </BackgroundReportPackage>
</BackgroundReports>
它从原始XML中删除了3个FEMP/PEMP筛选元素

第二个优先事项是审查,如果没有失败-审查,但如果有多个/所有审查要素,则只获得一个要素

第三,如果没有审查和失败,则通过,但如果多个/所有筛选元素通过,则仅获得一个元素

此外,还应列出除PEMP和FEMP之外的其他筛选元素

这是我迄今为止工作的xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:ns0="http://www.cpscreen.com/schemas"
    xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
    xmlns:my="http://www.fadv.com" exclude-result-prefixes="my fn xs xdt">
    <xsl:output method="xml" version="1.0" encoding="UTF-8"
        indent="yes" />

   <xsl:template match="node()|@*">
        <!-- Copy the current node -->
        <xsl:copy>
            <!-- Including any attributes it has and any child nodes -->
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/ns0:BackgroundReports/ns0:BackgroundReportPackage/ns0:Screenings">
        <test><xsl:value-of select="count(ns0:Screening)"/></test>
        <xsl:variable name="eduResultStatus"/>
        <xsl:for-each select="ns0:Screening[@type='PEMP' or @type='FEMP']">
            <!-- Yet to work here -->
        </xsl:for-each>

        <xsl:for-each select="ns0:Screening[@type != 'PEMP' and @type != 'FEMP']">
            <xsl:copy>
                <!-- Including any attributes it has and any child nodes -->
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

因为您使用的是XSLT2.0,所以可以声明排序顺序并拆分值

<xsl:param name="sortOrder" select="'Fail,Review,Pass'" />
<xsl:variable name="valSequence" select="tokenize($sortOrder, ',')"/>
对于其余节点,按原样应用它们

<xsl:apply-templates select="Screening[not(@type = 'FEMP' or @type = 'PEMP')]" />
完整的XSLT如下所示

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

    <xsl:param name="sortOrder" select="'Fail,Review,Pass'" />
    <xsl:variable name="valSequence" select="tokenize($sortOrder, ',')"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Screenings">
        <xsl:copy>
            <xsl:for-each select="Screening[@type = 'FEMP' or @type = 'PEMP']">
                <xsl:sort select="index-of($valSequence, ScreeningStatus/ResultStatus)" />
                <!-- Get 1st element after sorting -->
                <xsl:if test="position() = 1">
                    <xsl:copy>
                        <xsl:apply-templates select="@* | node()" />
                    </xsl:copy>
                </xsl:if>
            </xsl:for-each>
            <xsl:apply-templates select="Screening[not(@type = 'FEMP' or @type = 'PEMP')]" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
输出

<BackgroundReports>
   <BackgroundReportPackage>
      <Screenings>
         <Screening type="FEMP">
            <ScreeningStatus>
               <OrderStatus>Completed</OrderStatus>
               <ResultStatus>Fail</ResultStatus>
            </ScreeningStatus>
         </Screening>
         <Screening type="TEST">
            <ScreeningStatus>
               <OrderStatus>Completed</OrderStatus>
               <ResultStatus>Review</ResultStatus>
            </ScreeningStatus>
         </Screening>
      </Screenings>
   </BackgroundReportPackage>
</BackgroundReports>

您能分享到目前为止编写的XSLT代码吗?如果您还可以根据上述优先级添加所需的输出,这将非常有用。您想让所有的测试都先失败,然后复查,并在输出XML的末尾通过测试吗。@AniketVUpdated@AniketV是的,首先失败,然后检查下一步并在输出xml结束时通过。我只需要一个筛选元素用于PEMP和FEMPDo。你的意思是你想从每个结果状态中筛选一个?不,排序后我只需要一个元素,它可以是FEMP或PEMP,具体取决于种类。我从您的xslt中得到了结果。只需拉动排序结果的第一个筛选元素,在这种情况下,它将拉动FEMP或PEMP的第一个筛选。Sort无法获取appliedI需要排序然后获取第一个元素
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />

    <xsl:param name="sortOrder" select="'Fail,Review,Pass'" />
    <xsl:variable name="valSequence" select="tokenize($sortOrder, ',')"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Screenings">
        <xsl:copy>
            <xsl:for-each select="Screening[@type = 'FEMP' or @type = 'PEMP']">
                <xsl:sort select="index-of($valSequence, ScreeningStatus/ResultStatus)" />
                <!-- Get 1st element after sorting -->
                <xsl:if test="position() = 1">
                    <xsl:copy>
                        <xsl:apply-templates select="@* | node()" />
                    </xsl:copy>
                </xsl:if>
            </xsl:for-each>
            <xsl:apply-templates select="Screening[not(@type = 'FEMP' or @type = 'PEMP')]" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
<BackgroundReports>
   <BackgroundReportPackage>
      <Screenings>
         <Screening type="FEMP">
            <ScreeningStatus>
               <OrderStatus>Completed</OrderStatus>
               <ResultStatus>Fail</ResultStatus>
            </ScreeningStatus>
         </Screening>
         <Screening type="TEST">
            <ScreeningStatus>
               <OrderStatus>Completed</OrderStatus>
               <ResultStatus>Review</ResultStatus>
            </ScreeningStatus>
         </Screening>
      </Screenings>
   </BackgroundReportPackage>
</BackgroundReports>