2011-09-27 40 views
1

我想用xsl樣式表將xml文件轉換爲html。xslt - 不能訪問帶屬性選擇器的curent節點

這是Java

TransformerFactory tFactory = TransformerFactory.newInstance(); 
      Transformer transformer = tFactory.newTransformer(new StreamSource(classLoader.getResourceAsStream("driving.xsl"))); 
      StreamResult drivingHtml = new StreamResult(new StringWriter()); 
      transformer.transform(new StreamSource(classLoader.getResourceAsStream("driving.xml")), drivingHtml); 
      System.out.println(drivingHtml.getWriter().toString()); 

這是一些XML的:

<?xml version="1.0" encoding="UTF-8"?> 
<user xmlns="http://notreal.org/ns1" xmlns:poi="http://notreal2.org/ns2"> 
    <address type="primary"> 
     <street>1031 Court St.</street> 
     <city>Monhegan, NY</city> 
    </address> 

    <address type="secondary"> 
     <street> Elm St.</street> 
    </address> 

這是xsl:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <html> 
      <head> 
      <title>User</title> 
      </head> 
      <body> 
         <p>Detailed Addresses</p> 
         <table> 
       <th>Primary</th> 
       <th>Secondary</th> 
          <tr> 
       <xsl:apply-templates select="/user/address"/> 
          </tr> 
         </table> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="address"> 
      <td> 
      <xsl:value-of select=".[@type='primary']/street" /> 
      <xsl:value-of select=".[@type='secondary']/street" /> 
      </td> 
      <td> 
      <xsl:value-of select=".[@type='primary']/city" /> 
      <xsl:value-of select=".[@type='secondary']/city" /> 
      </td> 
    </xsl:template> 
</xsl:stylesheet> 

當我運行的是,我得到「不能編譯樣式表「

回答

1

根據提供的XML和XSLT代碼,您的主要問題是您的代碼根本無法解決XML文檔中的元素位於默認名稱空間中的問題。

如何使用默認命名空間處理XML文檔是一個FAQ - 只需搜索xslt和xpath標記,就可以找到許多好的答案。

這裏是一個可能的解決方案

該轉化

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="http://notreal.org/ns1" 
exclude-result-prefixes="x"> 
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/*"> 
    <html> 
    <head> 
    <title>User</title> 
    </head> 
    <body> 
     <p>Detailed Addresses</p> 
     <table> 
     <thead> 
     <xsl:apply-templates select="x:address/@type"/> 
     </thead> 
     <tr> 
     <xsl:apply-templates select="x:address/x:street"/> 
     </tr> 
     <tr> 
     <xsl:apply-templates select="x:address/x:city"/> 
     </tr> 
     </table> 
    </body> 
    </html> 
</xsl:template> 

<xsl:template match="@type"> 
    <th><xsl:value-of select="."/></th> 
</xsl:template> 

<xsl:template match="x:address/*"> 
    <td><xsl:value-of select="."/></td> 
</xsl:template> 
</xsl:stylesheet> 

當在包括在問題提供的XML片段的完整和良好的XML文檔施加:

<user xmlns="http://notreal.org/ns1" 
xmlns:poi="http://notreal2.org/ns2"> 
    <address type="primary"> 
     <street>1031 Court St.</street> 
     <city>Monhegan, NY</city> 
    </address> 

    <address type="secondary"> 
     <street>203 Elm St.</street> 
     <city>Pittsburgh, PA</city> 
    </address> 
</user> 

生產(似乎是)通緝,正確的結果

<html> 
    <head> 
     <title>User</title> 
    </head> 
    <body> 
     <p>Detailed Addresses</p> 
     <table> 
     <thead> 
      <th>primary</th> 
      <th>secondary</th> 
     </thead> 
     <tr> 
      <td>1031 Court St.</td> 
      <td>203 Elm St.</td> 
     </tr> 
     <tr> 
      <td>Monhegan, NY</td> 
      <td>Pittsburgh, PA</td> 
     </tr> 
     </table> 
    </body> 
</html> 
+0

@DanielBrockman:不客氣。 –