2014-01-16 55 views
0

我越來越瘋狂。我只想檢索我的XML中的幾個值:與我的XSLT轉換匹配錯誤

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
<s:Body> 
    <SearchDocumentResponse xmlns="http://axa.fr/gedald/2010/11"> 
    <SearchDocumentResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <DocumentCollection> 
      <document> 
       <ObjectStore i:nil="true"/> 
       <DocId>{1DC43D04-2541-459C-83A7-BA6A761C64B5}</DocId> 
       <IndexCollection> 
       <index> 
        <IndexId>P001</IndexId> 
        <Value>OBJ002301</Value> 
       </index> 
       <index> 
        <IndexId>P002</IndexId> 
        <Value>15/11/2013 13:00:00</Value> 
       </index> 
       </IndexCollection> 
      </document> 
     </DocumentCollection> 
     <Message i:nil="true"/> 
     <NbDocument>7</NbDocument> 
     <Result>OK</Result> 
    </SearchDocumentResult> 
    </SearchDocumentResponse> 
</s:Body> 
</s:Envelope> 

無論我嘗試作爲XSL文件,我都無法訪問內容。有時我會收到所有標籤內容。但從來沒有我想要的。

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!-- Edited by XMLSpy® --> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="s:Envelope/s:Body/SearchDocumentResponse/SearchDocumentResult/DocumentCollection/document/IndexCollection"> 
    <html> 
    <body> 
     <xsl:value-of select="DocId"/> 
     <xsl:for-each select="index"> 
      <ul> 
      <li><xsl:value-of select="IndexId"/></li> 
      <li><xsl:value-of select="Value"/></li> 
      </ul> 
     </xsl:for-each> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

回答

0

源文檔中的元素在不同的命名空間,所以你需要綁定同一個命名空間URI來適當前綴的樣式表,然後在你的XPath表達式中使用這些前綴:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:axa="http://axa.fr/gedald/2010/11"> 

    <xsl:template match="/"> 
    <html> 
    <body> 
     <xsl:apply-templates select="s:Envelope/s:Body/axa:SearchDocumentResponse/axa:SearchDocumentResult/axa:DocumentCollection/axa:document/axa:IndexCollection" /> 
    </body> 
    </html> 
    </xsl:template> 


    <xsl:template match="axa:IndexCollection"> 
    <xsl:value-of select="axa:DocId"/> 
    <xsl:for-each select="axa:index"> 
     <ul> 
      <li><xsl:value-of select="axa:IndexId"/></li> 
      <li><xsl:value-of select="axa:Value"/></li> 
     </ul> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

XPath 1.0表達式中未命名的名稱始終指代不在名稱空間中的節點,這就是爲什麼諸如DocId之類的東西不會選擇任何內容。

+0

完美。但我仍然不明白它是如何工作的...爲什麼我不能在SearchDocumentResponse之前放置axa,而不是在XML中? –

+0

@TDLemon,因爲這是XML名稱空間的工作方式 - XML中的'xmlns =「http://axa.fr/gedald/2010/11」'將該元素及其所有後代元素與未加前綴的名稱放在' http:// axa.fr/gedald/2010/11'命名空間。要匹配XPath 1.0中的名稱空間節點,您必須使用前綴。 –

+0

@TDLemon XSLT/XPath不關心原始XML文檔使用哪些前綴或默認命名空間聲明,重要的是您在stylesheet_中爲正確的命名空間URI聲明瞭前綴綁定,然後一致地使用這些前綴。我本來可以使用'xmlns:blah =「http://axa.fr/gedald/2010/11」'和'blah:SearchDocumentResponse',這樣可以很好地工作。 –