如何在单个xpath中为此编写谓词?

时间:2019-04-23 11:37:49

标签: xslt-2.0 xslt-3.0

我正在尝试编写一个函数,该函数基于遵循某种逻辑的属性来返回值。

if <Position isAdded="1"> exists then this is what I need.

if <Position isAdded="1"> doesn't exist, and <Position isUpdated=1> exists, that I need this element.

I never should pull <Position isDeleted=1>

If isAdded and isUpdated don't exist, then I need to get the value from 
<Position> without any attributes.

这是我到目前为止尝试过的     / Root / Effective_Change / Position [not(exists(@isDeleted))] [存在(@iwego)]

让我了解iFyre元素。 我可以添加多选时间并测试每个属性。我只是 尝试查看是否可以在单个xpath中完成此操作。由于我的xml中有很多元素,所以我不想对每个元素都这样做。因此 试图建立一个功能。 喜欢

/Root/Effective_Change/Position[if (exists(@isAdded then
     .[@isAdded]
else if exists(@isUpdated) then 
.[@isUpdated] else not(exists(@isDeleted))] 

示例xml

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <Effective_Change Sequence="1">
        <Position isUpdated="1">
            <Business_Title>Updated Product Engineer</Business_Title>
            <Worker_Type>Regular</Worker_Type>
        </Position>
        <Position isAdded="1">
            <Business_Title>Product Engineer</Business_Title>
            <Worker_Type>Regular</Worker_Type>
        </Position>
        <Position isDeleted="1">
            <Business_Title>Deleted Product Engineer</Business_Title>
            <Worker_Type>Regular</Worker_Type>
        </Position>
        <Position>
            <Business_Title>Product Engineer</Business_Title>
            <Worker_Type>Regular</Worker_Type>
        </Position>
    </Effective_Change>
</Root>

实际文件可以在多个级别上具有这些属性。对于 例如:

<Position isUpdated="1">
    <Business_Title>Updated Product Engineer</Business_Title>
    <Worker_Type isDeleted="1">Temporary</Worker_Type>
    <Worker_Type isAdded="1">Regular</Worker_Type>
</Position>

我使用此功能获取未删除的数据

<xsl:function name="this:getNonDeletedElement">
    <xsl:param name="element"/>
    <xsl:variable name="nodeName">
        <xsl:value-of select="$element[1]/name()"/>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="exists($element/ancestor::*[name() = 'Effective_Change']//*[not(exists(@isDeleted))]/*[name() = $nodeName][not(exists(@isDeleted))])">
            <xsl:value-of select="$element/ancestor::*[name() = 'Effective_Change']//*[not(exists(@isDeleted))]/*[name() = $nodeName][not(exists(@isDeleted))]"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="''"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:function>

但是,此函数仍将返回i☎联系人,isUpdated和未归因的行。在这种情况下,如果i长存不存在,我需要,如果i长存不存在而isUpdated确实存在,那么我需要,否则是非归因行。

我正在用它来检查iFyre

<xsl:function name="this:isElementAdded">
    <xsl:param name="element"/>
    <xsl:choose>
        <xsl:when test="$element/../@isAdded or $element/@isAdded or exists($element/ancestor::*[@isAdded and name(..)='Effective_Change'])">
            <xsl:value-of select="true()"/>                                                 
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="false()"/>                        
        </xsl:otherwise>
    </xsl:choose>
</xsl:function> 

我想知道是否有更简单的方法来做到这一点。

0 个答案:

没有答案