首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT任务准备考试

XSLT任务准备考试
EN

Stack Overflow用户
提问于 2014-08-22 12:10:35
回答 1查看 166关注 0票数 0

关于XSLT的问题,我需要您的帮助。这是我考试准备的一部分。给出的信息如下:图1中XML文档的HTML呈现。注意,组和每个组中的人员以与XML文档相同的顺序出现。在每一名工作人员入职结束时,都有该学术人员所属的其他团体的识别资料。它们超链接到相应组节的开始。应用XSLT的最终结果必须是:

XML文件如下:

代码语言:javascript
复制
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="research.xsl"?>
<ResearchGroups xmlns="http://www.essex.ac.uk/ce832">
<Group name="Intelligent Systems Group" id="ISG"> The Intelligent
Systems Group pursues internationally-leading research in a wide
range of intelligent systems. </Group>
<Group name="Robotics" id="RBT"> The Essex robotics group is one of
the largest mobile robotics groups in the UK. </Group>
<Staff name="Callaghan, Vic" title="Professor" groups="ISG RBT">
Intelligent environments and robotics. </Staff>
<Staff name="Gu, Dongbing" title="Dr" groups="RBT"> Multi-agent
and distributed control systems. </Staff>
</ResearchGroups>

我创建的.xsl文件如下:

代码语言:javascript
复制
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rg="http://www.essex.ac.uk/ce832"
xmlns="http://wwww.w3.org/1999/xhtml"
version="2.0">
<xsl:template match="/">
<html>
<head> <title>Research Groups</title> </head>
<body> <xsl:apply-templates select="//rg:Group"/> 
</body>
</html>
</xsl:template>
<xsl:template match="rg:Group">
<xsl:variable name="ID" select="@id"/>
<h3> <a name="{$ID}"> <xsl:value-of select="@name"/> </a> </h3>
<p> <xsl:value-of select="text()"/> </p>
    <xsl:for-each select="//rg:Staff">
        <xsl:variable name="string" select="@groups" />
        <xsl:value-of select="$string" />
        <xsl:variable name="stringList" select="tokenize($string, ' ')" />
        <xsl:variable name="staffName" select="@name" />
        <xsl:variable name="description" select="text()" />
        <xsl:for-each select="$stringList">
        <xsl:variable name="item" select="." />
        <xsl:choose>
        <xsl:when test="matches($item, $ID)">
        <ul>
                    <li>
                    <xsl:value-of select="$staffName" />: <xsl:value-of select="$description" />
                    <xsl:value-of select="$ID" />
                    </li>
                    </ul>
        </xsl:when>
        </xsl:choose>
        </xsl:for-each>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

我遇到的问题是“在每一名工作人员入职结束时,都有学术人员所属的其他组的标识符,这些都是超链接到相应组段的开始部分。”因为第一名工作人员属于两组,而第二名则不属于两组。目前,显示的信息是当前组的id,而不是预期的。第一人属于世卫组织- ISG和RBT。当它们的名称出现在第一个组中时,必须显示另一个组的id,并包含到第二个组的链接--机器人。如果该人出现在第二组中,则同样适用。如果该人不属于多个组,则不需要显示链接。我想我必须从stringList变量中获取每个值并进行比较。问题是我不知道怎么做。我希望有人能帮我!提前谢谢你。

谢谢马克·文斯特拉的解决方案。有人能提供另一个解决方案吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-22 12:48:21

下一步,XSLT应该完成以下工作:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rg="http://www.essex.ac.uk/ce832" xmlns="http://wwww.w3.org/1999/xhtml" exclude-result-prefixes="rg">
    <xsl:output method="html" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Research Groups</title>
            </head>
            <body>
                <xsl:apply-templates select="rg:ResearchGroups/rg:Group"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="rg:Group">
        <xsl:variable name="ID" select="@id"/>
        <h3>
            <a name="{$ID}">
                <xsl:value-of select="@name"/>
            </a>
        </h3>
        <p>
            <xsl:value-of select="."/>
        </p>
        <ul>
            <xsl:apply-templates select="ancestor::rg:ResearchGroups/rg:Staff[contains(@groups, $ID)]">
                <xsl:with-param name="ID" select="$ID" />
            </xsl:apply-templates>
        </ul>
    </xsl:template>

    <xsl:template match="rg:Staff">
        <xsl:param name="ID" />
        <li>
            <xsl:value-of select="concat(@name, ': ', .)" />
            <xsl:for-each select="tokenize(translate(@groups, $ID, ''), ' ')">
                <xsl:if test="normalize-space(.) != ''">
                    <a href="#{.}"><xsl:value-of select="." /></a>
                </xsl:if>
            </xsl:for-each>
        </li>
    </xsl:template>
</xsl:stylesheet>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25446736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档