使用 XSL 封装 HTML 中的单词

2024-01-02

我需要在 HTML 文档中用标签(例如 span)包装每个单词,例如:

<html>
<head>
    <title>It doesnt matter</title>
</head>
<body>
         <div> Text in a div </div>
         <div>
    Text in a div
    <p>
        Text inside a p
    </p>
     </div>
</body>
</html>

得到这样的结果:

<html>
<head>
    <title>It doesnt matter</title>
</head>
<body>
         <div> <span>Text </span> <span> in </span> <span> a </span> <span> div </span> </div>
         <div>

             <span>Text </span> <span> in </span> <span> a </span> <span> div </span>                     
             <p>
               <span>Text </span> <span> in </span> <span> a </span> <span> p </span> 
             </p>
     </div>
</body>
</html>

保持身体结构很重要...

有什么帮助吗?


下面所有三种不同的解决方案都使用 XSLT 设计模式来覆盖身份规则 http://www.dpawson.co.uk/xsl/sect2/identity.html一般保留 XML 文档的结构和内容,并且仅修改特定节点。

一、XSLT 1.0解决方案:

这个简短而简单的转变 (no <xsl:choose>在任何地方使用):

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

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

 <xsl:template match="*[not(self::title)]/text()"
               name="split">
  <xsl:param name="pText" select=
       "concat(normalize-space(.), ' ')"/>

  <xsl:if test="string-length(normalize-space($pText)) >0">
   <span>
   <xsl:value-of select=
        "substring-before($pText, ' ')"/>
   </span>

   <xsl:call-template name="split">
    <xsl:with-param name="pText"
         select="substring-after($pText, ' ')"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

当应用于提供的 XML 文档时:

<html>
    <head>
        <title>It doesnt matter</title>
    </head>
    <body>
        <div> Text in a div </div>
        <div>
         Text in a div
            <p>
             Text inside a p
         </p>
        </div>
    </body>
</html>

产生想要的正确结果:

<html>
   <head>
      <title>It doesnt matter</title>
   </head>
   <body>
      <div>
         <span>Text</span>
         <span>in</span>
         <span>a</span>
         <span>div</span>
      </div>
      <div>
         <span>Text</span>
         <span>in</span>
         <span>a</span>
         <span>div</span>
         <p>
            <span>Text</span>
            <span>inside</span>
            <span>a</span>
            <span>p</span>
         </p>
      </div>
   </body>
</html>

二. XSLT 2.0解决方案:

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

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

 <xsl:template match="*[not(self::title)]/text()">
  <xsl:for-each select="tokenize(., '[\s]')[.]">
   <span><xsl:sequence select="."/></span>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于同一个 XML 文档(如上)时,会再次生成正确的所需结果:

<html>
   <head>
      <title>It doesnt matter</title>
   </head>
   <body>
      <div>
         <span>Text</span>
         <span>in</span>
         <span>a</span>
         <span>div</span>
      </div>
      <div>
         <span>Text</span>
         <span>in</span>
         <span>a</span>
         <span>div</span>
         <p>
            <span>Text</span>
            <span>inside</span>
            <span>a</span>
            <span>p</span>
         </p>
      </div>
   </body>
</html>

三、解决方案使用FXSL http://fxsl.sf.net:

使用str-split-to-wordsFXSL 的模板/函数可以轻松实现更复杂的标记化——在任何版本的 XSLT 中:

让我们有一个更复杂的 XML 文档和标记化规则:

<html>
    <head>
        <title>It doesnt matter</title>
    </head>
    <body>
        <div> Text: in a div </div>
        <div>
         Text; in; a. div
            <p>
             Text- inside [a] [p]
         </p>
        </div>
    </body>
</html>

这里有多个分隔符指示单词的开始或结束。在此特定示例中,分隔符可以是:" ", ";", ".", ":", "-", "[", "]".

以下转换使用 FXSL 进行更复杂的标记化:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
 exclude-result-prefixes="ext">

   <xsl:import href="strSplit-to-Words.xsl"/>

   <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
   <xsl:strip-space elements="*"/>

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

    <xsl:template match="*[not(self::title)]/text()">
      <xsl:variable name="vwordNodes">
        <xsl:call-template name="str-split-to-words">
          <xsl:with-param name="pStr" select="normalize-space(.)"/>
          <xsl:with-param name="pDelimiters" 
                          select="' ;.:-[]'"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
    </xsl:template>

    <xsl:template match="word[string-length(normalize-space(.)) > 0]">
      <span>
        <xsl:value-of select="."/>
      </span>
    </xsl:template>
</xsl:stylesheet>

并产生想要的正确结果:

<html>
   <head>
      <title>It doesnt matter</title>
   </head>
   <body>
      <div>
         <span>Text</span>
         <span>in</span>
         <span>a</span>
         <span>div</span>
      </div>
      <div>
         <span>Text</span>
         <span>in</span>
         <span>a</span>
         <span>div</span>
         <p>
            <span>Text</span>
            <span>inside</span>
            <span>a</span>
            <span>p</span>
            <word/>
         </p>
      </div>
   </body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 XSL 封装 HTML 中的单词 的相关文章

随机推荐

  • 数据表分页不起作用?

    我的 html 页面包含一个表格 我使用 dataTable 插件进行分页 1 https i stack imgur com O2C1e png 1 https i stack imgur com O2C1e pnghttps datat
  • 在 cython 中使用 typedef'd 结构

    我在头文件 dcm h 中有以下定义 typedef struct double alpha double gamma double tau ThetaDCM 我想将它导入到 cython 中 所以我有 cdef extern from d
  • 如何将 Object.values 与打字稿一起使用?

    我正在尝试从对象中形成逗号分隔的字符串 const data Ticket 1 pdf 8e6e8255 a6e9 4626 9606 4cd255055f71 pdf Ticket 2 pdf 106c3613 d976 4331 ab0
  • 如何生成WM_SEC.AUTH_SIGNATURE?

    有谁有可以为 Walmart API 生成此标头的 python 代码吗 WM SEC AUTH SIGNATURE 我试图理解 java 示例 但我没有任何运气 因为我没有 Java 经验 如果有人知道需要签名的字符串的格式 我可能可以从
  • iPad ios 8.4 中不会触发窗口 onload 事件

    我面临以下奇怪的问题 功能 当我打开我的网站页面时 该页面包含许多图像并使用 Javascript jQuery 作为客户端功能 单击每个图像时 所有其他图像都会更改其不透明度 并且所选图像会显示 div 包含一些信息和图像的视频 我使用了
  • Android:在自定义适配器中调用 getView() 两次

    我正在将自定义 SimpleCursorAdapter 设置为 ListView 由于某种原因 FriendAdapter 的 getView 会针对数据库中的每个项目调用两次 经过一番调查 我的 contact list xml 中没有w
  • php的file_get_contents是否忽略文件锁定?

    我读过 php 的手册页 http php net manual en function file get contents php 120389关于 file get contents 函数 它没有说明 file get contents
  • Firebase 检索最高 100 分

    This is a screen shot of my firebase I am trying to retrieve the highest 100 score in firebase database I am using this
  • 2008 年和 2010 年使用实体框架有什么区别

    您知道 首先实体框架是随 Visual Studio 2008 SP1 一起提供的 现在它随 Visual Studio 2010 一起提供 问题是 这两个版本有什么区别呢 持久性无知 您可以定义自己的 POCO 普通旧 CLR 对象 它们
  • MSI WIX:使用 2 个 MSI 创建次要升级补丁

    有没有办法根据旧的 MSI 安装程序和新的 MSi 安装程序创建次要升级 补丁文件 即 msp 或 msi 我们希望为客户提供次要升级补丁 msp 或 msi0 其中仅包含更改 1 我基于 4 个 wxs 文件创建了 旧的 Test msi
  • 用行均值替换 NA 值

    我想替换通过以下方式获取的矩阵中的 NA 值 read table 这些值应该是相应行的平均值 即表的以下行 1 2 1 NA 2 1 1 2 会成为 1 2 1 1 43 2 1 2 谢谢 这是一些示例数据 m lt matrix 1 1
  • Javascript 显示非常大的数字而不是显示 xe+n [重复]

    这个问题在这里已经有答案了 我的 JavaScript 代码经常输出非常大的数字 我希望完全显示这些数字 而不是获取诸如2 7934087356437704e 56我希望它显示完整的数字 在 JS 中可以实现这一点吗 如此大的数字 你会失去
  • gwt maven war插件配置问题

    我正在 Maven 中开发 gwt 应用程序 在这里我使用 Maven War 插件 一切正常 当我给予MVN安装命令它构建abc war目标文件夹中的文件 但它不是复制已编译的 javascript 文件 module1 and modu
  • 错误:定义表达式时出现“意外符号”

    我正在上一门统计和数据分析课 最近开始使用 R 我收到一条错误消息 到目前为止我还无法准确确定错误是什么或如何修复它 我们得到了绘制该函数的指示 y 0 1x 4 0 5x 3 x 2 3x 2 下一条指令要求遵循此编码并输入上述函数 gt
  • jquery deferred - 等待两个调用完成

    我正在寻找一种在两个 ajax 调用完成后进行回调的方法 when call1 call2 always function Here I want to be sure the two calls are done and to get t
  • 使用facet_wrap显示多个直方图

    样本数据 df lt data frame id rep 1 6 each 50 x rnorm 50 6 mean 10 sd 5 y rnorm 50 6 mean 20 sd 10 z rnorm 50 6 mean 30 sd 15
  • 将 xtable 与 longtable 选项一起使用时重复标头

    在使用 longtable 选项生成 xtable 时 有没有办法重复顶行 设置标题 例如 如果我有 tableSb lt xtable df caption A Very Long Table label ALongTable print
  • 处理不是 Action 请求的 Multipart 请求?

    我一直在想是否可以处理不是操作请求的多部分请求 对我来说这似乎不可能是有原因的 只有 ActionRequest 实现 getFile 类方法 我不能 找到任何简单的方法来获取文件 出于除 Action 之外的请求 要求 如果我不使用 ht
  • 不在 UI 线程中触发 PropertyChanged 会产生哪些副作用?

    如果您实现 INotifyPropertyChanged 您可以在非 UI 线程中引发事件 我应该避免这种情况吗 为什么 Update 这是关于 wpf 应用程序中的绑定 不 你不应该避免这个 WPF 元帅PropertyChanged代表
  • 使用 XSL 封装 HTML 中的单词

    我需要在 HTML 文档中用标签 例如 span 包装每个单词 例如 div Text in a div div div Text in a div p Text inside a p p div 得到这样的结果 div span Text