2012-01-08 32 views
-1

如何完成应该产生在图HTML输出下面的样式表1.如何这个样式表可以生成HTML输出

我都称呼它的某些部分,但在图1中不能使其完全一样。我尝试过XSL中的“复制元素”,但给了我重复的结果。

这是我的XML:

<?xml version="1.0"?> 
<?xml-stylesheet type="text/xsl" href="research.xsl"?> 
<ResearchGroups xmlns="http://www.sam.com/sam.html"> 

<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> 

我的XSLT:

<xsl:stylesheet 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:rg="http://www.sam.com/sam.html" 
     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:template> 
</xsl:stylesheet> 

HTML图1

XSLT样式表应该输出以下HTML

enter image description here

回答

1

事情是这样的,我认为:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rg="http://www.sam.com/sam.html" 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> 
     <ul> 
      <xsl:apply-templates select="//rg:Staff[contains(@groups, current()/@id)]"> 
       <xsl:with-param name="curGroup"><xsl:value-of select="@id"/></xsl:with-param> 
      </xsl:apply-templates> 
     </ul> 
    </xsl:template> 
    <xsl:template match="rg:Staff"> 
     <xsl:param name="curGroup"/> 
     <li> 
      <xsl:value-of select="@name"/> 
      <xsl:text>: </xsl:text> 
      <xsl:value-of select="text()"/> 
      <xsl:if test="//rg:Group[(@id != $curGroup) and contains(current()/@groups, @id)]"> 
       <xsl:text> (</xsl:text> 
       <xsl:apply-templates select="//rg:Group[(@id != $curGroup) and contains(current()/@groups, @id)]" mode="otherGroups"/> 
       <xsl:text>) </xsl:text> 
      </xsl:if> 
     </li> 
    </xsl:template> 
    <xsl:template match="rg:Group" mode="otherGroups"> 
     <a href="#{@id}"> 
      <xsl:value-of select="@id"/>  
     </a> 
    </xsl:template> 
</xsl:stylesheet> 
+0

许多感谢,伟大的 – 2012-01-08 10:22:02

+0

能否请你解释一下,究竟是什么在这部分代码:[包含(@groups,电流( )/@ID)] ?我不确定是否正斜杠/谢谢 – 2012-01-08 11:21:56

+0

'current()'返回正在元素外部处理的节点,即组。因此'current()/ @ id'返回Group元素的'id'属性。 '@ groups'是被匹配的Staff元素的'groups'属性,而contains是一个简单的“第二个字符串是第一个字符串的子字符串?”测试。 – Alohci 2012-01-08 11:32:37