本文介绍了使用 xslt 从特定的 xml 元素中排除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 xslt 新手.我有以下问题.我需要在 xml 中从特定元素(例如 div)中删除特定属性(在示例中为 theAttribute).即

<头>...</头><身体><div id="qaz" theAtribute="44">

<div id ="ddd" theAtribute="4"><div id="ggg" theAtribute="9">

<font theAttribute="foo"/></html>

成为

<头>...</头><身体><div id="qaz">

<div id="ddd"><div id="ggg">

<font theAttribute="foo"/></html>

其中属性 theAtribute 已被删除.我找到了这个,http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.html 基于它,我试图找到合适的解决方案.

<xsl:template match="@theAtribute"/>

从整个文档中删除了它......还有其他人喜欢匹配,如果选择等.没有任何效果...... :-(你能帮我解决这个问题吗?这对我来说听起来微不足道,但是使用 xslt,我不能完全应付...

先谢谢大家

解决方案

什么不起作用?您是否想要相同的内容,只是没有 @theAtribute?

如果是这样,请确保您的样式表具有 @theAtribute 的空模板,而且还有一个标识模板,用于将其他所有内容复制到输出中:

<!--空模板禁止该属性--><xsl:template match="@theAtribute"/><!--身份模板默认复制所有内容--><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板></xsl:stylesheet>

如果您只想抑制某些@theAtribute,那么您可以使匹配条件更加具体.例如,如果您只想从 @id="qaz"div 中删除该属性,那么您可以使用此模板:

<xsl:template match="@theAtribute[../@id='qaz']"/>

或这个模板:

<xsl:template match="*[@id='qaz']/@theAtribute"/>

如果要从所有 div 元素中删除 @theAttribute,请将匹配表达式更改为:

I am new in xslt. I have the following problem. I need within an xml, to remove a specific attribute (theAttributein the example) from a specific element (e.g. div).i.e.

<html>
   <head>...</head>
   <body>
      <div id="qaz" theAtribute="44">
      </div>
      <div id ="ddd" theAtribute="4">
         <div id= "ggg" theAtribute="9">
         </div>
      </div>
      <font theAttribute="foo" />
   </body>
</html>

to become

<html>
   <head>...</head>
   <body>
      <div id="qaz">
      </div>
      <div id ="ddd">
         <div id= "ggg">
         </div>
      </div>
      <font theAttribute="foo" />
   </body>
</html>

Where attribute theAtribute has been removed. I found this,http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.html based on which i made attempts to find the proper solution.

i.e. <xsl:template match="@theAtribute" />

Which removed it from the whole document... and others like match, if choose, etc. Nothing worked.. :-( can you please help me on this? it sound trivial to me, but with xslt, i cannot cope at all...

Thank you all in advance

解决方案

What is not working? Do you want the same content, just without the @theAtribute?

If so, make sure your stylesheet has the empty template for @theAtribute, but also has an identity template that copies everything else into the output:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!--empty template suppresses this attribute-->
    <xsl:template match="@theAtribute" />
    <!--identity template copies everything forward by default-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

If you only want to suppress certain @theAtribute, then you can make the match criteria more specific. For instance, if you only wanted to remove that attribute from the div who's @id="qaz", then you could use this template:

<xsl:template match="@theAtribute[../@id='qaz']" />

or this template:

<xsl:template match="*[@id='qaz']/@theAtribute" />


If you want to remove @theAttribute from all div elements, then change the match expression to:

<xsl:template match="div/@theAtribute" />

这篇关于使用 xslt 从特定的 xml 元素中排除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 05:06