xml - 编写 XSLT 模板

标签 xml templates xslt

我已经开始使用 XSLT,但遇到了一些问题。我已经使用一系列 value-of select 指令设法输出了我的 XML 文档,但是当我自己编写 XSLT 模板时,我真的很挣扎。

这是我的 XML:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="lecturers.xsl"?>
<lecturers>
<lecturer>
        <name> 
             <title>Professor</title> 
        <first>Peter </first> <last>Quirk</last>
        </name>
        <teaching>
        <course code="CO3070">XML and the Web</course>
        <course code="CO3300"> Web Server Architectures</course>
    </teaching>
    <research>
        The application of Web protocols to Biology
    </research>
</lecturer>

<lecturer>
    <name> 
    <title>Doctor</title> 
    <first>Brian </first> <last>Johnson</last>
    </name>
    <teaching>
        <course code="CO9999">Computer Hacking</course>
        <course code="CO3300"> Web Server Architectures</course>
    </teaching>
    <research>
        Investigating the various complexities of Computer Hacking
    </research>
</lecturer>

然后,这是我目前的 XSL:
 <?xml version="1.0"?>
 <xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" version="4.0" />

 <xsl:template match="/">
 <html>
 <head>
 <title>XML Week 7</title>
 </head>

  <body>
  <h1>Week 7: Lecturers file turned to XSL:Template</h1>

 <table border="1">
<tr>
    <th><b>Title</b></th>
    <th><b>Name</b></th>
    <th><b>Teaching</b></th>
    <th><b>Research</b></th>
</tr>

<tr>

    <td><xsl:value-of select="lecturers/lecturer/name/title" /></td>
    <td><xsl:value-of select="lecturers/lecturer/name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="lecturers/lecturer/name/last" /></td>
    <td><xsl:value-of select="lecturers/lecturer/teaching/course" /> and <xsl:value-of select="(lecturers/lecturer/teaching/course)[2]" /></td>
    <td><xsl:value-of select="lecturers/lecturer/research" /></td>

</tr>
 </table>

</body>

</html>
 </xsl:template>
 </xsl:stylesheet>

这确实将我需要的信息输出到表格中,但我被指示创建新模板来保存讲师元素,然后是该元素的类(class)子元素。我可能只是把事情想得太复杂了,但我就是无法让它工作,每当我尝试将模板应用于其中一个 td 时,我都会在浏览器中遇到解析错误。那么,有人对我有什么建议吗?将不胜感激,即使是一些基本的例子来解释如何让它在我的例子中工作也会很棒。干杯伙计们。

最佳答案

使用一个模板来创建结果文档结构,例如

 <xsl:template match="/">
 <html>
 <head>
 <title>XML Week 7</title>
 </head>

  <body>
    <xsl:apply-templates/>

</body>

</html>
 </xsl:template>

然后编写更多模板来创建内容,例如
<xsl:template match="lecturers">
  <h1>Week 7: Lecturers file turned to XSL:Template</h1>

 <table border="1">
<tr>
    <th><b>Title</b></th>
    <th><b>Name</b></th>
    <th><b>Teaching</b></th>
    <th><b>Research</b></th>
</tr>
  <xsl:apply-templates/>
</table>
</xsl:template>

<xsl:template match="lecturer">
<tr>

    <td><xsl:value-of select="name/title" /></td>
    <td><xsl:value-of select="name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="name/last" /></td>
    <td><xsl:value-of select="teaching/course" /> and <xsl:value-of select="(teaching/course)[2]" /></td>
    <td><xsl:value-of select="lecturers/lecturer/research" /></td>

</tr>
</xsl:template>

如您所见,模板执行 apply-templates以保持处理。

[编辑]
为了回应您的评论,如果您想为 lecturer 的不同子元素或后代元素使用更多模板你可以使用的元素
<xsl:template match="lecturer">
   <tr>
     <xsl:apply-templates/>
   </tr>
</xsl:template>

然后为元素编写模板,例如
<xsl:template match="name/title | research">
  <td>
    <xsl:value-of select="."/>
  </td>
</xsl:template>

<xsl:template match="name/first">
  <td>
    <xsl:value-of select="concat(., ' ', ../last)"/>
  </td>
</xsl:template>

<!-- don't output name/last as the name/first template already does -->
<xsl:template match="name/last"/>

<xsl:template match="teaching">
  <xsl:apply-templates select="course"/>
</xsl:template>

<xsl:template match="course">
  <xsl:if test="position() > 1"><xsl:text> and </xsl:text></xsl:if>
  <xsl:value-of select="."/>
</xsl:template>

关于xml - 编写 XSLT 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13682096/

相关文章:

function - 在 Go 条件模板中使用自定义函数

javascript - drupal 6 和 jquery 1.4/1.5

xml - XSLT:使用 xsl:value-of - 我返回的是字符串还是单个字符串的节点集?

c# - SqlDataReader 不生成 XML 文档

javascript - 在 iframe 上运行 javascript 函数 (execCommand ('SaveAs' ..)

java - 如何在android中创建上述自定义对话框?

c++ - 特定的 C++ 模板特化

c# - 更多 XML 问题 - 未声明的实体 'nbsp'

c# - XSLT 的输出参数

xml - 根据 .csv 文件更新 .xml 文档