如何使用xsl从xml中删除属性?

时间:2012-11-02 08:32:16

标签: xml xslt

我想从我的xml文件中删除unnessary属性。我知道我们可以通过xsl删除。但我不知道该怎么做。我在互联网上搜索我不能期待结果。

这是我当前的xml输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<rsm:Invoice xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:rsm="http://www.ita.org/" 
xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecification:2" 
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:2" 
xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:2" 
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.ita.org/ Invoice_20.xsd">
  <rsm:HeaderExchangedDocument>
    <ram:ID xmlns:ram="http://www.ita.org/">103004</ram:ID>
    <ram:TypeCode xmlns:ram="http://www.ita.org/">Invoice</ram:TypeCode>
    <ram:IssueDateTime xmlns:ram="http://www.ita.org/">2014-01-01</ram:IssueDateTime>
    <ram:CopyIndicator xmlns:ram="http://www.ita.org/">Original</ram:CopyIndicator>
    <ram:CustomsID xmlns:ram="http://www.ita.org/" />
    <ram:HeaderInformation xmlns:ram="http://www.ita.org/" />
    <ram:SummaryInformation xmlns:ram="http://www.ita.org/" />
  </rsm:HeaderExchangedDocument>
  <rsm:SpecifiedLogisticsConsignment>
    <ram:IncludedSupplyChainConsignment xmlns:ram="http://www.ita.org/">
      <ram:BorderCrossingLogisticsTransportMovement>
        <ram:ModeCode>1</ram:ModeCode>
        <ram:Mode>Sea</ram:Mode>
        <ram:ID />
      </ram:BorderCrossingLogisticsTransportMovement>
    </ram:IncludedSupplyChainConsignment>
  </rsm:SpecifiedLogisticsConsignment>
</rsm:Invoice>

我希望获得以下格式

<?xml version="1.0" encoding="UTF-8"?>
<rsm:Invoice xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:rsm="http://www.ita.org/" 
xmlns:ccts="urn:un:unece:uncefact:documentation:standard:TechnicalSpecification:2" 
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:2" 
xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:2" 
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.ita.org/ Invoice_20.xsd">
  <rsm:HeaderExchangedDocument>
    <ram:ID>103004</ram:ID>
    <ram:TypeCode>Invoice</ram:TypeCode>
    <ram:IssueDateTime>2014-01-01</ram:IssueDateTime>
    <ram:CopyIndicator>Original</ram:CopyIndicator>
    <ram:CustomsID/>
    <ram:HeaderInformation/>
    <ram:SummaryInformation/>
  </rsm:HeaderExchangedDocument>
  <rsm:SpecifiedLogisticsConsignment>
    <ram:IncludedSupplyChainConsignment>
      <ram:BorderCrossingLogisticsTransportMovement>
        <ram:ModeCode>1</ram:ModeCode>
        <ram:Mode>Sea</ram:Mode>
        <ram:ID />
      </ram:BorderCrossingLogisticsTransportMovement>
    </ram:IncludedSupplyChainConsignment>
  </rsm:SpecifiedLogisticsConsignment>
</rsm:Invoice>

我怎么能在xslt?

1 个答案:

答案 0 :(得分:0)

嗯,ram:ID正在作为命名空间“http://www.ita.org/”中的元素输出,并且您希望它作为命名空间中的元素“urn:un:unece:uncefact:data :标准:ReusableAggregateBusinessInformationEntity:2"

因此,无论生成什么代码,都会将其置于错误的命名空间中并需要修复;你没有显示你的代码,所以我们无法告诉你哪里错了。

谷歌搜索答案的麻烦是,如果你不理解你正在使用的语言的概念,你会谷歌错误的东西。您的问题不是删除属性,而是更正元素名称。忘了谷歌一段时间,忘了编码,而是读一本关于XSLT的好书。

相关问题