繁体   English   中英

如何使用xslt保留来转换xml <br /> 并且不保留文本中的其他标签

[英]how to transform xml with xslt preserving <br /> and not preserve other tags in a text

这个问题是非常相似这一个 除了我只想保留<br />和一些<img>标记(带有class =“ visible”)。 即来自:

<example>
   <text>
      Some text with <br /> and <img src="source" /> then text .... <img class="visible" src="source" />
   </text>
</example>

拥有:

 <div class="example"> 
  <p>Some text with <br /> and then text ....  <img class="visible" src="source" /></p> 
 </div>

此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="example">
   <div class="example">
    <xsl:apply-templates/>
   </div>
 </xsl:template>

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

 <xsl:template match=
  "example/text/*
          [not(self::br)
         and
           not(self::img[@class='visible'])
          ]"/>
</xsl:stylesheet>

当应用于提供的XML文档时,会产生所需的正确结果

<div class="example"><p>
      Some text with <br/> and  then text .... <img class="visible" src="source"/></p></div>

我没有测试它,但我认为这应该为您做。

<xsl:template match="/example/text">
  <div class="example">
    <p>
      <xsl:copy-of select="@* | node()[name()='br' or (name()='img' and @class='visible')]"/>
    </p>
  </div>
</xsl:template>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM