2012-12-03 3 views
2

저는 XSLT로 놀기 시작했으며 몇 가지 문제가 있습니다. 필자는 일련의 가치가있는 선택 지침을 사용하여 XML 문서를 출력했지만, XSLT 템플릿을 직접 작성하는 데 어려움을 겪고 있습니다. 여기 XSLT 템플릿 작성

내 XML : 그것은 현재 서 그런 다음

<?xml version="1.0" ?> 
<?xml-stylesheet type="text/xsl" href="lecturers.xsl"?> 
<lecturers> 
<lecturer> 
     <name> 
      <title>Professor</title> 
     <first>Peter </first> <last>Quirk</last> 
     </name> 
     <teaching> 
     <course code="CO3070">XML and the Web</course> 
     <course code="CO3300"> Web Server Architectures</course> 
    </teaching> 
    <research> 
     The application of Web protocols to Biology 
    </research> 
</lecturer> 

<lecturer> 
    <name> 
    <title>Doctor</title> 
    <first>Brian </first> <last>Johnson</last> 
    </name> 
    <teaching> 
     <course code="CO9999">Computer Hacking</course> 
     <course code="CO3300"> Web Server Architectures</course> 
    </teaching> 
    <research> 
     Investigating the various complexities of Computer Hacking 
    </research> 
</lecturer> 

이 내 XSL입니다 :

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

<xsl:template match="/"> 
<html> 
<head> 
<title>XML Week 7</title> 
</head> 

    <body> 
    <h1>Week 7: Lecturers file turned to XSL:Template</h1> 

<table border="1"> 
<tr> 
    <th><b>Title</b></th> 
    <th><b>Name</b></th> 
    <th><b>Teaching</b></th> 
    <th><b>Research</b></th> 
</tr> 

<tr> 

    <td><xsl:value-of select="lecturers/lecturer/name/title" /></td> 
    <td><xsl:value-of select="lecturers/lecturer/name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="lecturers/lecturer/name/last" /></td> 
    <td><xsl:value-of select="lecturers/lecturer/teaching/course" /> and <xsl:value-of select="(lecturers/lecturer/teaching/course)[2]" /></td> 
    <td><xsl:value-of select="lecturers/lecturer/research" /></td> 

</tr> 
</table> 

</body> 

</html> 
</xsl:template> 
</xsl:stylesheet> 

이 출력에게 내가 테이블에 필요한 정보를 않습니다,하지만 난 강사 요소를 보유 할 새 템플릿을 만든 다음 해당 요소의 과정 하위를 만드는 것이 좋습니다. 나는 머리 속에있는 것들을 지나치게 복잡하게 만들지 만, 템플릿 중 하나에 템플리트를 적용하려고 할 때마다 브라우저에서 구문 분석 오류가 발생하기 때문에 작동하지 않습니다. 그래서, 나에게 어떤 조언이 있니? 많이 감사 할 것입니다, 심지어 나의 예제에서 작동하게하는 방법을 설명하는 몇 가지 기본 예제가 우수 할 것입니다. 건배.

답변

2

하나의 템플릿을 사용하여 결과 문서 구조를 생성합니다.

<xsl:template match="/"> 
<html> 
<head> 
<title>XML Week 7</title> 
</head> 

    <body> 
    <xsl:apply-templates/> 

</body> 

</html> 
</xsl:template> 

그런 다음 추가 템플릿을 작성하여 예를 들어.

<xsl:template match="lecturers"> 
    <h1>Week 7: Lecturers file turned to XSL:Template</h1> 

<table border="1"> 
<tr> 
    <th><b>Title</b></th> 
    <th><b>Name</b></th> 
    <th><b>Teaching</b></th> 
    <th><b>Research</b></th> 
</tr> 
    <xsl:apply-templates/> 
</table> 
</xsl:template> 

<xsl:template match="lecturer"> 
<tr> 

    <td><xsl:value-of select="name/title" /></td> 
    <td><xsl:value-of select="name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="name/last" /></td> 
    <td><xsl:value-of select="teaching/course" /> and <xsl:value-of select="(teaching/course)[2]" /></td> 
    <td><xsl:value-of select="lecturers/lecturer/research" /></td> 

</tr> 
</xsl:template> 

보시다시피

, 템플릿이 처리를 계속하기 위해 apply-templates을한다. 당신이 다른 아이 또는 lecturer 요소의 하위 요소에 대한 더 많은 템플릿을 사용하려는 경우

귀하의 코멘트에 대한 응답으로 [편집] , 당신은

<xsl:template match="lecturer"> 
    <tr> 
    <xsl:apply-templates/> 
    </tr> 
</xsl:template> 

을 사용하고, 예를 들어 요소에 대한 템플릿을 작성할 수 있습니다

<xsl:template match="name/title | research"> 
    <td> 
    <xsl:value-of select="."/> 
    </td> 
</xsl:template> 

<xsl:template match="name/first"> 
    <td> 
    <xsl:value-of select="concat(., ' ', ../last)"/> 
    </td> 
</xsl:template> 

<!-- don't output name/last as the name/first template already does --> 
<xsl:template match="name/last"/> 

<xsl:template match="teaching"> 
    <xsl:apply-templates select="course"/> 
</xsl:template> 

<xsl:template match="course"> 
    <xsl:if test="position() > 1"><xsl:text> and </xsl:text></xsl:if> 
    <xsl:value-of select="."/> 
</xsl:template> 
+0

좋아요, 템플릿을 match = "lecturer"에 추가하면 템플리트에있는 select의 값을 변경해야 템플릿으로 만들 수 있습니까, 아니면 필요한 것입니까? 나는이 모든 일이 어떻게 작동하는지에 대해 정말로 혼란 스럽다. 그것은 우리에게 꽤 열악하게 가르쳐졌다. 감사. – Bic1245

+1

특정 유형의 노드 (예 :'lecturer'와'lecturer')의 요소 노드를'match = "lecturer"'와 일치시키는 템플리트를 작성하고 일치하는 노드와 관련된 표현식을 컨텍스트 노드로 쓰는 템플리트 내에서 템플리트를 작성합니다. 그리고 물론'apply-templates'를 사용하여 노드가 처리되는지 확인해야합니다. –

+0

좋아요, 아쉽게도 죄송합니다. 템플릿을 사용하여 제목을 표시하려면 어떻게해야합니까?겠습니까이 작품 같은 : ' \t \t Bic1245

1

당신이 currenly이 여기서

<tr> 
    <td><xsl:value-of select="lecturers/lecturer/name/title" /></td> 
    <td><xsl:value-of select="lecturers/lecturer/name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="lecturers/lecturer/name/last" /></td> 
    <td><xsl:value-of select="lecturers/lecturer/teaching/course" /> and <xsl:value-of select="(lecturers/lecturer/teaching/course)[2]" /></td> 
    <td><xsl:value-of select="lecturers/lecturer/research" /></td> 
</tr> 

당신은 apply-templates에게 전화를 가지고 있고 lecturer 템플릿을 갖고 싶어한다.

<xsl:template match="lecturer"> 
    <tr> 

     <td><xsl:value-of select="name/title" /></td> 
     <td><xsl:value-of select="name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="name/last" /></td> 
     <td><xsl:value-of select="teaching/course" /> and <xsl:value-of select="(teaching/course)[2]" /></td> 
     <td><xsl:value-of select="research" /></td> 

    </tr> 
</xsl:template>