xslt - 如何针对另一个节点测试节点,并仅为测试为正的节点接收输出

时间:2014-07-13 17:42:34

标签: xml xslt

我正在创建一个将RDF Schema转换为HTML的XSLT。我试图将某些受控词汇表与相应的RDF类匹配。迄今为止的尝试都没有结果。

以下是RDF架构的示例:

    <?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:aps="rdf_apsSchema.rdf#"
    xml:base="rdf_apsSchema.rdf#">

    <rdfs:Class rdf:ID="ParentID">
        <rdfs:label xml:lang="en">Parent Identifier</rdfs:label>
        <rdfs:comment>A unique identifier for a carrier in which the current carrier was copied
            from</rdfs:comment>
        <rdfs:subClassOf rdf:resource="http://ella.slis.indiana.edu/~shelbyjt/aps/2013-12-17.rdf#ID"
        />
    </rdfs:Class>

    <rdfs:Class rdf:ID="TapeSpeed">
        <rdfs:label xml:lang="en">Tape Speed</rdfs:label>
        <rdfs:comment>The speed in inches per second (IPS) of a tape carrier</rdfs:comment>
    </rdfs:Class>

    <rdfs:Class rdf:ID="TrackConfig">
        <rdfs:label xml:lang="en">Track Configuration</rdfs:label>
        <rdfs:comment>The track configuration of a tape carrier</rdfs:comment>
    </rdfs:Class>

    <aps:TrackConfig rdf:ID="FullTrack"/>
    <aps:TrackConfig rdf:ID="HalfTrackMono"/>
    <aps:TrackConfig rdf:ID="HalfTrackStereo"/>
    <aps:TrackConfig rdf:ID="QuarterTrackMono"/>
    <aps:TrackConfig rdf:ID="QuarterTrackStereo"/>

</rdf:RDF>

以下是XSLT的示例:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:aps="rdf_apsSchema.rdf#" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

    <!-- shell for the html -->
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <title>schema html</title>
            <body>
                <xsl:apply-templates select="rdf:RDF"/>
            </body>
        </html>
    </xsl:template>

    <!-- sets up the Class Class tables -->
    <xsl:template match="rdf:RDF">
        <table>
            <xsl:apply-templates select="rdfs:Class"/>
        </table>
    </xsl:template>

    <!-- sets up the rdfs:Class transformations -->
    <xsl:template match="rdfs:Class">
        <xsl:for-each select=".">
            <xsl:if test="rdfs:label">
                <tr>
                    <th>
                        <xsl:text>name: </xsl:text>
                        <xsl:value-of select="rdfs:label"/>
                    </th>
                </tr>
            </xsl:if>
    <!-- this is the problem code -->
            <xsl:for-each select="@rdf:ID=document('controlledVocab.xml')/controlledVocab/*">
                <tr>
                    <th>
                        <xsl:text>controlled vocabulary: </xsl:text>
                    </th>
                </tr>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

问题代码(在XSLT中记录)应该测试<rdfs:Class/@rdf:ID>是否等于document('controlledVocab.xml')/controlledVocab/*(此文件包含带有前缀的<aps:*>元素)。如果是这样,请将“以下内容”添加到<table>

的该元素部分

以下是controlledVocab.xml的示例:

<?xml version="1.0" encoding="UTF-8"?>
<controlledVocab xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    <TrackConfig rdf:ID="FullTrack"/>
    <TrackConfig rdf:ID="HalfTrackMono"/>
    <TrackConfig rdf:ID="HalfTrackStereo"/>
    <TrackConfig rdf:ID="QuarterTrackMono"/>
    <TrackConfig rdf:ID="QuarterTrackStereo"/>
</controlledVocab>

期望的结果:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>schema html</title>
    </head>
    <body>
        <table>
            <tr>
                <th>label: </th>
                <td>Parent Identifier</td>
            </tr>
            <tr>
                <th>label: </th>
                <td>Tape Speed</td>
            </tr>
            <tr>
                <th>label: </th>
                <td>Track Configuration</td>
            </tr>
            <tr>
                <th>controlled vocabulary: </th>
            </tr>
        </table>
    </body>
</html>

目前的结果:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>schema html</title>
    </head>
    <body>
        <table>
            <tr>
                <th>label: </th>
                <td>Parent Identifier</td>
            </tr>
            <tr>
                <th>controlled vocabulary: </th>
            </tr>
            <tr>
                <th>label: </th>
                <td>Tape Speed</td>
            </tr>
            <tr>
                <th>controlled vocabulary: </th>
            </tr>
            <tr>
                <th>label: </th>
                <td>Track Configuration</td>
            </tr>
            <tr>
                <th>controlled vocabulary: </th>
            </tr>
        </table>
    </body>
</html>

我为这篇长篇文章道歉,但我希望尽可能清楚。任何反馈将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,问题尚不清楚。看看这是否可以让你面向。它测试当前的类id是否出现在controlledVocab的子元素列表中。如果是,则表格单元格包含&#34;肯定&#34;被添加到当前行:

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:aps="rdf_apsSchema.rdf#" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

    <!-- shell for the html -->
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <title>schema html</title>
            <body>
                <xsl:apply-templates select="rdf:RDF"/>
            </body>
        </html>
    </xsl:template>

    <!-- sets up the Class Class tables -->
    <xsl:template match="rdf:RDF">
        <table border="1">
            <xsl:apply-templates select="rdfs:Class"/>
        </table>
    </xsl:template>

    <!-- sets up the rdfs:Class transformations -->
    <xsl:template match="rdfs:Class[rdfs:label]">
        <tr>
            <th>label: </th>
            <td><xsl:value-of select="rdfs:label"/></td>
            <!-- this is the problem code -->
            <xsl:if test="@rdf:ID = document('controlledVocab.xml')/controlledVocab/*/name()">
                <td>positive</td>
            </xsl:if>
        </tr>
    </xsl:template>

</xsl:stylesheet>