2011-02-23 62 views
2

我想從XML文件中刪除XML元素。我想要移除的標記被命名爲xfdf:field。 如何在我的xslt中指定此項?我試過這個,我得到一個錯誤說「org.apache.xpath.domapi.XPathStylesheetDOM3Exception:前綴必須解析爲名稱空間:xfdf」。刪除名稱類似xfdf:的字段(帶名稱空間)的XML標記

這是我的xslt。

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" /> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="xfdf:field"></xsl:template> 
</xsl:stylesheet> 

這是我的xml。

<xfa:datasets 
xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" 
xmlns:dd="http://ns.adobe.com/data-description/" xmlns:tns="http://hostname" 
xmlns:xfdf="http://ns.adobe.com/xfdf/"> 
    <xfa:data> 
     <tns:form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
      <tns:formHeader> 
       <tns:formId>formid</tns:formId> 
       <tns:revId>Rev123</tns:revId> 
       <tns:agencyId>agency</tns:agencyId> 
       <tns:progId>program</tns:progId> 
       <tns:serviceId>service</tns:serviceId> 
      </tns:formHeader> 
      <tns:formFields> 
       <tns:date>08-13-1967</tns:date> 
       <tns:agreementBetween></tns:agreementBetween> 
       <tns:ncr>xxxx</tns:ncr> 
       <tns:formConfirmationInfo> 
        <tns:confNbrLbl>nbrlabel</tns:confNbrLbl> 
        <tns:confNbrData>1231</tns:confNbrData> 
        <tns:custNameLbl>3332</tns:custNameLbl> 
        <tns:custNameData>dasdas</tns:custNameData> 
        <tns:dateLbl>date</tns:dateLbl> 
        <tns:dateData>01012001</tns:dateData> 
       </tns:formConfirmationInfo> 
      </tns:formFields> 
      <xfdf:field xmlns:xfdfi="http://ns.adobe.com/xfdf-transition/" 
         xfdfi:original="FSAPPLICATIONDATA_"> 
       <!--irrelevant data omitted--> 
      </xfdf:field> 
     </tns:form> 
    </xfa:data> 
</xfa:datasets> 
+0

首先幾乎重複此FAQ我發現的[XSLT與XML源,有一個默認的命名空間設置爲XMLNS](HTTP ://www.stackoverflow.com/questions/1344158/xslt-with-xml-source-that-has-a-default-namespace-set-to-xmlns) – 2011-02-23 23:13:12

回答

3

您需要在您的XSLT文件中指定的命名空間:

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

+1爲正確的答案。 – Flack 2011-02-24 07:16:59