为什么我的 XSLT 会剥离 HTML 标签

2024-01-14

我正在使用 XSLT 1.0 将一些 XML 转换为 JSON 输出。不幸的是,我正在使用的一些 XML 中包含 HTML 标记。以下是一些 XML 输入的示例:

 <text>
 Kevin Love and Steph Curry can talk about their first-
 time starting gigs in the All-Star game Friday night when the Minnesota
 Timberwolves visit Oracle Arena to face the Golden State Warriors.
</text>
  <continue>
    <P>
 Love and Curry were two of four first-time All-Star starters when the league
 made the announcement on Thursday.
</P>
    <P>
 Love got a late push to overtake Houston Rockets center Dwight Howard in the
 final week of voting.
</P>
    <P>
 "I think it's a little sweeter this way because I really didn't expect it,"
 Love said on a conference call. "I was already humbled by the response the
 fans gave me to being very close to the top (frontcourt players). The outreach
 by the Minnesota fans and beyond was truly amazing."
</P>
</continue>

标记并不理想,我需要保留<P>我的 JSON 输出中的标签。为了处理引号,我避开了它们。这是我处理此问题的模板:

<xsl:variable name="escaped-continue">
      <xsl:call-template name="replace-string">
        <xsl:with-param name="text" select="continue"/>
        <xsl:with-param name="replace" select="'&quot;'" />
        <xsl:with-param name="with" select="'\&quot;'"/>
      </xsl:call-template>
    </xsl:variable>
     <xsl:variable name="escaped-text">
      <xsl:call-template name="replace-string">
        <xsl:with-param name="text" select="text"/>
        <xsl:with-param name="replace" select="'&quot;'" />
        <xsl:with-param name="with" select="'\&quot;'"/>
      </xsl:call-template>
    </xsl:variable>
 <xsl:template name="replace-string">
        <xsl:param name="text"/>
        <xsl:param name="replace"/>
        <xsl:param name="with"/>
        <xsl:choose>
            <xsl:when test="contains($text,$replace)">
                <xsl:value-of select="substring-before($text,$replace)"/>
                <xsl:value-of select="$with"/>
                <xsl:call-template name="replace-string">
                    <xsl:with-param name="text"
                        select="substring-after($text,$replace)"/>
                    <xsl:with-param name="replace" select="$replace"/>
                    <xsl:with-param name="with" select="$with"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
   </xsl:template>

然后我只需使用类似以下内容来输出 JSON:

{
    "text": "<xsl:value-of select="normalize-space($escaped-text)"/>", 
    "continue": "<xsl:value-of select="normalize-space($escaped-continue)"/>"
}

我这里遇到的问题是输出如下所示:

{
 "text": "Kevin Love and Steph Curry can talk about their first- time starting gigs in the All-Star game Friday night when the Minnesota Timberwolves visit Oracle Arena to face the Golden State Warriors.", 
  "continue": "Love and Curry were two of four first-time All-Star starters when the league made the announcement on Thursday. Love got a late push to overtake Houston Rockets center Dwight Howard in the final week of voting. \"I think it's a little sweeter this way because I really didn't expect it,\" Love said on a conference call. \"I was already humbled by the response the fans gave me to being very close to the top (frontcourt players). The outreach by the Minnesota fans and beyond was truly amazing.\"
}

如您所见,双引号已正确转义,但是<P>标签已被 XSLT 解析器直接剥离和/或解析,然后由normalize-space()。重新添加的最佳方法是什么<P>标签到我的输出中?


试试这样:

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

<xsl:template match="/root">
    <xsl:text>{&#10;"text": "</xsl:text>
    <xsl:apply-templates select="text/text()"/>
    <xsl:text>"&#10;"continue": "</xsl:text>
    <xsl:apply-templates select="continue/*"/>
    <xsl:text>"&#10;}</xsl:text>
</xsl:template>

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

<xsl:template match="text()">
<xsl:variable name="escaped-text">
    <xsl:call-template name="replace-string">
        <xsl:with-param name="text" select="."/>
        <xsl:with-param name="replace" select="'&quot;'" />
        <xsl:with-param name="with" select="'\&quot;'"/>
    </xsl:call-template>
</xsl:variable>
<xsl:value-of select="normalize-space($escaped-text)"/>
</xsl:template>

<xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
        <xsl:when test="contains($text,$replace)">
            <xsl:value-of select="substring-before($text,$replace)"/>
            <xsl:value-of select="$with"/>
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text"
                    select="substring-after($text,$replace)"/>
                <xsl:with-param name="replace" select="$replace"/>
                <xsl:with-param name="with" select="$with"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

应用于输入的修改版本(添加根元素和更多用于测试的标记):

<root>
    <text>
    Kevin Love and Steph Curry can talk about their first-
    time starting gigs in the All-Star game Friday night when the Minnesota
    Timberwolves visit Oracle Arena to face the Golden State Warriors.
    </text>
    <continue>
        <P>
        Love and Curry were <i>two of <b>four</b> first-time All-Star</i> starters when the league
        made the announcement on Thursday.
        </P>
        <P>
        Love got a late push to overtake Houston Rockets center Dwight Howard in the
        final week of voting.
        </P>
        <P>
        "I think it's a little sweeter this way because I really didn't expect it,"
        Love said on a conference call. "I was already humbled by the response the
        fans gave me to being very close to the top (frontcourt players). The outreach
        by the Minnesota fans and beyond was truly amazing."
        </P>
    </continue>
</root>

产生以下结果:

{
"text": "Kevin Love and Steph Curry can talk about their first- time starting gigs in the All-Star game Friday night when the Minnesota Timberwolves visit Oracle Arena to face the Golden State Warriors."
"continue": "<P>Love and Curry were<i>two of<b>four</b>first-time All-Star</i>starters when the league made the announcement on Thursday.</P><P>Love got a late push to overtake Houston Rockets center Dwight Howard in the final week of voting.</P><P>\"I think it's a little sweeter this way because I really didn't expect it,\" Love said on a conference call. \"I was already humbled by the response the fans gave me to being very close to the top (frontcourt players). The outreach by the Minnesota fans and beyond was truly amazing.\"</P>"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的 XSLT 会剥离 HTML 标签 的相关文章

随机推荐

  • 无法找到捆绑的 Java 版本。 MacBook Air M1

    我在配备 intel 的 MacBook Pro 上使用 flutter 一切正常 现在我改用配备 M1 芯片的 MacBook Air 我收到错误无法找到捆绑的 Java 版本 请问我该如何解决它 扑动医生 v Flutter 频道稳定
  • 在 Apps 脚本中设置数据验证显示样式

    是否可以在应用程序脚本中设置下拉列表显示样式 检查文档后发现 API 只允许您在 箭头 和 纯文本 之间进行选择 应用程序脚本文档 https developers google com apps script reference spre
  • 无法以编程方式在 C:\inetpub\wwwroot 中创建文件

    我在 ASP NET 网页的代码后面有一个函数 它创建一个文件 然后使用 JavaScript 命令打开它 这在 IDE 中有效 它会创建文件 询问我要在哪里保存文件 我可以保存它 等等 但是当我安装网站并测试它时 我在尝试创建网站时收到
  • Internet Explorer 8 原型和 XMLHttpRequest

    这部分是对解决方法的请求 部分是试图表明 Internet Explorer 的原型实现仍然有缺陷 以下代码在 Internet Explorer 上不起作用 XMLHttpRequest prototype old XMLHttpRequ
  • 如何关闭 Safari 的预取功能?

    Safari 有一个 功能 可以在您输入网址时预加载页面 现在对于大多数用户来说 这确实是一个功能 可以加快页面加载速度 但对于 Web 开发人员来说 它可能会带来麻烦 特别是当它自动加载您之前使用过但当前无意运行的脚本 例如导入程序或后台
  • 根据 Celery 任务状态更新 Django 模型字段

    在我的模型中 我有一个status默认值为 处理 的字段 在 Django 管理界面中 用户单击 保存 按钮后 表单输入将传递给仅休眠 30 秒的 celery 任务 30 秒后 我该如何 判断celery任务是否成功 更新模型的statu
  • 如果浏览器不是 Internet Explorer 9 或更高版本,则显示一条消息

    我想向我的用户展示一个如下所示的栏 如果 浏览器不是IE 或者 浏览器为 IE 但版本为 8 或更早 请注意 屏幕截图仅用于说明 IE 9is支持我的网站 我发现了这个不错的 jQuery 插件 但我不想使用弹出窗口 http jrejec
  • x 轴刻度日期格式和位置

    我尝试使用 matplotlib 复制最初使用 flotr2 创建的绘图图以进行 pdf 输出 我必须说 flotr 更容易使用 但除此之外 我目前一直在尝试将 x 轴上的日期 时间格式化为所需的格式 即小时 分钟 每 2 小时间隔一次 如
  • 如何恢复 .condarc 中 env_prompt 参数的默认行为?

    如果你创建一个python环境conda with prefix标记并激活它 激活后环境将通过其整个路径显示 这可能是一条很长的路 因此conda 文档 https docs conda io projects conda en lates
  • Spark 中的任务不可序列化

    我有这样的转变 JavaRDD
  • WPF 本机 Windows 10 Toast

    使用 NET WPF和Windows 10 有没有办法使用c 将本地toast通知推送到操作中心 我只看到人们为此制作自定义对话框 但必须有一种方法可以通过 os 您可以使用NotifyIcon from System Windows Fo
  • 使用 YAML 和过滤器登录 python

    想要使用 YAML 设置带有过滤器的记录器 YAML 配置文件config yaml如下 version 1 formatters simple format asctime s name s message s extended form
  • swift 中的默认初始化器

    我创建了一个带有字符串选项 字符串 的 swift 类 并在另一个 swift 文件中实例化了该类 但出现了编译错误 当我在同一文件中实例化该类时 没有错误 我做错了什么吗 我仔细检查了该行为 并且该行为即使与 swift 文档中给出的类定
  • 无法在android中调用REST API

    我正在尝试从手机调用 REST Web 服务 我正在使用以下代码来完成此操作 我只有一项具有按钮和文本视图的活动 每当我单击按钮时 它都会出现以下错误logcat AndroidRuntime at android os Handler d
  • 当我尝试运行 Yesod 书中的第一个示例时,出现“无法找到模块‘Yesod’”

    我知道这似乎重复找不到模块 Yesod https stackoverflow com questions 14589358 could not find module yesod 但与该用户不同的是 ghc pkg list不显示Yeso
  • 根据另一列的聚合将非聚合列添加到聚合数据集中

    是否可以使用聚合函数从原始数据框中添加另一列 而不实际使用该列来聚合数据 这是一个非常简化的数据版本 将有助于说明我的问题 我们称之为数据 name result 1 result 2 replicate day data for mean
  • Url.Action 如何在我不设置路由值的情况下就知道它们?

    我正在对 MVC 操作执行 jquery post 操作 该操作返回一个 json 结果 例如 Id 123 代码仍处于早期阶段 但我非常惊讶地发现 Url Action action controller 正在构建一个完整的 url我从
  • 如何使用 Google Sheets API 将文本格式设置为数字?

    我有一个 Google 表格 其中 C 列中的信息是一个以字符串形式填充的数字 Example 单元格值为 35135 22 在浏览器中 我可以突出显示 C 列并选择格式 gt 数字 gt 0 它会将字符串转换为数字 然后对其进行格式化 示
  • 在 Spark 中使用 Hive 上下文时出错:对象 hive 不是包 org.apache.spark.sql 的成员

    我正在尝试构建一个继承自 SQLContext 的 Hive Context val sqlContext new org apache spark sql hive HiveContext sc 我收到以下错误 error object
  • 为什么我的 XSLT 会剥离 HTML 标签

    我正在使用 XSLT 1 0 将一些 XML 转换为 JSON 输出 不幸的是 我正在使用的一些 XML 中包含 HTML 标记 以下是一些 XML 输入的示例