XSLT混合内容节点 [英] XSLT mixed content node

查看:63
本文介绍了XSLT混合内容节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很愚蠢的问题.如何确保我的XML混合内容节点不会混淆?我有一个类似于此的XML结构.

I have a quite stupid question. How can I make sure that my XML mixed content node doesn't get mixed up? I have, say, an XML structure resembling this.

<root>
 <book>
  <title>Stuff</title>
  <description> This book is <i>great</i> if you need to know about stuff.
                I suggest <link ref="Things">this one</link> if you need to know
                about things. </description>
 </book>
 [other books]
</root> 

我需要最终内容看起来像这样

I need the final content to look like this

<h1>List of books</h1>
<h2><a name="Stuff"/>Stuff</h2>
<p> This book is <i>great</i> if you need to know about stuff.
    I suggest <a href="#Things">this one</a> if you need to know
    about things. </p>

但是我无法提取文本节点的部分,所以我总是抓住整个东西.我正在使用后代轴.知道我在做什么错吗?

But I can't extract the portions of the text node, I always grab the whole thing. I'm using the descendant axis. Any clue what I'm doing wrong?

这是我的xslt:

<xsl:template match="description/*">
    <xsl:for-each select="following-sibling::*">
            <xsl:choose>
            <xsl:when test="name(.)='link'">
                <a href="{@ref}"><xsl:value-of select="."/></a>
            </xsl:when>
            <xsl:when test="name(.)='em'">
                <em><xsl:value-of select="."/></em>
            </xsl:when>
            <xsl:otherwise><p><xsl:value-of select="."/></p></xsl:otherwise>    
        </xsl:choose>
    </xsl:for-each>
  </xsl:template>

请注意,随附的XML和生成的html仅仅是示例,为了清楚起见,我不得不处理一个更大的结构,但我并未将其包含在内.

Please note that the enclosed XML and resulting html are merely examples, I have to deal with a bigger structure which I'm not enclosing in, for sake of clarity.

推荐答案

<xsl:apply-templates>是你的朋友:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="html" />

  <xsl:template match="root">
    <h1>List of books</h1>
    <xsl:apply-templates />
  </xsl:template>

  <!-- a <book> consists of its <title> and <description> -->
  <xsl:template match="book">
    <xsl:apply-templates select="title" />
    <xsl:apply-templates select="description" />
  </xsl:template>

  <!-- <title> is turned into a <h2> -->
  <xsl:template match="title">
    <h2>
      <a name="{.}"/>
      <xsl:value-of select="." />
    </h2>
  </xsl:template>

  <!-- <description> is turned into a <p> -->
  <xsl:template match="description">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>

  <!-- default rule: copy any node beneath <description> -->
  <xsl:template match="description//*">
    <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <!-- override rule: <link> nodes get special treatment -->
  <xsl:template match="description//link">
    <a href="#{@ref}">
      <xsl:apply-templates />
    </a>
  </xsl:template>

  <!-- default rule: ignore any unspecific text node -->
  <xsl:template match="text()" />

  <!-- override rule: copy any text node beneath description -->
  <xsl:template match="description//text()">
    <xsl:copy-of select="." />
  </xsl:template>

</xsl:stylesheet>

为您的输入XML生成以下输出(注意:为了便于阅读,我将其通过整洁的方式进行了管道传输.在此过程中删除了不相关的空格):

The following output is generated for your input XML (Note: I piped it through tidy for the sake of readability. Non-relevant white-space was removed in the process):

<h1>List of books</h1>
<h2><a name="Stuff">Stuff</h2>
<p>This book is <i>great</i> if you need to know about stuff. I
suggest <a href="#Things">this one</a> if you need to know about
things.</p>

这篇关于XSLT混合内容节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆