基本XML / XSLT - 当它们具有相同名称时的许多节点的值和当它们包含TEXT和其他节点时

时间:2016-12-18 18:10:15

标签: xml xslt

所以这是我的问题

我试图处理以学习XSL基础知识的整个简单XML代码如下所示:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="penguins.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>Its not known how many more birds will be released from Port Elizabeth after receiving treatment. </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 but presently 
        there were in excess of 4 500 birds being rehabilitated and more would be brought to the 
        centre on Wednesday. </para>
<source>John Rolfe</source>
</article>

我试图弄明白,如何使用VALUE-OF打印整个<para>由其他子项组成,例如<company><link ref=...&gt;以及其他文本。我坚持这个:

    <xsl:for-each select="article/para">
    <xsl:value-of select="text()"/><br/>

只打印文本,没有任何其他子项。

很抱歉这个简单而基本的问题,但我刚刚启动了XML / XSLT

2 个答案:

答案 0 :(得分:1)

您应该使用XSLT的标准编码模式:为每个元素名称编写一个模板规则,您可以在其中递归处理其子代:

<xsl:template match="p">
  <p>
   <xsl:apply-templates/>
  </p>
</xsl:template>

然后修改要执行其他操作的元素的模板规则。

每个XSLT教科书都会详细介绍,所以如果你错过了它,那么在你开始编码之前你应该做更多的阅读。

答案 1 :(得分:0)

听起来你正试图在param元素上执行深层复制。 这个答案涵盖了这个XSLT: deep child copy