2017-11-18 4 views
0

xsl : for-each를 중첩 할 때 문제가있는 xml 파일을 사용하고 있습니다.xsl 중첩 루프 오류

XML 파일 :

<?xml version='1.0' encoding='utf-8'?> 
<calibredb> 
    <record> 
    <title sort="Demon Under the Microscope, The">The Demon Under the Microscope</title> 
    <authors sort="Hager, Thomas"> 
     <author>Thomas Hager</author> 
    </authors> 
    </record> 
    <record> 
    <title sort="101 Things Everyone Should Know About Math">101 Things Everyone Should Know About Math</title> 
    <authors sort="Zev, Marc &amp; Segal, Kevin B. &amp; Levy, Nathan"> 
     <author>Marc Zev</author> 
     <author>Kevin B. Segal</author> 
     <author>Nathan Levy</author> 
    </authors> 
    </record> 
    <record> 
    <title sort="Biohazard">Biohazard</title> 
    <authors sort="Alibek, Ken"> 
     <author>Ken Alibek</author> 
    </authors> 
    </record> 
    <record> 
    <title sort="Infectious Madness">Infectious Madness</title> 
    <authors sort="WASHINGTON, HARRIET"> 
     <author>Harriet A. Washington</author> 
    </authors> 
    </record> 
    <record> 
    <title sort="Poetry Will Save Your Life">Poetry Will Save Your Life</title> 
    <authors sort="Bialosky, Jill"> 
     <author>Jill Bialosky</author> 
    </authors> 
    </record> 
</calibredb> 

XSL은 :

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <html> 
    <body> 
    <h2>My Calibre Collection</h2> 
    <table border="1"> 
     <tr bgcolor="#9acd32"> 
     <th>Title</th> 
     <th>Author</th> 
     </tr> 
     <xsl:for-each select="calibredb/record"> 
     <tr> 
     <td><xsl:value-of select="title" /></td> 
     <td><xsl:for-each select="authors"><xsl:value-of select="author" /></xsl:for-each></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

여러 저자가있는 경우, 루프가 더 계속 실패 할 것으로 보인다.

누구나 내게 xsl을 올바르게 포맷하는 방법에 대한 제안을 줄 수 있습니까?

감사합니다.

답변

0

XSLT 1.0에서 xsl:value-of은 첫 번째 author 값만 제공합니다.

대신 내부에 대한-각각

<xsl:for-each select="authors/author"> 
    <xsl:value-of select="." /> 
</xsl:for-each> 

당신은 아마도 출력 쉼표 또는 다른 구분 기호 값을 구분하는 것이 좋습니다 ... authors/author을 선택하려고 ...

authors을 선택
<xsl:for-each select="authors/author"> 
    <xsl:if test="position() > 1"> 
     <xsl:text>, </xsl:text> 
    </xsl:text> 
    <xsl:value-of select="."/> 
</xsl:for-each> 
0

이 오류는 실제로 중첩 루프와 관련이 없습니다. 각 record 요소가 하나 개의authors 요소를 가지고 공지 사항 때문에 내부 xsl:for-each select="authors"가 (그것은 단지 1 회전을합니다) 필요하지 않습니다.

다른 곳, 즉 value-of 명령에 문제가 있습니다. 귀하의 사례는 XSLT 1.0value-of, 많은 XSLT 사용자가 인식하지 못하는 에 관한 이상한 기능의 예입니다. select 문구 여러 노드를 검색하더라도

, 다음 value-of 인쇄 이들의 첫 번째 아닌 전체 시퀀스 과 (IMHO)이 평균 XSLT 사용자가 무엇을 기대하지 않습니다.

늦은 시각에만 2.0이 더 직관적 인 방식으로 작성되었습니다.

value-of 2.0는 세퍼레이터 separator 속성 지정 그들 사이에 삽입이 경우 전체 시퀀스에 출력한다 XSLT에 지시. 이 버전 2.0로 이동할 수있는 경우

그래서, 그냥 쓰기 :

<xsl:for-each select="calibredb/record"> 
    <tr> 
    <td><xsl:value-of select="title" /></td> 
    <td><xsl:value-of select="authors/author" separator=", "/></td> 
    </tr> 
</xsl:for-each>