如何使用xslt 1.0逐行输出文本节点值?

时间:2013-04-17 22:32:27

标签: xml xpath xslt-1.0

我有一个xml文件,如:

<Messages>
   error message 1

   error message 2 
</Messages>

我希望输出为:

Error 1: 
error message 1

Error 2:
error message 2

我正在使用xslt 1.0,我尝试过:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" omit-xml-declaration="yes"/>

<xsl:template match="Messages">
    <h3>Error 1:</h3>
    <xsl:value-of select="substring-before(./text(), '&#10;')"/>
    <h3>Error 2:</h3>
    <xsl:value-of select="substring-after(./text(), '&#10;')"/>
</xsl:template>
</xsl:stylesheet>

但它没有给我任何回报......任何人都可以帮助我吗?谢谢!

2 个答案:

答案 0 :(得分:1)

如果我必须使用XSLT 1.0,我会写这样的两遍转换:

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

 <xsl:template match="/*">
  <xsl:variable name="vrtfPass1">
      <xsl:copy><xsl:apply-templates/></xsl:copy>
  </xsl:variable>

  <xsl:apply-templates select=
  "ext:node-set($vrtfPass1)/*/line
       [preceding-sibling::node()[1][self::text() and normalize-space()]]"/>
 </xsl:template>

 <xsl:template match="/*/text()" name="markUp">
  <xsl:param name="pText" select="."/>

  <xsl:if test="normalize-space($pText)">
      <xsl:value-of select="substring-before(concat($pText,'&#xA;'), '&#xA;')"/>
      <line/>
      <xsl:call-template name="markUp">
        <xsl:with-param name="pText" select="substring-after($pText,'&#xA;')"/>
      </xsl:call-template>
  </xsl:if>
 </xsl:template>

 <xsl:template match="line">
Error: <xsl:value-of select="concat(position(), '&#xA;')"/>
  <xsl:value-of select="normalize-space(preceding-sibling::node()[1])"/>
  <xsl:text>&#xA;</xsl:text>
 </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<Messages>
   error message 1

   error message 2
</Messages>

产生了想要的正确结果:

Error: 1
error message 1

Error: 2
error message 2

答案 1 :(得分:1)

您可以使用此递归模板来实现此目的:

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

  <xsl:output method="html" omit-xml-declaration="yes"/>

  <xsl:template match="Messages" name="outputError">
    <xsl:param name="index" select="1"/>
    <xsl:param name="source" select="text()"/>
    <xsl:variable name="thisLine" select="normalize-space(substring-before($source, '&#10;'))"/>
    <xsl:variable name="theRest" select="substring-after($source, '&#10;')"/>
    <xsl:choose>
      <xsl:when test="$thisLine">
        <h3>Error <xsl:value-of select="$index"/>:</h3>
        <span><xsl:value-of select="$thisLine"/></span>
        <xsl:call-template name="outputError">
          <xsl:with-param name="index" select="$index + 1"/>
          <xsl:with-param name="source" select="$theRest"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$theRest">
        <xsl:call-template name="outputError">
          <xsl:with-param name="index" select="$index"/>
          <xsl:with-param name="source" select="$theRest"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

我将实际错误包装在<span>元素中,只是为了清楚地将它们与空格分开,如果您愿意,您当然可以使用<p><div>或根本没有元素。