0

我一直在尝试从 xsl 中修改父元素的文本。我怎样才能删除

XSL 代码中的元素(我不控制输入)。我只想删除前面的换行符,而不是正文中的所有换行符。前面的“这里有一些文字”可能采用多个段落的形式。

Xsl

<xsl:template match="element">
  <!-- attempting to add fix here -->

  <xsl:apply-templates />
</xsl:template>

输入

<body>
  <p>
    some text here
  </p>
  <element>
    some more text
  </element>
</body>

输出

some text here
some more text

期望的输出

some text here some more text
4

1 回答 1

1

<xsl:template match="p[following-sibling::*[1][self::element]]//text() | element[preceding-sibling::*[1][self::p]//text()">
  <xsl:value-of select="normalize-space()"/>
</xsl:template>

做你想做的事?

您不需要,<xsl:template match="element"><xsl:apply-templates/></xsl:template>因为内置模板无论如何都会这样做。

我找到了一些时间来测试代码,现在我有了

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

<xsl:output method="text"/>

<xsl:template match="p[following-sibling::*[1][self::element]]//text() |
  element[preceding-sibling::*[1][self::p]]//text()">
  <xsl:value-of select="normalize-space()"/>
</xsl:template>

<xsl:template match="text()[preceding-sibling::*[1][self::p] and following-sibling::*[1][self::element] and not(normalize-space())]">
  <xsl:text> </xsl:text>
</xsl:template>

</xsl:stylesheet>

变换

<body>
  <p>
    some text here
  </p>
  <element>
    some more text
  </element>
</body>

进入

  some text here some more text
于 2012-09-14T17:04:38.070 回答