用xsl中的空格替换结束标记

时间:2012-09-24 15:15:26

标签: xslt-2.0

我在尝试将重度嵌套的xml文件转换为文本时遇到问题。我真正想要的输出只是某些节点及其子节点的文本内容(在本例中是'secondBit'和'thirdBit'类型的节点'div')。不幸的是,有一些内部标记(span class =“bold”)需要删除,而结束标记用空格替换。这是输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<myxml>
<content>
<p class="heading">Omit this please</p>
  <div type="firstBit">
    <div class="extended"><span class="italic">this as well</span>
    </div>
    <meta name="factor"/>
  </div>
  <div type="secondBit">
    <p>
      <meta name="first" id="9"/>Some text in here.</p>
  </div>
  <div type="thirdBit">
    <div class="internal" >
      <div class="intro">
        <p class="varied">And then a <span class="bold">Button</span>can be pushed <span class="bold">without</span>any trouble</p>
      </div>
    </div>
  </div>
</content>
</myxml>

这是我到目前为止的xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:strip-space elements="*" />

<xsl:output method="text" encoding="UTF-8" indent="no"
    omit-xml-declaration="yes" />

<xsl:template match="node()">
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="div[@type= ('secondBit', 'thirdBit')]">
    <xsl:value-of select="." />
</xsl:template>

</xsl:stylesheet>

然而,这给了我输出:

  

这里有一些文字。然后一个按钮可以毫无困难地推送

虽然我想要输出(注意空格):

  

这里有一些文字。然后可以毫无困难地推送按钮

我知道我可能不得不以某种方式介绍以下xsl片段,但我不确定如何:

<xsl:template match="span[@class='bold']">
    <xsl:value-of select="normalize-space(.)"/><xsl:text> </xsl:text>
</xsl:template>

如果这不是提出这些问题的地方,我会为浪费人们的时间而道歉。

否则感谢所有的帮助。

1 个答案:

答案 0 :(得分:0)

这是我的建议:

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

<xsl:strip-space elements="*" />

<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="*[not(.//div[@type= ('secondBit', 'thirdBit')])]"/>

<xsl:template match="*[.//div[@type= ('secondBit', 'thirdBit')]] |
                     div[@type= ('secondBit', 'thirdBit')] |
                     div[@type= ('secondBit', 'thirdBit')]//*" priority="5">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="div[@type= ('secondBit', 'thirdBit')]//span[@class='bold']" priority="10">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
</xsl:template>

</xsl:stylesheet>

当我将Saxon 9.4 HE应用于您的输入样本时,我得到结果Some text in here.And then a Button can be pushed without any trouble

相关问题