簡體   English   中英

XSLT 2.0比較屬性值與變量的順序

[英]XSLT 2.0 Compare sequence of attribute values to variable

我有諸如

<elem attr1="value1 someValue" attr2="value2 someOtherValue"/>

具有可變數量的屬性的元素,每個元素具有可變數量的值。

這些屬性的名稱和它們的某些值會預先保存在變量中,並根據這些變量檢查元素。

我需要檢查元素是否具有在該變量中指定的屬性(我已經開始使用該屬性),並且對於每個屬性,至少其值之一是在另一個變量中指定的值之一。

因此,如果變量包含“ Value1 Value2 ”(或更多值)

元素

<elem attr1="Value1 SomeValue" attr2="Value1 someOtherValue AthirdValue" attr3="value2 otherValue"/>

滿足要求(前提是attr1,attr2和attr3是正確的屬性名稱,我之前已經對其進行過檢查),但是元素:

<elem attr1="Value1 SomeValue" attr2="anything someOtherValue" attr3="value otherValue"/>

不符合要求,因為其中一個屬性沒有包含在變量中的值( attr2 :在變量中未指定anythingsomeOtherValue )。

我嘗試對值進行標記,但是即使只有一個屬性具有正確的值之一,這些元素也被認為是正確的。 我需要確保所有屬性至少具有正確的值之一。

幫助和提示高度贊賞!

編輯:

獲取變量值的方式:

<xsl:variable name="filterName" select="$someDoc//someElem[@id = $curID]//filters/@value"/>

另一個文件:

<?xml version="1.0"/>
<doc>
<filters name="attr1" value="value1"/>
<filters name="attr2" value="value2"/>
<filters name="attr3" value="value2"/>
</doc>

**EDIT 2:**

查看上面的filter元素,elems可能如下所示:

<elem/> <!-- no attribute -->
<elem someattr="somevalue"/> <!-- some irrelevant attribute -->
<elem attr1="value1"/> <!-- one of the attributes -->
<elem attr1="value1" attr2="value"/> <!-- two of the attributes -->

因此, filters元素的@name屬性指定elem元素上的屬性的名稱, filters元素的@value屬性指定該屬性必須具有的值才能滿足要求。

解決方案(改編自答案)

<xsl:when test="count(@*[local-name() = $filterNames]) > 1">
<!-- element has more then one filter attribute -->
    <xsl:variable name="currentFilterValues">
        <xsl:value-of select="$filterValues"/>
    </xsl:variable>
    <xsl:variable name="attributeNamesOfCurrentElement">
        <xsl:value-of select="@*[local-name() = $filterNames]/fn:local-name()"/>
    </xsl:variable>
    <xsl:variable name="errors">
        <xsl:for-each select="tokenize($attributeNamesOfCurrentElement, '\s+')">
            <xsl:variable name="currentName" select="."/>
            <xsl:variable name="currentValue" select="$currentElement/@*[fn:local-name() = $currentName]"/>
            <xsl:if test="not(tokenize($currentValue, '\s+') = tokenize($currentFilterValues, ' '))">
                <error/>
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="$errors/error">
            <!-- at least one attribute doesnt have any of the required values -->
        </xsl:when>
        <xsl:otherwise>
            <!-- all attributes have at least one of the required values -->
            <xsl:copy>
                <xsl:apply-templates select="$currentElement_2/@*"/>
                <xsl:if test="child::*">
                    <xsl:for-each select="child::*">
                        <xsl:call-template name="recurse">
                            <xsl:with-param name="filterNames" select="$filterNames"/>
                            <xsl:with-param name="filterValues" select="$filterValues"/>
                            <xsl:with-param name="filterType" select="$filterType"/>
                            <xsl:with-param name="currentElement_2" select="."/>
                        </xsl:call-template>
                    </xsl:for-each>
                </xsl:if>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:when>

這有點抽象,但是為了展示一個原理,讓我們有以下幾點:

XML格式

<root>
    <elem attr1="alpha charlie" attr2="delta alpha echo" attr3="foxtrot bravo golf"/>
    <elem attr1="alpha charlie" attr2="hotel delta" attr3="bravo india"/> 
</root>

XSLT 2.0

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="values" select="('alpha', 'bravo')"/>

<xsl:template match="/root">
    <xsl:copy>
        <xsl:apply-templates select="elem"/>
    </xsl:copy>
</xsl:template>     

<xsl:template match="elem">
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="@*[not(tokenize(., ' ')=$values)]">
                <xsl:text>NO</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>YES</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

結果

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <elem>YES</elem>
   <elem>NO</elem>
</root>

說明

考試:

test="@*[not(tokenize(., ' ')=$values)]"

當至少一個屬性在標記化之后不包含任何與$ values變量中的值之一匹配的標記時,返回true。


編輯

根據您修改后的要求,我認為這應該可行:

XSLT 2.0

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="filter-doc" select="document('filter.xml')"/>

<xsl:key name="filter-by-name" match="filters" use="@name" />

<xsl:template match="/root">
    <xsl:copy>
        <xsl:apply-templates select="elem"/>
    </xsl:copy>
</xsl:template>     

<xsl:template match="elem">
    <xsl:variable name="strikes">
        <xsl:for-each select="@*[key('filter-by-name', name(), $filter-doc)]">
            <xsl:variable name="values" select="key('filter-by-name', name(), $filter-doc)/@value" />
            <xsl:if test="not(tokenize(., ' ')=$values)">
                <strike/>
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>

    <xsl:copy>
        <xsl:choose>
            <xsl:when test="$strikes/strike">
                <xsl:text>NO</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>YES</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

可能可以簡化一下,但是首先我想知道它是否提供正確的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM