简体   繁体   English

xslt删除xml中的所有属性

[英]xslt to remove all the attributes from xml

I have the below sample xml 我有以下示例XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<type>
<subtype id="1">
    <Shoebox>
        <author index="0">BUILTIN\Administrators</author>
        <dateModified index="0">2001-02-23T11:30:38.000
        </dateModified>
        <title index="0">false</title>
        <sourceLocation index="0">\\vms2\TestData\Filesystem\1 
                     doc
        </sourceLocation>
        <keywords index="0">doc1</keywords>
        <contentSize index="0">123</contentSize>
        <department index="0">Windows 7</department>
        <fileName index="0">doc1.docx</fileName>
        <dateCreated index="0">2001-02- 
23T11:30:38.000</dateCreated>
        <format index="0">docx</format>
    </Shoebox>      
</subtype>  
</type>    

and below is my xslt 以下是我的xslt

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns="urn:philips:en:xsd:Trailbalance.1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="no"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="node()">
        <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
    </xsl:template>
    <xsl:template match="type">
        <xsl:element name="trackwise">
            <xsl:apply-templates select="subtype"/>
        </xsl:element>  
    </xsl:template>
        <xsl:template match="subtype">      
        <xsl:for-each select="Shoebox">      
        <capa>      
          <xsl:copy-of select="node()" />
        </capa>
        </xsl:for-each>
        </xsl:template>

The expect xml is as below 期望的xml如下

<?xml version="1.0" encoding="UTF-8"?>
<trackwise>
<capa>
<author>BUILTIN\Administrators</author>
<dateModified>2001-02-23T11:30:38.000           </dateModified>
  <title>false</title>
<sourceLocation>\\vms2\TestData\Filesystem\1 doc    </sourceLocation>
<keywords>doc1</keywords>
<contentSize >123</contentSize>
<department>Windows 7</department>
<fileName>doc1.docx</fileName>
<dateCreated >2001-02-23T11:30:38.000</dateCreated>
<format>docx</format>
</capa>
</trackwise>

the problem with above xslt is the attributes "index" is also copied, I want to remove the "index" attributes from all the child nodes, What am i doing wrong in above xslt. 上面的xslt的问题是属性“索引”也被复制了,我想从所有子节点中删除“索引”属性,在上面的xslt中我做错了什么。

If you want to remove all attribute then use: 如果要删除所有属性,请使用:

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

See Link: http://xsltransform.net/nbiCsYY 请参阅链接: http : //xsltransform.net/nbiCsYY

AND

If you want to remove all index attribute then use: 如果要删除所有index属性,请使用:

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

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

See link: http://xsltransform.net/nbiCsYY/2 请参阅链接: http : //xsltransform.net/nbiCsYY/2

first, build an identity template: 首先,构建一个身份模板:

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

if you want to delete all @index attributes, you can use 如果要删除所有@index属性,可以使用

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

also, instead of for-each, you can use direct template matches to achieve the results that you want 此外,您可以使用直接模板匹配来获得所需的结果,而不是逐个进行

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

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

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

The whole stylesheet is as follows: 整个样式表如下:

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

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

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

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

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

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

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

</xsl:stylesheet>
<capa>      
<xsl:apply-templates select="node()"/>
</capa>
use apply-template instead of copy-of element

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM