3

我在我的 xhtml 的命名空间 x 中定义了一对自定义的自结束标签 s1 和 s2。对于具有相同 id 的每个标签对 s1、s2,我想将 span 标签添加到它们之间的所有文本节点。每个 s1、s2 标签对都有一个唯一的 id。我正在寻找基于 XSL 的解决方案。我正在为 XSL 使用 Saxon java 处理器。

样本输入:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>This is my title</title> 
</head> 
<body> 
<h1 align="center"> 
  This is my heading 
</h1> 
<p> 
  Sample content Some text here. Some content here. 
</p> 
<p> 
   Here you go. 
</p> 
</body> 
</html> 

样本输出:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>This is my title</title> 
</head> 
<body> 
<h1 align="center"> 
  This <span class="spanClass" id="1">is my</span>heading 
</h1> 
<p> 
  Sample content <span class="spanClass" id="2">Some text here. Some content here.</span> 
</p> 
<p> 
   <span class="spanClass" id="3">Here you</span>go. 
</p> 
</body> 
</html> 
4

2 回答 2

4

这种转变

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

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="text()[(preceding::s1 | preceding::s2)[last()][self::s1]]">
  <span class="spanClass" id="{generate-id()}">
   <xsl:copy-of select="."/>
  </span>
 </xsl:template>

 <xsl:template match="s1|s2"/>
</xsl:stylesheet>

当应用于原始 XML 文档时(更正为格式正确):

<a>
  <b>Some <s1 id="1" />text here</b>
  <c>Some <s2 id="1"/>more text <s1 id="2"/> here</c>
  <d>More data</d>
  <e>Some <s2 id="2" />more data</e>
</a>

产生想要的输出:

<a>
  <b>Some <span class="spanClass" id="IDANI2QB">text here</span></b><span class="spanClass" id="IDAOI2QB">
  </span><c><span class="spanClass" id="IDAQI2QB">Some </span>more text <span class="spanClass" id="IDAWI2QB"> here</span></c><span class="spanClass" id="IDAXI2QB">
  </span><d><span class="spanClass" id="IDAYI2QBIDAYI2QB">More data</span></d><span class="spanClass" id="IDAZI2QB">
  </span><e><span class="spanClass" id="IDA1I2QB">Some </span>more data</e>
</a>
于 2010-04-21T20:16:12.810 回答
2

编辑:修改答案以使用您添加的 XHTML 示例。

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:x="http://www.w3.org/1999/xhtml" 
  xmlns="http://www.w3.org/1999/xhtml" 
  exclude-result-prefixes="x"
>
  <xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" />

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()[normalize-space()]">
    <xsl:choose>
      <xsl:when test="preceding::x:s1[1][
        not(following::x:s2[1][
          following::text()[generate-id() = generate-id(current())]
        ])
      ]">
        <span class="spanClass" id="{generate-id()}">
          <xsl:copy-of select="." />
        </span>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="." />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="x:s1|x:s2" />

</xsl:stylesheet>

结果(为了便于阅读,换行/缩进):

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>This is my title</title>
  </head>
  <body>
    <h1 align="center">
      This <span class="spanClass" id="IDATA2Q">is my</span>heading
    </h1>
    <p>
      Sample content <span class="spanClass" id="IDA2A2Q">Some text here. Some content here.</span>
    </p>
    <p>
       <span class="spanClass" id="IDA5A2Q">Here you</span>go.
    </p>
  </body>
</html>
于 2010-04-21T19:55:08.493 回答