2016-02-29 63 views
0

非常感謝!XSLT不匹配 - 名稱空間tempuri.org加密

Crypto是法國房地產XML規範。我從1條記錄開始,測試XML文件。

任何人都可以告訴我,爲什麼當我從XML輸入文件中刪除加密XMLNS聲明時,爲什麼下面的XSLT有效?

命名空間XML REMOVED輸出正確

<?xml version="1.0" encoding="UTF-8"?> 
<ROOT xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/CryptML.xsd"> 
    <DESTINATAIRE>  
     <AGENCE>  
      <BIEN> 
       <REFERENCE>43</REFERENCE>   
      </BIEN>  
     </AGENCE> 
    </DESTINATAIRE> 
</ROOT> 

XML一起工作的XML

<?xml version="1.0" encoding="UTF-8"?> 
<ROOT xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <DESTINATAIRE>  
     <AGENCE>  
      <BIEN> 
       <REFERENCE>43</REFERENCE>   
      </BIEN>  
     </AGENCE> 
    </DESTINATAIRE> 
</ROOT> 

XSLT

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:for-each select="ROOT/DESTINATAIRE/AGENCE/BIEN"> 
     <COL><DATA><xsl:text>This is a test</xsl:text></DATA></COL> 
    </xsl:for-each> 
</xsl:template> 

預期的結果

<?xml version="1.0" encoding="UTF-8"?> 
    <COL> 
     <DATA>This is a test</DATA> 
    </COL> 

謝謝!

回答

0

如果在XML中有默認名稱空間,則所有元素都屬於該名稱空間。這使得它們與不在名稱空間中的元素不同,即使它們在XML中具有相同的名稱。

您的XSLT不會引用該命名空間。它正在尋找沒有名稱空間的元素,因此不會匹配屬於名稱空間的元素。

解決辦法是宣佈在您的XSLT命名空間,並使用它的元素

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl" 
           xmlns:cml="http://tempuri.org/CryptML.xsd"> 

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

<xsl:template match="/"> 
    <xsl:for-each select="cml:ROOT/cml:DESTINATAIRE/cml:AGENCE/cml:BIEN"> 
     <COL><DATA><xsl:text>This is a test</xsl:text></DATA></COL> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

需要注意的是,如果你可以使用XSLT 2.0,您可以使用「XPath的默認匹配該命名空間-namespace「屬性,它允許默認名稱空間應用於xpath表達式,不需要明確設置前綴:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl" 
xpath-default-namespace="http://tempuri.org/CryptML.xsd"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:for-each select="ROOT/DESTINATAIRE/AGENCE/BIEN"> 
     <COL><DATA><xsl:text>This is a test</xsl:text></DATA></COL> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 
+0

非常感謝。這個問題讓我頭痛不已! – TVRV8S