【问题标题】:XSLT: preserve html tags in node value and apply templatesXSLT:在节点值中保留 html 标记并应用模板
【发布时间】:2014-08-06 10:23:36
【问题描述】:
<sectiondiv>
    <p>
        <ph linebreak="true">
            Lorem<br/>&#160;&#160;Ipsum<br/>&#160;&#160;&#160;&#160;<br/><br/><br/><br/><br/><br/><br/>
        </ph>
    </p>
    <p>
        <ph image-relation="test" linebreak="true“>
            Lorem<br/><br/>Ipsum:<br/><br/>testsentence<br/><myref kind="Variable" type="PlainText">1 684 463 394</myref ><br/><br/>
        </ph>
    </p>
</sectiondiv>

我想转换这个 XML 并保留几个节点中的 HTML 标记。 此外,我想在值上应用其他模板,例如myref 标签有自己的模板。

有什么建议可以实现吗?

【问题讨论】:

    标签: xslt xslt-1.0


    【解决方案1】:

    使用identity transformation可以实现元素的“保存”

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

    如果您想对一些标签执行额外的转换,那么将这些模板放在身份转换模板之前,如下所示:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="/">
        <xsl:apply-templates/>
      </xsl:template>
    
      <xsl:template match="myref">
        <strong>myref encountered!</strong>
      </xsl:template>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>  
      </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 这对我不起作用。我想将其他模板应用于 元素的值。
    • @Drag0ndust:你没有在你的问题中这么说。为了更改&lt;ph&gt; 元素,您可以执行类似于我对myref 模板所做的操作。但最好的是:为什么不显示您目前拥有的代码?
    • 由于 nda 问题,我无法向您展示我的转换。但是您可以看到第二个 :必须保留值中的所有 html 标记,并且必须在 myref 上应用模板
    猜你喜欢
    • 2022-01-14
    • 2019-08-13
    • 2018-06-18
    • 1970-01-01
    • 2021-04-29
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多