2011-10-22 3 views
1

이 내 xml 파일내 XSLT의 오류는 무엇입니까?

<html xmlns="http://www.w3schools.com"> 
    <body> 
     <shelf> 
      <cd id="el01"> 
       <artist>Elton John</artist> 
       <title>Circle of Life</title> 
       <country>UK</country> 
       <company>Spectrum</company> 
       <price>10.90</price> 
       <year>1999</year> 
       <description>As heard in the Lion King.</description> 
      </cd> 

      <book id="bk101"> 
       <author>Gambardella, Matthew</author> 
       <title>XML Developer's Guide</title> 
       <genre>Computer</genre> 
       <price>44.95</price> 
       <publish_date>2000-10-01</publish_date> 
       <description>An in-depth look at creating applications 
     with XML. 
       </description> 
      </book> 

      </shelf> 
    </body> 
</html> 

입니다 이건 내 XSL 파일입니다

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="html"/> 

<!-- TODO customize transformation rules 
    syntax recommendation http://www.w3.org/TR/xslt 
--> 
<xsl:template match="/"> 
    <html> 
     <head> 
      <title>The Shelf</title> 
     </head> 
     <body> 
      <h1>The Shelf</h1> 
      <xsl:apply-templates select="shelf"/> 
     </body> 
    </html> 
</xsl:template> 
<xsl:template match="//shelf"> 
    <xsl:for-each select="cd|book"> 
     <xsl:value-of select="title"/> 
    </xsl:for-each> 
</xsl:template> 

내 출력이 브라우저에서 그냥 "선반"입니다. 내가 어디서 잘못한거야?

답변

6

두 가지 문제가 있습니다.

  1. 귀하의 데이터는 그러나 당신이 당신 XSLT이 선언하여 사용할 수 없습니다, 네임 스페이스 "http://www.w3schools.com"가 - 때문에 XML/XPath는 사양의 불일치로 선택기도 변경해야합니다. XML 문서의 기본 이름 공간과 일치하는 '데이터'접두어를 선언 한 다음 모든 xpath를 변경하여 일치하도록 선택했습니다. 불행히도 기본 네임 스페이스는 xpath에서 작동하지 않으므로 기본 네임 스페이스 만 사용할 수는 없습니다. (또는 XML 문서에서 기본 네임 스페이스를 제거 할 수 있지만 항상 옵션이 아닐 수도 있습니다).

  2. 선반 선택기가 '/'를 기준으로 일치하는 노드를 찾지 않습니다. 초기 적용 템플릿을 // data : shelf로 변경하여 문서의 어느 위치에서나 찾을 수있는 모든 데이터 : 선반 노드를 일치시킵니다.

시도 다음은 작동하지 않습니다

<xsl:stylesheet xmlns:data="http://www.w3schools.com" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="html"/> 

<!-- TODO customize transformation rules 
syntax recommendation http://www.w3.org/TR/xslt 
--> 
<xsl:template match="/"> 
<html> 
    <head> 
     <title>The Shelf</title> 
    </head> 
    <body> 
     <h1>The Shelf</h1> 
     <xsl:apply-templates select="//data:shelf"/> 
    </body> 
</html> 
</xsl:template> 
<xsl:template match="//data:shelf"> 
<xsl:for-each select="data:cd|data:book"> 
    <p><xsl:value-of select="data:title"/></p> 
</xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 
+0

덕분에 .. – rgksugan

+0

하나를 작동하지 않았다 모든 레벨에서 모든 일치하는 노드를 찾음으로써 큰 ​​문서에서는 비효율적 일 수 있기 때문에 일반적으로'/'단축키를 사용하는 것은 좋지 않습니다. 알고 있으면 실제 경로로가는 것이 더 좋습니다. –

0

select = "shelf"가 잘못되었습니다. 방금 제거하면 작동해야합니다. 또는 select = "html/body/shelf"를 시도하십시오.

+0

... – rgksugan

0

라인 <xsl:apply-templates select="shelf"/>은 루트 노드의 컨텍스트 (예 : html/shelf)에 적용됩니다. 이 레벨에는 선반 노드가 없습니다.

행을 <xsl:apply-templates select="body/shelf"/>으로 변경하면 충분합니다.

+0

네임 스페이스를 안보에 대한 :-(.. 작동 mike..it – rgksugan