如何使用xslt格式化内部节点,同时您还需要格式化外部节点?
How to use xslt to format inner nodes while you also need to format the outer node?
我有以下 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 元素及其 sons 元素上应用模板来获得上图中描述的输出。
到现在我只有:
<?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: 不允许 for-each。只有模板和应用模板。这是一个关于作业的练习,它要求最少使用模板并且没有 for-each
看来你需要在<xsl:template match="para">
里面使用<xsl:for-each>
例如:
<xsl:for-each select="place">
(当然这不是唯一的解决方案,但这是我想到的)
因为你提到这是一个练习,我不想给出一个完整的解决方案,但也许你可以使用一些东西:当你将以下模板(并替换 <xsl:template match="para">
)添加到你的XSLT
<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
使用 ref
属性的 href
值创建一个 <a>
标签。模板匹配 company
和 source
元素创建一个 <span>
具有粗体和普通字体样式(这里你也可以使用 css 类 而不是内联样式) ,因为所需输出中的大部分副本都是斜体。
因此,您可以只使用与输入元素匹配的模板来创建 HTML 标记并应用适当的内联样式或添加 类 以获得所需的输出。
我有以下 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 元素及其 sons 元素上应用模板来获得上图中描述的输出。 到现在我只有:
<?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: 不允许 for-each。只有模板和应用模板。这是一个关于作业的练习,它要求最少使用模板并且没有 for-each
看来你需要在<xsl:template match="para">
<xsl:for-each>
例如:
<xsl:for-each select="place">
(当然这不是唯一的解决方案,但这是我想到的)
因为你提到这是一个练习,我不想给出一个完整的解决方案,但也许你可以使用一些东西:当你将以下模板(并替换 <xsl:template match="para">
)添加到你的XSLT
<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
使用 ref
属性的 href
值创建一个 <a>
标签。模板匹配 company
和 source
元素创建一个 <span>
具有粗体和普通字体样式(这里你也可以使用 css 类 而不是内联样式) ,因为所需输出中的大部分副本都是斜体。
因此,您可以只使用与输入元素匹配的模板来创建 HTML 标记并应用适当的内联样式或添加 类 以获得所需的输出。