【问题标题】:XSLT - Extract and manipulate portion of XML dataXSLT - 提取和操作部分 XML 数据
【发布时间】:2013-07-09 19:00:38
【问题描述】:

输入 XML:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Description><![CDATA[Audience: Andrew Reed, Senior Training Specialist, Microsoft Corporation<br/>This session is for individuals who spend significant time writing and creating documents and have some familiarity with Microsoft Word.<br/>Thanks.]]></Description>
</root>

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="yes"/>
  <xsl:template  match="/root">
    <div>
      <xsl:value-of  disable-output-escaping="yes" select="Description"/>
    </div>
  </xsl:template>
</xsl:stylesheet>

我需要在第一次出现 BR 之后添加几个 BR 标签,即在受众行之后和其他描述开始之前。 您能否修改我的 XSLT 以获得所需的输出?

所以我想要如下输出:

Audience: Andrew Reed, Senior Training Specialist, Microsoft Corporation


This session is for individuals who spend significant time writing and creating documents and have some familiarity with Microsoft Word.


Thanks.

【问题讨论】:

  • “我需要再添加几个 BR 标签” - 但您显示的示例输出中没有任何 BR 标签。您的意思是要将每个&lt;br/&gt; 元素转换为三个回车符吗?

标签: xslt


【解决方案1】:

如果您的输入数据将 &lt;br/&gt; 元素作为实际元素而不是被转义,那就太好了,这样就可以直接使用 XPath 选择它们。

但由于它们是原样,您可以使用正则表达式替换,依赖于它们将始终符合有限范围的模式的假设。您经常会被警告不要使用正则表达式一般解析 XML 或 HTML,这是正确的,因为正则表达式不是通用解决方案。但对于有限的用途,它们就足够了。

如果我猜对了您的要求,您可以使用类似

<xsl:value-of select="replace(Description, '&lt;[Bb][Rr] ?/?>',
                                           '&#10;&#10;&#10;')"/>

这将为您提供您显示的示例输出,而不是在第一次出现 BR 后添加几个 BR 标签。它会容忍一些变化,例如&lt;br&gt;&lt;BR /&gt;

这是假设您可以使用 XSLT 2.0,因为 replace() 在 1.0 中不可用。如果您仅限于 1.0,请告诉我。

【讨论】:

  • 感谢回复,我试过了,但是说replace函数未知,所以是XSLT 1.0版
  • @2177:stackoverflow.com/questions/5280079/… 的一些答案显示了如何在 XSLT 1.0 中进行字符串替换。
猜你喜欢
  • 2019-01-27
  • 1970-01-01
  • 2011-12-02
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
相关资源
最近更新 更多