簡體   English   中英

為什么帶有if-condition的for-each選擇全部而不是特定節點的屬性

[英]Why does for-each with if-condition select all instead of specific node's attribute

編輯:我找到了一個適合我的解決方案(正如 Conal Tuohy 建議的那樣,更改 RDF 語法確實更好):

<xsl:variable name="all_rels_comparable" select="$all/relations/rel[@name = 'is_comparable_to']"/>
<xsl:for-each select="$all_rels_comparable[@from = $id_lexUnit]">
    <bla:is_comparable_to rdf:resource="blabla/is_comparable_to/{./@to}" />
</xsl:for-each>

我正在將各種 XML 文件中的信息合並到一個 RDF-XML 文件中。 當我嘗試檢索單個節點時,我總是用完內存,因為它以某種方式迭代了比預期更多的節點。 我猜這行<xsl:value-of select="$all/relations/rel/@to"/>是這里的問題。 正確的方法是什么?

這是我的 xsl 腳本:

<xsl:stylesheet version="3.0">

<xsl:template match="node()|@*">
    <rdf:RDF 
        xmlns:bla="blabla"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:xml="http://www.w3.org/XML/1998/namespace"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

        
        <xsl:variable name="all" select="collection('./?select=*.xml')"/>

        <xsl:for-each select="$all/sets/singleSet/unit">

            <xsl:variable name="id_unit" select="./@id"/>
            <xsl:variable name="uri_unit">
                <xsl:copy-of select="concat('https://blabla/', $id_unit)" />
            </xsl:variable>

            <NamedIndividual rdf:about="{$uri_unit}">
                
                <bla:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
                    <xsl:value-of select="./@id"/>
                </bla:identifier>
                
                <!-- THIS PART DOES NOT WORK AS EXPECTED: -->
                <xsl:if test="(($all/relations/rel/@name = 'is_comparable_to') and ($all/relations/rel/@from = $id_unit))">
                    <bla:is_comparable_to rdf:resource="blabla/is_comparable_to">
                        <xsl:value-of select="$all/relations/rel/@to"/>
                    </bla:is_comparable_to>
                </xsl:if>

            </NamedIndividual>
        </xsl:for-each>
        
    </rdf:RDF>
    
</xsl:template>
</xsl:stylesheet>

這是我嘗試僅獲取“to”的最后一行值的文件:

<relations>
<rel name="has_user" from="28" to="45" dir="one" />
<rel name="is_part_of" from="22" to="90" dir="one" />
<rel name="is_comparable_to" from="55" to="36" dir="one" />
<rel name="is_comparable_to" from="11" to="40" dir="one" />
</relations>

所以這是我打算得到的輸出:

<NamedIndividual rdf:about="https://blabla/11">
    <bla:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#string">11</bla:identifier>
    <bla:is_comparable_to rdf:resource="blabla/is_comparable_to">40</bla:is_comparable_to>
</NamedIndividual>

你正在做的

<xsl:for-each select="$all/sets/singleSet/unit">

在正文中,您有幾個以$all/relations開頭的路徑表達式

relations也許是unit的子元素? 無論如何,當您當前正在處理比 $all 低幾個級別的元素時,從$all $all進行選擇似乎很奇怪。 但是如果沒有看到整個 XML 的樣子,我們就看不到如何糾正它。

在我看來,在您的xsl:for-each中,您正在搜索relations ,尋找將您當前unit連接到其他unitrel元素。 如果我理解正確,你應該使用這樣的東西來代替你的xsl:if塊:

<xsl:for-each test="$all/relations/rel[@from = $id_unit][@name = 'is_comparable_to']">
    <bla:is_comparable_to rdf:resource="blabla/is_comparable_to">
        <xsl:value-of select="@to"/>
    </bla:is_comparable_to>
</xsl:if>

但是,在您的示例中,用於表示is_comparable_to屬性的 RDF/XML 語法也很混亂。 我想也許您打算將單位之間的關系( bla:is_comparable_to )建模為 RDF 對象屬性? IE

<xsl:for-each test="$all/relations/rel[@from = $id_unit][@name = 'is_comparable_to']">
    <bla:is_comparable_to rdf:resource="https://blabla/{@to}"/>
</xsl:if>

...產生:

<NamedIndividual rdf:about="https://blabla/11">
    <bla:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#string">11</bla:identifier>
    <bla:is_comparable_to rdf:resource="https://blabla/40"/>
</NamedIndividual>

暫無
暫無

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

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