【问题标题】:Processing an XSLT input file to remove particular content from XSLT处理 XSLT 输入文件以从 XSLT 中删除特定内容
【发布时间】:2018-11-21 23:34:57
【问题描述】:

我需要处理 XSLT 代码,如果 XSL 标记元素的属性值为 enable="yes",则必须从输出中删除相应的标记。

我的输入xsl文件如下,

    <xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" />


    <xsl:template match="/">   
    <xsl:for-each select="Node/Node_1">
    <node>
    <line enable="false"><xsl:value-of select="Line"/></line>
    <text><xsl:value-of select="Text"/></text>
    <desc enable="false"><xsl:value-of select="Desc"/></desc>
    <cust><xsl:value-of select="Cust"/></cust>
    </node>
    </xsl:for-each>
    </xsl:template>    
    </xsl:stylesheet> 

然后输出数据必须删除相应的具有属性 enable="false" 的 XSL 标记,

    <xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" />


    <xsl:template match="/">   
    <xsl:for-each select="Node/Node_1">
    <node>
    <text><xsl:value-of select="Text"/></text>
    <cust><xsl:value-of select="Cust"/></cust>
    </node>
    </xsl:for-each>
    </xsl:template>    
    </xsl:stylesheet> 

通过XSLT本身是否可行,将xsl文件视为XML并对其进行处理以删除具有enable =“false”属性的标签。或者有没有更好的方法来完成它?

【问题讨论】:

  • 是的,您可以这样做,但是您是否考虑过使用 XSLT 2.0 标准属性 use-when="false()" 代替?这样您就不需要进行预处理,XSLT 处理器会为您完成。

标签: xml xslt


【解决方案1】:

为此,您必须在必须使用的地方编写另一个 xslt:

  • 身份模板
  • 的模板

或者将 xsl 制作成 XML 然后使用这个 XSLT 你会得到 ouptpu:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">

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

    <xsl:template match="*[@enable = 'yes']"/>

    <xsl:template match="*[@enable = 'false']"/>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    很容易做到这一点,一个空模板&lt;xsl:template match="*[@enable = 'false']"/&gt; 与身份转换一起实现了这一点:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="3.0">
    
      <xsl:mode on-no-match="shallow-copy"/>
    
      <xsl:template match="*[@enable = 'false']"/>
    
    </xsl:stylesheet>
    

    https://xsltfiddle.liberty-development.net/jyH9rMh 是 XSLT 3 示例,在早期版本的 XSLT 中,您需要用拼写出的身份模板替换 &lt;xsl:mode on-no-match="shallow-copy"/&gt;

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

    【讨论】:

    • 谢谢,很遗憾,我不支持 XSLT 3。我使用的是 XSLT 1.0,我使用了第一条评论中发布的那个,它对我来说很有效。非常感谢.
    猜你喜欢
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2010-11-03
    • 1970-01-01
    相关资源
    最近更新 更多