2013-08-07 1 views
1

XSLT와 관련하여 질문이 있습니다. XSLT를 사용하여 XML을 다른 XML로 변환 할 수 있다는 것을 알고 있습니다. 의이XSLT를 사용하여 일대 다 매핑을 수행하는 방법

<data> 
    <field> 
    <attr>Attribute 1</attr> 
    <attr>Attribute 2</attr> 
    </field> 
</data> 

data.xml이라는 XML 파일이 있다고 가정 해 봅시다 가정 transform.xsl와 두 파일은 유사한 방식으로 data.xml 변환하는 방법을 정의 another.xsl
를라는 XSL 파일이있다.
transform.xslanother.xsl
이 사람이 만일 그 작업을 수행하는 방법을 알고 않습니다의 내가 foo.html로 변환하고 another.xsl의 변환이 foo.html에 추가 할 수 있도록 transform.xsl을하게하고 싶은 말은하자가 포함 ???
다른 문제는 바로 <xsl:include>이 템플릿을 덮어 쓸 수 있기 때문에 하나만 적용된다는 것입니다. 동일한 <xsl:template>을 여러 번 호출 할 수 있습니까?

답변

1

모드를 사용하여 처리 단계 (<xsl:template match="field" mode="m1">...</xsl:template><xsl:template match="data"><xsl:apply-templates select="field" mode="m1"/></xsl:template>)를 구별 할 수 있지만 물론 another.xsl을 제작하거나 모드를 사용하도록 편집해야합니다.

두 번째로 질문을 XSLT 2.0으로 태그 지정 했으므로 <xsl:next-match/>을 사용하는 옵션이 있습니다 (http://www.w3.org/TR/xslt20/#element-next-match 참조).

<xsl:stylesheet version="2.0" exclude-result-prefixes="xs" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xsl:include href="test2013080802.xsl"/> 

    <xsl:output method="html" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/"> 
     <html lang="en"> 
     <head> 
      <title>Example</title> 
     </head> 
     <body> 
      <h1>Example</h1> 
      <xsl:apply-templates/> 
     </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="data"> 
     <table> 
     <thead> 
      <xsl:apply-templates select="field[1]" mode="thead"/> 
     </thead> 
     <tbody> 
      <xsl:apply-templates/> 
     </tbody> 
     </table> 
     <xsl:apply-templates select="." mode="list"/> 
    </xsl:template> 

    <xsl:template match="field" mode="thead"> 
     <tr> 
     <xsl:apply-templates mode="thead"/> 
     </tr> 
    </xsl:template> 

    <xsl:template match="field/*" mode="thead"> 
     <th><xsl:value-of select="local-name()"/></th> 
    </xsl:template> 

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

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

</xsl:stylesheet> 

하면이 다음 모드가 list 이름을 사용합니다 포함

이 모드로 작업의 당신에게 예를 제공하기 위해, 주요 스타일입니다 :

<xsl:stylesheet version="2.0" exclude-result-prefixes="xs" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="/" mode="list"> 
     <html lang="en"> 
     <head> 
      <title>Example</title> 
     </head> 
     <body> 
      <h1>Example</h1> 
      <xsl:apply-templates mode="#current"/> 
     </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="data" mode="list"> 
     <ul> 
     <xsl:apply-templates mode="#current"/> 
     </ul> 
    </xsl:template> 

    <xsl:template match="field" mode="list"> 
     <li> 
     <xsl:apply-templates select="*" mode="#current"/> 
     </li> 
    </xsl:template> 

    <xsl:template match="field/*" mode="list"> 
     <xsl:if test="position() > 1">, </xsl:if> 
     <xsl:value-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 

출력은 다음

<html lang="en"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Example</title> 
    </head> 
    <body> 
     <h1>Example</h1> 
     <table> 
     <thead> 
      <tr> 
       <th>attr</th> 
       <th>attr</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td>Attribute 1</td> 
       <td>Attribute 2</td> 
      </tr> 
     </tbody> 
     </table> 
     <ul> 
     <li>Attribute 1, Attribute 2</li> 
     </ul> 
    </body> 
</html> 
입니다
+0

대단히 감사합니다! –

관련 문제