我有以下xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ping1.xsl"?>
<article>
<date>28/06/2000 12:30</date>
<title>Rescued penguins swim home</title>
<para><place>Cape Town</place> Some 150 penguins unaffected by the oil spill began
their long swim from Port Elizabeth in the Eastern Cape back to their breeding
habitat at Robben Island near Cape Town on Wednesday. </para>
<para>The penguins, who have all been tagged, were transported in a truck hired by
the <company>South African National Conservation of Coastal Birds
(Sanccob)</company> to Port Elizabeth on Tuesday night. </para>
<para>More than <link ref="www.newsrus.com/oilspill.html">400 tons of fuel oil
escaped from the bulk ore carrier Treasure</link> before divers were able to seal
the holds.</para>
<para>The ship was carrying 130 000 tons of iron ore and 1 300 tons of fuel oil
when she sank off the Cape West coast last Friday. </para>
<para>A spokesperson for <company>Sanccob</company>, Christina Pretorius said
the centre had a capacity to treat 1 000 penguins. </para>
<source>John Rolfe</source>
</article>
我需要在下面的html表单中转换它(你有HTML表单的图像):
问题是我不知道如何在para元素及其子元素上应用模板以获得上图中描述的输出。 直到现在我只有:
<?xml version="1.0" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
<xsl:template match='/'>
<HTML>
<BODY>
<xsl:apply-templates />
</BODY>
</HTML>
</xsl:template>
<xsl:template match="date">
<b><xsl:value-of select="."/></b>
<br/>
</xsl:template>
<xsl:template match="title">
<font color="blue"><xsl:value-of select="."/></font>
<br/>
<br/>
</xsl:template>
<xsl:template match="para">
<xsl:value-of select="."/><br/>
</xsl:template>
</xsl:stylesheet>
PS:我并不真正关心确切的html风格。你也可以使它更简单。我只需要知道如何将模板应用于para元素及其子元素,并具有图像中描述的输出。 PS2:不允许 - 每个人都允许。仅模板和应用模板。这是一项关于作业的练习,它要求最少使用模板,而不是每个
答案 0 :(得分:1)
您似乎需要在<xsl:for-each>
<xsl:template match="para">
e.g:
<xsl:for-each select="place">
(当然这不是唯一的解决方案,但它是我想到的)
答案 1 :(得分:0)
因为你提到它是一个练习,我不想提供一个完整的解决方案,但也许你可以使用的东西:当你添加以下模板(并替换{{1} })到您的XSLT
<xsl:template match="para">
创建此输出(仅作为示例的一部分):
<xsl:template match="para">
<p>
<xsl:apply-templates />
</p>
</xsl:template>
<xsl:template match="link">
<a>
<xsl:attribute name="href" select="@ref"/>
<xsl:apply-templates />
</a>
</xsl:template>
<xsl:template match="company | source">
<span style="font-weight:bold; font-style:normal;">
<xsl:apply-templates />
</span>
</xsl:template>
匹配<p>The penguins, who have all been tagged, were transported in a truck hired by the
<span style="font-weight:bold; font-style:normal;">South African National Conservation
of Coastal Birds (Sanccob)</span> to Port Elizabeth on Tuesday night.
</p>
<p>More than <a href="www.newsrus.com/oilspill.html">400 tons of fuel oil escaped
from the bulk ore carrier Treasure</a> before divers were able to seal the holds.
</p>
的模板会创建para
元素,并将模板应用于<p>
元素的内容。匹配para
的模板会创建link
标记,其<a>
属性的href
值。匹配ref
和company
元素的模板会创建一个带有粗体和普通字体样式的source
(这里也可以使用css类而不是内联样式),因为大多数副本都在期望的输出是斜体
因此,您可以使用与输入元素匹配的模板来创建HTML标记,并应用适当的内联样式或添加类以获得所需的输出。