2016-10-21 6 views
0

중간 요소는 어떻게 계산합니까? 이 문제에 대한 해결책은 this question과 관련이 있다고 생각합니다.xlst를 사용하여 중간 요소를 계산하십시오.

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       version="2.0"> 
<xsl:template match="/root"> 
<xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="row"> 
    <xsl:choose> 
     <!--these are the indented ones--> 
     <xsl:when test="cell/@padding"> 
     <xsl:number 
       value="count(preceding-sibling::row[child::cell[1][@padding]]) + 1" 
       format="a.&#x20;"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:number 
       value="count(preceding-sibling::row[child::cell[1][not(@padding)]]) + 1" 
       format="1.&#x20;"/> 
     </xsl:otherwise> 
    </xsl:choose> 
    <xsl:value-of select="cell/text()"/> 
</xsl:template> 

</xsl:stylesheet> 

나는이 결과를 받고 있어요 : 내가 좋아하는 것이 무엇

1. Title 
    a. Chapter 
    b. Chapter 
    c. Chapter 
2. Title 
    d. Chapter 
3. Title 
    e. Chapter 
    f. Chapter 
4. Title 
    g. Chapter 

가지고하는 것입니다 이런 변환을 적용,

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <row> 
    <cell>Title</cell> 
    </row> 
    <row> 
    <cell padding='true'>Chapter</cell> 
    <cell>example additional cell</cell> 
    </row> 
    <row> 
    <cell padding='true'>Chapter</cell> 
    </row> 
    <row> 
    <cell padding='true'>Chapter</cell> 
    </row> 
    <row> 
    <cell>Title</cell> 
    </row> 
    <row> 
    <cell padding='true'>Chapter</cell> 
    </row> 
    <row> 
    <cell>Title</cell> 
    </row> 
    <row> 
    <cell padding='true'>Chapter</cell> 
    </row> 
    <row> 
    <cell padding='true'>Chapter</cell> 
    </row> 
    <row> 
    <cell>Title</cell> 
    </row> 
    <row> 
    <cell padding='true'>Chapter</cell> 
    </row> 
</root> 

그리고 : 나는 이런 식으로 뭔가를했다 가정하면 "하위"항목의 번호 매기기가 다시 시작됩니다. 나는 이전의 요소를 되돌아 보는 것을 멈추려면 축을 어떻게 바꾸는 지 알 수 없다.

"챕터"를 계산하려고했습니다. 그래서 :

1. Title 
    a. Chapter 
    b. Chapter 
    c. Chapter 
2. Title 
    a. Chapter 
3. Title 
    a. Chapter 
    b. Chapter 
4. Title 
    a. Chapter 
+0

당신이 원하는 출력을 게시하고 "하위"항목이 입력에 무엇을 설명해주십시오. –

답변

0

난 당신이이 방법을 시도 제안 :

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text" encoding="UTF-8" /> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/root"> 
    <xsl:apply-templates select="row[not(cell/@padding)] | row/cell[@padding]"/> 
</xsl:template> 

<xsl:template match="row"> 
    <xsl:number count="row[not(cell/@padding)]" format="1. "/> 
    <xsl:value-of select="cell"/> 
    <xsl:text>&#10;</xsl:text> 
</xsl:template> 

<xsl:template match="cell"> 
    <xsl:number count="cell[@padding]" level="any" from="row[not(cell/@padding)]" format=" a. "/> 
    <xsl:value-of select="."/> 
    <xsl:text>&#10;</xsl:text> 
</xsl:template> 

</xsl:stylesheet> 
+0

그래, 그래, 그게 올바른 방향으로 나를 지적했다. 템플릿 매칭을 변경할 수는 없지만, 'count'와'from'을 사용하여 원점을 식별하고 무엇을 계산해야하는지 알 수 있습니다. –

관련 문제