簡體   English   中英

如何移除 <br> 標簽,並使用xslt保留所有其他標簽

[英]How to remove <br> tags and leave all other tags using xslt

我正在嘗試從xslt1.0中的字符串中刪除break標記。 我嘗試使用translate(s,'&ltbr>',''),其中s是字符串值,但這也刪除了粗體標簽。 還嘗試了一個模板,但沒有運氣。 我的問題是如何僅刪除中斷標簽。

所以字符串:

<b>trying</b> to remove <br> tags only <br> and not <b>bold tags</b>

將被解析為:

<b>trying</b> to remove tags only and not <b>bold tags</b>

此XSLT:

    <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 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=
    "br[not(@*|*|comment()|processing-instruction()) 
    and normalize-space()=''
    ]"/>
</xsl:stylesheet>

應用於格式正確,有效的XML:

<?xml version="1.0" encoding="UTF-8"?>
<list>
<b>trying</b> to remove <br/> tags only <br/> and not <b>bold tags</b>
</list>

給出以下結果:

<list>
<b>trying</b> to remove  tags only  and not <b>bold tags</b>
</list>

另請參考Dimitre的解決方案: 通過XSLT從XML刪除空標簽

最好的問候,彼得

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM