Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
Xml 删除XSLT中单个节点的属性_Xml_Xslt - Fatal编程技术网

Xml 删除XSLT中单个节点的属性

Xml 删除XSLT中单个节点的属性,xml,xslt,Xml,Xslt,这是我的XML源代码 <?xml version="1.0" encoding="UTF-8"?> <tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/"> <GrandParent> <Parent> <Child xmlns:tns="http://www.domain.com/"> <Age>3</Age

这是我的XML源代码

<?xml version="1.0" encoding="UTF-8"?>
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/">
  <GrandParent>
    <Parent>
        <Child xmlns:tns="http://www.domain.com/">
            <Age>3</Age>
            <Gender>Male</Gender>
            <Name>Todd</Name>
        </Child>
        <Other>1234</Other>
    </Parent>
  </GrandParent>
</tns:Grand_Parent_XML>

3.
男性
托德
1234
这是我正在使用的XSLT的主体

<xsl:template match="node()|@*">
             <xsl:copy>
                    <xsl:apply-templates select="node()|@*"/>
             </xsl:copy>
       </xsl:template>
       <xsl:template match="Grand_Parent_XML">
             <xsl:element name="tns:{name()}" namespace="http://www.domain.com/">
                    <xsl:copy-of select="namespace::*"/>
                    <xsl:apply-templates select="node()|@*"/>
             </xsl:element>
       </xsl:template>

       <xsl:template match="node()|@*">
        <xsl:if test="normalize-space(string(.)) != ''">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
        </xsl:if>
        </xsl:template>

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

<xsl:template match="Child"/>

我想删除子项上的xmlns:tns=“”,并将其保存在Grand_Parent_XML中。我在这里尝试了其他建议,比如在XSLT的底部创建以下代码,但不起作用。任何帮助都将不胜感激

<xsl:template match="Child">
    <xsl:apply-templates />
</xsl:template>

试试这个:

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

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

</xsl:stylesheet>

一个简单的标识转换将删除冗余的名称空间声明,仅此而已。用萨克森6.5和9.5测试

在编辑之前,XML输入有一个名称空间声明,如

xmlns:tns=""
请注意,在XML1.0中取消名称空间的定义是非法的,但在XML1.1中是可能的。如果将文件的序言更改为以下内容,则XML解析器不会引发错误:

<?xml version="1.1" encoding="UTF-8"?>

样式表

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

    <xsl:strip-space elements="*"/>

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

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/">
  <GrandParent>
    <Parent>
        <Child xmlns:tns="http://www.domain.com/">
            <Age>3</Age>
            <Gender>Male</Gender>
            <Name>Todd</Name>
        </Child>
        <Other>1234</Other>
    </Parent>
  </GrandParent>
</tns:Grand_Parent_XML>
<?xml version="1.0" encoding="utf-8"?>
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/">
   <GrandParent>
      <Parent>
         <Child>
            <Age>3</Age>
            <Gender>Male</Gender>
            <Name>Todd</Name>
         </Child>
         <Other>1234</Other>
      </Parent>
   </GrandParent>
</tns:Grand_Parent_XML>

XML输入

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

    <xsl:strip-space elements="*"/>

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

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/">
  <GrandParent>
    <Parent>
        <Child xmlns:tns="http://www.domain.com/">
            <Age>3</Age>
            <Gender>Male</Gender>
            <Name>Todd</Name>
        </Child>
        <Other>1234</Other>
    </Parent>
  </GrandParent>
</tns:Grand_Parent_XML>
<?xml version="1.0" encoding="utf-8"?>
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/">
   <GrandParent>
      <Parent>
         <Child>
            <Age>3</Age>
            <Gender>Male</Gender>
            <Name>Todd</Name>
         </Child>
         <Other>1234</Other>
      </Parent>
   </GrandParent>
</tns:Grand_Parent_XML>

3.
男性
托德
1234
XML输出

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

    <xsl:strip-space elements="*"/>

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

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/">
  <GrandParent>
    <Parent>
        <Child xmlns:tns="http://www.domain.com/">
            <Age>3</Age>
            <Gender>Male</Gender>
            <Name>Todd</Name>
        </Child>
        <Other>1234</Other>
    </Parent>
  </GrandParent>
</tns:Grand_Parent_XML>
<?xml version="1.0" encoding="utf-8"?>
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/">
   <GrandParent>
      <Parent>
         <Child>
            <Age>3</Age>
            <Gender>Male</Gender>
            <Name>Todd</Name>
         </Child>
         <Other>1234</Other>
      </Parent>
   </GrandParent>
</tns:Grand_Parent_XML>

3.
男性
托德
1234

xmlns:tns=”“
无效。带前缀的命名空间绑定不能为空。您的输入XML格式不正确。如果不更改名称空间定义,则任何样式表都不会接受它作为输入。或者将XML版本设置为
1.1
。抱歉,我省略了原始内容。我现在就更新。我无法复制您的问题。您正在使用哪个处理器?--与您的问题无关,但是您有太多的模板,并且一些元素的规则匹配不明确。当然,这不是您真正的输入,是吗?我使用了萨克森6.5.5