2016-11-24 134 views
0

我有一個XML格式的變量,我想格式化。在變量中,它是帶有標籤的HTML。我們的系統產生太多的p標籤,所以我想刪除所有<p>並關閉</p>。 p標籤之間有文本,我想留下來,只需刪除p和/ p標籤即可。XSL替換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> 
+0

是'description'與雙文本節點轉義HTML片段?無論如何,爲什麼你不註冊一個用戶定義的函數並使用它來完成這項工作? – ThW

+0

請提供一個輸入和預期輸出的例子 - 請參閱:[mcve] –

+0

輸入

很多文字。

很多文字.2

預計: 很多文字。 很多文字 – LarkoJr

回答

0

是否與以下解決一個問題:

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

所以,你會匹配p標籤都有效,只會處理他們的子節點,例如像文字和標籤。

0

覆蓋以這種方式身份規則

<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>