2012-10-17 5 views
1

div 기반 XHTML 레이아웃을 XSLT 1.0을 사용하는 테이블 기반 레이아웃으로 변환해야합니다. 기본 변환의 경우 테이블 구조를 잘 생성하는 스타일 시트 (아래)가 있습니다.XSL : HTML 테이블 속성을 만들기 위해 DIV 클래스 속성 읽기

출력에 표 특정 속성을 추가하기 위해 입력 XHTML에서 여러 클래스 속성을 구문 분석하는 방법을 이해할 수 없습니다.

내 샘플 XHTML은 (클래스에서 복사해도 그래, 난, 새 테이블 속성 이러한 싶습니다) :

<div class="table align-center"> 
    <div class="tr"> 
     <div class="td"><p>Table Cell 1</p></div> 
     <div class="td"><p>Table Cell 2</p></div> 
    </div> 
</div> 

다음과 같이 테이블 구조 확인을 구축 기본 XSL은 다음과 같습니다

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="div[contains(@class, 'table')]"> 
    <table> 
     <xsl:copy-of select="attribute::node()"/> 
     <xsl:apply-templates/> 
    </table> 
</xsl:template> 

<xsl:template match="div[contains(@class, 'tr')]"> 
    <tr> 
     <xsl:copy-of select="attribute::node()"/> 
     <xsl:apply-templates/> 
    </tr> 
</xsl:template> 

<xsl:template match="div[contains(@class, 'td')]"> 
    <td> 
     <xsl:copy-of select="attribute::node()"/> 
     <xsl:apply-templates/> 
    </td> 
</xsl:template> 

이 스타일 시트는 생산 : 내가 생산은하고 싶은 무엇

<table class="table align-center"> 
    <tr class="tr"> 
     <td class="td"><p>Table Cell 1</p></td> 
     <td class="td"><p>Table Cell 2</p></td> 
    </tr> 
</table> 

e는 다음과 같습니다.

<table class="table align-center" align="center"> 
    <tr class="tr"> 
     <td class="td"><p>Table Cell 1</p></td> 
     <td class="td"><p>Table Cell 2</p></td> 
    </tr> 
</table> 

XSLT 1.0에서는이 작업을 수행 할 수 있습니까? 두 개 이상의 클래스를 추가하고 필요한 테이블 특성을 추가하기 위해 구문 분석을 수행 할 수있을 정도로 일반적인 솔루션을 원합니다.

감사합니다. 이 문서에 적용

+0

클래스 속성 값은 토큰 화를 요구하므로 http://www.exslt.org/str/functions/tokenize/index.html을 사용할 수 있는지 궁금합니다. 어떤 XSLT 1.0 프로세서를 대상으로합니까? –

+0

안녕 마틴. PHP에 내장 된 XSL 프로세스를 사용합니다. 나는 기능이 지원되지 않습니다 믿습니다. – Kevin

답변

1

이 1.0 XSLT 스타일 시트 ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" indent="yes"/> 
<xsl:strip-space elements="*" /> 

<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

    <xsl:template match="div[starts-with(@class, 'table ')]"> 
    <table> 
     <xsl:call-template name="extract-class"> 
      <xsl:with-param name="class-list" select="normalize-space(substring-after(@class,'table '))" /> 
     </xsl:call-template> 
     <xsl:apply-templates select="@*|node()"/> 
    </table> 
</xsl:template> 

<xsl:template match="div[starts-with(@class, 'tr ')]"> 
    <tr> 
     <xsl:apply-templates select="@*|node()"/> 
    </tr> 
</xsl:template> 

<xsl:template match="div[starts-with(@class, 'td ')]"> 
    <td> 
     <xsl:apply-templates select="@*|node()"/> 
    </td> 
</xsl:template> 

<xsl:template name="extract-class"> 
    <xsl:param name="class-list" /> 
    <xsl:if test="contains($class-list,'-')"> 
    <xsl:variable name="name-value" select="substring-before(concat($class-list,' '),' ')" /> 
    <xsl:attribute name="{substring-before($name-value,'-')}"> 
     <xsl:value-of select="substring-after($name-value,'-')" /> 
    </xsl:attribute> 
    <xsl:call-template name="extract-class"> 
     <xsl:with-param name="class-list" select="normalize-space(substring-after($class-list,' '))" /> 
    </xsl:call-template> 
    </xsl:if> 
</xsl:template> 

</xsl:stylesheet> 

...

<div class="table align-center border-1 cellspacing-5"> 
    <div class="tr"> 
     <div class="td"><p>Table Cell 1</p></div> 
     <div class="td"><p>Table Cell 2</p></div> 
    </div> 
</div> 

수율 ... ... *

<table align="center" border="1" cellspacing="5" class="table align-center border-1 cellspacing-5"> 
    <tr class="tr"> 
    <td class="td"> 
     <p>Table Cell 1</p> 
    </td> 
    <td class="td"> 
     <p>Table Cell 2</p> 
    </td> 
    </tr> 
</table> 
+0

와우! 저건 완벽 해. 정말 고맙습니다! – Kevin

+0

환영합니다. 체크 표시를 클릭하여 답변을 수락하십시오. –