如何消除所有<TAG/>和所有属性=“"通过 XSLT? [英] How to eliminate all <TAG/> and all attribute="" by XSLT?

查看:27
本文介绍了如何消除所有<TAG/>和所有属性=“"通过 XSLT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xsl:stylesheet 中,我有这个类似身份"的转换,以消除注释、空(终端)标签和空属性......但是第二 <代码>xsl:when 不起作用

In a xsl:stylesheet I have this "identity like" transform, to eliminate comments, empty (terminal) tags and empty attributes... But the second xsl:when not works

  <xsl:template match="node()">
  <xsl:choose>
    <xsl:when test="name()='p' and not(./*) and not(normalize-space(.))"></xsl:when>
    <xsl:when test="not(name()='img') and not(name()='br') and not(./*) and not(text())"
    ></xsl:when> <!-- this line NOT WORKS -->
    <xsl:otherwise><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:otherwise>
  </xsl:choose>
  </xsl:template>

  <xsl:template match="@*">
  <xsl:choose>
    <xsl:when test="not(normalize-space(.))"></xsl:when>
    <xsl:otherwise><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:otherwise>
  </xsl:choose>
  </xsl:template>

  <xsl:template match="comment()"></xsl:template>

在这种情况下如何表达空标签的条件?

Whow to express condition to empty tags in this context?

PS:空规则"是在这里解释,我尝试使用它,但不明白为什么不工作.

PS: the "empty rules" are explained here, I try to use it, but not see why not working.

推荐答案

空元素是指没有子节点的元素.

An empty element is an element with no child nodes.

模板匹配优先级是您的朋友...以下应该是符合您描述的身份样式表以及我认为您对图像和中断元素所做的事情.

Template match priority is your friend ... the following should be the kind of identity stylesheet that meets your description plus what I think you are doing with image and break elements.

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<!--toss these-->
<xsl:template match="comment() | 
                    *[not(node())] |
                    @*[not(normalize-space())]"/>

<!--preserve these-->
<xsl:template match="img|br" priority="1">
  <xsl:call-template name="identity"/>
</xsl:template>

<!--preserve everything else-->
<xsl:template match="@*|node()" name="identity">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这篇关于如何消除所有&lt;TAG/&gt;和所有属性=“"通过 XSLT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆