【问题标题】:XSL replace P tagsXSL 替换 P 标签
【发布时间】:2016-11-24 13:51:57
【问题描述】:

我有一个要格式化的 XML 变量。在变量中,它是带有标签的 HTML。我们的系统产生了太多的p标签,所以我想删除所有<p>并关闭</p>。 p标签之间有文字,我想留下来,去掉p和/p标签就行了。

 <xsl:value-of select="php:functionString('str_replace',$remove,'',normalize-space(php:functionString('html_entity_decode',description)))" />

我试过这个,但这只是删除一个 p 而不是关闭一个。

什么是最好的解决方案?

这是整个模板。我如何实现它: 谢谢您的回答。我对 XSL 完全陌生,但我没有设法让它工作。这是我的代码部分。我想从变量描述中删除 p 标签。

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

<xsl:template match="/">

<products>
<xsl:for-each select="objects/object">
<xsl:element name="name"><xsl:value-of select="name"/></xsl:element>
<xsl:element name="url"><xsl:value-of select="product_url"/></xsl:element>
<xsl:element name="description"><xsl:value-of select="description"/>           </xsl:element>
</products>
</xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • description 是带有双转义 HTML 片段的文本节点吗?无论如何,您为什么不注册一个用户定义的函数并使用它来完成工作?
  • 请提供输入示例和预期输出 - 请参阅:minimal reproducible example
  • 输入

    大量文本。

    大量文本2

    预期:大量文本。很多文字。2

标签: php xml xslt replace


【解决方案1】:

以下解决方案是否有问题:

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

因此,您将匹配 p-tags 并且只会处理它们的子节点,例如文本和标签。

【讨论】:

    【解决方案2】:

    覆盖 身份规则 以这种方式

    <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="p"><xsl:apply-templates/></xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      相关资源
      最近更新 更多