2016-01-01 34 views
2

我的问题是关于显示在方向节点步骤喜欢在最后用粗体和一些信息,而不是“速度,时间和温度”标签。如何为具有属性的Xml嵌套节点编写样式表?

XML数据示例

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="temp.xsl"?> 
<records> 
    <record> 
     <title>Text for title</title> 
     <category>Text for category</category> 
     <info>Text for info</info> 
     <author>Text for author</author> 
     <directions> 
      <step>Text for simple first step</step> 
      <step>Text for complex second step with <time type="seconds">5</time> and <velocity type="clockwise">3</velocity></step> 
      <step>Text for complex third step with <time type="minute">3</time> <temperature type="celsius">50</temperature> <velocity type="wheat">1</velocity> </step> 
      <step>Text for medium fourth step with <temperature type="celsius">50</temperature> and nothing more</step> 
     </directions> 
     <notes> 
      <note>Text for first note</note> 
      <note>Text for second note</note> 
     </notes> 
    </record> 
</records> 

temp.xsl

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <html> 
     <body> 
      <xsl:apply-templates/> 
     </body> 
    </html> 
</xsl:template> 
<xsl:template match="record"> 
    <xsl:apply-templates select="category"/> 
    <p> 
     <xsl:apply-templates select="title"/> 
     <xsl:apply-templates select="info"/> 
     <xsl:apply-templates select="author"/> 
    </p> 
    <h3>Directions</h3> 
    <p> 
     <ul> 
      <xsl:apply-templates select="directions"/> 
     </ul> 
    </p> 
    <h3>Notes</h3> 
    <p> 
     <ul> 
      <xsl:apply-templates select="notes"/> 
     </ul> 
    </p> 
</xsl:template> 
<xsl:template match="category"> 
    <h2> 
     <xsl:value-of select="."/> 
    </h2> 
</xsl:template> 
<xsl:template match="title"> 
    <h3> 
     <xsl:value-of select="."/> 
    </h3> 
</xsl:template> 
<xsl:template match="info"> 
    <xsl:value-of select="."/> 
    <br /> 
</xsl:template> 
<xsl:template match="author"> 
    Author: 
    <xsl:value-of select="."/> 
    <br /> 
</xsl:template> 
<xsl:template match="directions/step"> 
    <li> 
     <xsl:apply-templates select="time"/> 
     <xsl:value-of select="."/> 
    </li> 
</xsl:template> 
<xsl:template match="time"> 
    <b>time: 
     <xsl:value-of select="."/> 
    </b> 
</xsl:template> 
<xsl:template match="notes/note"> 
    <li> 
     <xsl:value-of select="."/> 
    </li> 
</xsl:template> 

在HTML预期输出:

工作文本类别

文本的标题

文本的信息

  • 作者:文本作者

路线

  • 文本进行简单的第一步
  • 文本复杂第二步骤与5秒速度5顺时针
  • 文本复杂第三步骤与3分钟50℃速度1小麦
  • 工作文本介质第四步骤与50°C并没有更多

  • 文本的第一个音符
  • 文本的第二个音符

回答

3

更改模板匹配directions/step到:

<xsl:template match="directions/step"> 
    <li> 
     <xsl:apply-templates/> 
    </li> 
</xsl:template> 

然后添加单独的模板来处理timevelocitytemperature

<xsl:template match="time"> 
    <b> 
     <xsl:value-of select="."/> 
     <xsl:text> </xsl:text> 
     <xsl:value-of select="@type"/> 
    </b> 
</xsl:template> 

<xsl:template match="velocity"> 
    <b> 
     <xsl:text>Speed </xsl:text> 
     <xsl:value-of select="."/> 
     <xsl:text> </xsl:text> 
     <xsl:value-of select="@type"/> 
    </b> 
</xsl:template> 

<xsl:template match="temperature"> 
    <b> 
     <xsl:value-of select="."/> 
     <xsl:text>° </xsl:text> 
     <xsl:value-of select="translate(substring(@type, 1, 1), 'cfk', 'CFK')"/> 
    </b> 
</xsl:template> 

这样,文本节点,上述三个兄弟节点也将得到处理。

+0

谢谢,它工作的很好!洙简单洙强大 – itom