Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xslt从特定xml元素中排除属性_Xml_Xslt - Fatal编程技术网

使用xslt从特定xml元素中排除属性

使用xslt从特定xml元素中排除属性,xml,xslt,Xml,Xslt,我是xslt新手。我有以下问题。我需要在xml中,从特定元素(例如div)中删除特定属性(theAttribute)。 i、 e ... 成为 <html> <head>...</head> <body> <div id="qaz"> </div> <div id ="ddd"> <div id= "ggg"> &

我是xslt新手。我有以下问题。我需要在xml中,从特定元素(例如
div
)中删除特定属性(
theAttribute
)。 i、 e


...
成为

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

...
其中属性“剧场贡献”已被删除。我发现了这个, 在此基础上,我试图找到合适的解决方案

i、 e.

从整个文档中删除它。。。还有一些人喜欢match、if choose等。没有任何效果-(你能在这方面帮助我吗?对我来说这听起来微不足道,但使用xslt,我根本无法应付


提前感谢大家

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

如果是这样,请确保样式表中有
@theateribute
的空模板,但也有一个将所有其他内容复制到输出中的标识模板:

<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>
或此模板:

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

在select中,可以使用name函数排除(或包括)属性

例如,


我有一个类似于:

<row  attribute1="value" >some stuff</row> 
一些东西
我在属性列表的末尾添加了一个外部值的状态

\saxone\bin\Transform.exe-xsl:my_script.xsl-s:rows.xml status=“已完成”



如果您想格式化代码,您可以突出显示代码,然后单击编辑面板顶部的“010101”按钮,或者将文本缩进4个空格。您好,我也找到了这两个空格,但恐怕它们没有什么帮助。第一个选项从所有位置(div、body、font等标记)删除了“theAtribute”。第二种方法从所有id=qaz的节点中从任何地方删除@theAtribute。有没有一种方法可以让我编写??我需要的是这两种方法的混合,我想,但我无法确定格式,因为我对xslt一无所知。我知道该如何从所有div中删除theAtribute,而只从div中删除它。我明白了。我在描述中没有意识到您特别希望排除
div/@theateribute
。我已更新了答案。如果您的XML元素绑定到名称空间,则可能需要在XSLT中声明名称空间,或使用忽略名称空间并与本地名称匹配的XPATH语句(即
*[local-name()='div']/@theateribute
)对于关注者,如果要排除倍数,可以使用类似于
match=“@theAtribute |@otherAttribute”
的表达式,它们也可以是完整的xpath样式的表达式……这对我来说很有效,我想替换一个属性,然后复制其余的,而这个表达式对“其余的”也很有效
<xsl:template match="div/@theAtribute" />
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"  
xmlns:xs="http://www.w3.org/2001/XMLSchema" >

        <xsl:output method="xml" encoding="UTF-8" indent="yes" />
        <xsl:param name="status"/>

        <!--If attribute is present change it -->
        <xsl:template match="@status" >
            <xsl:attribute name="status" select="$status"/>
        </xsl:template>
        <!-- If attribute is not present add it al last position -->
        <xsl:template match="row[ not( @status ) ]" >
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <!-- put attribute in the last position -->
                <xsl:attribute name="status" select="$status"/>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </xsl:template>

        <!--identity template copies everything forward by default-->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>

    </xsl:stylesheet>
<row  attribute1="value" >some stuff</row> 
<row current_run="2019-07-29 19:00" status="completed">