简体   繁体   English

XSLT从XML中删除特定属性

[英]XSLT to remove particular attribute from XML

i am new to XSLT need help in removing an attribute from XML. 我是XSLT的新手,需要帮助来从XML中删除属性。 Nothing to be changed in the output from XSLT, except removing the attribute 除了删除属性外,XSLT的输出中没有任何更改。

xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 
pain.001.001.03.xsd" 

from the 'Document' 来自“文档”

<?xml version="1.0" encoding="utf-8"?>
<Document 
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 
pain.001.001.03.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<CstmrCdtTrfInitn>

I think you want to remove the namespace from the root element, try the below code to remove the namespace attributes 我认为您想从根元素中删除名称空间,请尝试以下代码以删除名称空间属性

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

  <xsl:template match="*">
    <!-- remove element prefix -->
    <xsl:element name="{local-name()}">
      <!-- process attributes -->
      <xsl:for-each select="@*">
        <!-- remove attribute prefix -->
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

You have to include in your script an empty template for the attribute to omit: 您必须在脚本中包含一个空模板 ,该属性才能省略:

<xsl:template match="@*[name() = 'xsi:schemaLocation']"/>

To copy the source without any other changes, you need also the identity template . 要复制源而不进行任何其他更改,还需要标识模板

For a working example see http://xsltransform.net/bEJaofE 有关工作示例,请参见http://xsltransform.net/bEJaofE

Edit 编辑

The above template will actually remove every xsi:schemaLocation attribute, regardless of its position in the document. 上面的模板实际上将删除每个 xsi:schemaLocation属性,无论其在文档中的位置如何。 If you want to remove this attribute only from the root node , change the template to: 如果只想从根节点删除此属性,请将模板更改为:

<xsl:template match="/*/@*[name() = 'xsi:schemaLocation']"/>

Note added /*/ to match only attributes of the root node, whatever its name. 注意添加了/*/以仅匹配根节点的属性,无论其名称如何。

Is better to use xsl:copy-of /> , try below 最好使用xsl:copy-of /> ,请尝试以下

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

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

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