0

尝试使用 XSLT 在我的 XML 中创建定义列表。

这是我的输入的示例:

  <p>

    <i>word1</i> definition text here <br />
    <br />
       <i>word1</i> definition text here <br />
    <br />
       <i>word1</i> definition text here <br />
    <br />
       <i>word1</i> definition text here <br />
    <br />
       <i>word1</i> definition text here <br />

</p>

上述 XML 中的“此处的定义文本”是我想要标记并包含在输出中的未标记文本节点。我想要的输出说明如下:

<dl>
   <di>
      <dt>word1</dt>
      <dd>definition text here<dd>
   <di>
<dl>

到目前为止,我的模板不起作用:

<xsl:template match="p">

        <dl>
             <dt>
                <xsl:value-of select="./i/node()"/>
            </dt>

             <dd>
              <xsl:sequence select="./text()" />
            </dd>
        </dl>

    </xsl:template>

任何人都知道一个快速简便的方法来做到这一点?

提前致谢。

4

2 回答 2

1

You need to emit one di element for each i in the input, not one for each p in the input. So first of all, move your code to a template for i.

In your input, each i is immediately followed by a single text node containing the text to be tagged as dd. This has to be embedded within the di element, so it needs to be handled within the template for i, something like this (not tested):

<xsl:template match='i'>
  <di>
    <dt><xsl:value-of select="."/></dt>
    <dd><xsl:value-of select="following-sibling::text()[1]"/></dd>
  </di>
</xsl:template>
<xsl:template match="p/text() | p/br"/>

If some definitions have embedded markup, you'll need a more complicated way to populate the dd element, but sufficient unto the day is the evil thereof. You asked for something quick and easy.

于 2012-09-15T02:24:55.947 回答
1

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="i">
  <dt><xsl:value-of select="."/></dt>
 </xsl:template>

 <xsl:template match="text()[preceding-sibling::*[1][self::i]]">
  <dd><xsl:value-of select="normalize-space()"/></dd>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document (it was severely malformed -- corrected here):

<p>
    <i>word1</i> definition text here 
    <br />
    <br />
    <i>word2</i> definition text here
    <br />
    <br />
    <i>word3</i> definition text here
    <br />
    <br />
    <i>word4</i> definition text here
    <br />
    <br />
    <i>word5</i> definition text here
    <br />
</p>

produces the wanted, correct result:

<dl>
   <di>
      <dt>word1</dt>
      <dd>definition text here</dd>
      <dt>word2</dt>
      <dd>definition text here</dd>
      <dt>word3</dt>
      <dd>definition text here</dd>
      <dt>word4</dt>
      <dd>definition text here</dd>
      <dt>word5</dt>
      <dd>definition text here</dd>
   </di>
</dl>

and it displays in the browser as:

word1
definition text here
word2
definition text here
word3
definition text here
word4
definition text here
word5
definition text here
于 2012-09-15T02:28:27.337 回答