删除文档节点上的属性

时间:2014-11-02 14:56:40

标签: xml xslt

我必须删除xsi:schemaLocation但是我没有让它工作, 我认为我在wt中做错了:Envelope / wt:Body / wt:MessageParts / * [not(name()='xsi:schemaLocation')]定义,但不知道如何解决这个问题

xml来源:

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
  <Header>
    <MessageId>{AAE4C191-6D18-4823-A29D-9C6CA68113C2}</MessageId>
    <Action>http://tempuri.org/VendPaymentsService/find</Action>
  </Header>
  <Body>
   <MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
     <Document xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd" 
          xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <CstmrCdtTrfInitn>
          ....
        </CstmrCdtTrfInitn>
      </Document>
    </MessageParts>
  </Body>
</Envelope>

我的xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:wt="http://schemas.microsoft.com/dynamics/2011/01/documents/Message" 
                xmlns:s1="http://schemas.microsoft.com/dynamics/2008/01/documents/VendPayments" 
                version="1.0">
  <xsl:output method="xml" encoding="iso-8859-1" />
  <xsl:template match="/">
    <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0" encoding="utf-8"?&gt;</xsl:text>
    <xsl:copy-of select="wt:Envelope/wt:Body/wt:MessageParts/*[not(name() ='xsi:schemaLocation')]" />
  </xsl:template>
</xsl:stylesheet>

结果

<?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="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CstmrCdtTrfInitn>
..
</CstmrCdtTrfInitn>
</Document>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

<xsl:copy-of>执行深层复制;您不能排除单个节点。另请注意,xsi:schemaLocation属性,而不是元素

也许尝试这种方式?

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wt="http://schemas.microsoft.com/dynamics/2011/01/documents/Message" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="/">
    <xsl:apply-templates select="wt:Envelope/wt:Body/wt:MessageParts/*" />
</xsl:template>

<xsl:template match="@xsi:schemaLocation"/>

</xsl:stylesheet>

注意
你应该从不来使用这样的黑客:

<xsl:text disable-output-escaping="yes">&lt;?xml version="1.0" encoding="utf-8"?&gt;</xsl:text>

如果您希望输出具有utf-8编码,请在<output>元素中说明。