Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 编写XSLT模板_Xml_Templates_Xslt - Fatal编程技术网

Xml 编写XSLT模板

Xml 编写XSLT模板,xml,templates,xslt,Xml,Templates,Xslt,我已经开始使用XSLT,现在有一些问题。我已经设法使用一系列有价值的select指令来输出XML文档,但是当我自己编写XSLT模板时,我真的很挣扎 以下是我的XML: <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="lecturers.xsl"?> <lecturers> <lecturer> <name> <t

我已经开始使用XSLT,现在有一些问题。我已经设法使用一系列有价值的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>

教授
彼得·奎克
XML与Web
Web服务器体系结构
Web协议在生物学中的应用
医生
布赖恩·约翰逊
计算机黑客
Web服务器体系结构
调查计算机黑客行为的各种复杂性

然后,这是我目前的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>

XML第7周
第7周:讲师文件转换为XSL:Template
标题
名称
教学
研究
 ;
及

这会将我需要的信息输出到一个表中,但是我被指示创建新模板来保存讲师元素,然后保存该元素的课程子元素。我可能只是想让事情变得过于复杂,但我就是无法让它正常工作,每当我尝试将模板应用到某个td时,浏览器中就会出现解析错误。那么,有人能给我一些建议吗?非常感谢,即使是一些基本的例子来解释如何让它在我的例子工作将是极好的。干杯,伙计们。

你们现在有:

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

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

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

  <body>
    <xsl:apply-templates/>

</body>

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

好的,那么,一旦我添加了模板match=“讲师”,我是否需要更改td中选择的值以使其成为一个模板,或者这就是我所需要的全部?我真的很困惑这整件事是怎么运作的,它教给我们的太差了。谢谢。您可以编写一个模板来匹配特定类型的节点,例如名为
讲师的元素节点,名为
match=“tascher”
的元素节点,并在模板内编写与匹配节点相关的表达式作为上下文节点。当然,您需要使用
apply templates
来确保节点得到处理。好的,很抱歉,这是一个痛苦的问题,但是,作为一个例子。如果我想使用模板显示标题,我将如何操作?想要这项工作:
<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>
<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>