Подтвердить что ты не робот

Смешанный контент XSLT node

У меня есть довольно глупый вопрос. Как я могу убедиться, что мой смешанный контент XML node не запутался? Я имею, скажем, структуру XML, похожую на это.

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

Мне нужно, чтобы окончательный контент выглядел так:

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

Но я не могу извлечь фрагменты текста node, я всегда хватаю все это. Я использую ось потомков. Любая подсказка, что я делаю неправильно?

Вот мой 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 являются просто примерами, я должен иметь дело с более крупной структурой, для которой я не прилагаюсь, для ясности.

4b9b3361

Ответ 1

<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 создается следующий вывод (Примечание: я использовал его для удобства в целях обеспечения удобочитаемости. В этом процессе было удалено пустое пространство):

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

Ответ 2

<root>
 <book>
  <title>Stuff</title>
  <description><![CDATA[
      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>