Martin Fowler가 설명한대로 "2 단계보기"패턴을 구현하는 동안 HTML 표의 대체 행 색상을 작동시키는 데 문제가있었습니다. XSLT position()
함수를 사용합니다. 아래 table/row
의 XSLT 템플릿을 볼 수 있습니다. 그러나 출력에서 요소의 bgcolor
특성은 항상 "linen"
이며 table/row
개 요소를 반복 할 때 position()
값이 변경되지 않음을 나타냅니다. 왜 이럴 수 있니?2 단계보기에서 XSLT position() 함수가 예상대로 작동하지 않습니다.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="screen">
<html>
<body bgcolor="white">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="title">
<h1>
<xsl:apply-templates/>
</h1>
</xsl:template>
<xsl:template match="field">
<p><b><xsl:value-of select="@label"/>: </b><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="table">
<table><xsl:apply-templates/></table>
</xsl:template>
<xsl:template match="table/row">
<xsl:variable name="bgcolor">
<xsl:choose>
<xsl:when test="(position() mod 2) = 0">linen</xsl:when>
<xsl:otherwise>white</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr bgcolor="{$bgcolor}"><xsl:apply-templates/></tr>
</xsl:template>
<xsl:template match="table/row/cell">
<td><xsl:apply-templates/></td>
</xsl:template>
</xsl:stylesheet>
입력 XML :
<?xml version="1.0"?>
<screen>
<title>Dissociation</title>
<field label="Artist">Dillinger Escape Plan</field>
<table>
<row>
<cell>Limerent Death</cell>
<cell>4:06</cell>
</row>
<row>
<cell>Symptom Of Terminal Illness</cell>
<cell>4:03</cell>
</row>
<row>
<cell>Wanting Not So Much To As To</cell>
<cell>5:23</cell>
</row>
</table>
</screen>
출력 HTML :
<html><body bgcolor="white">
<h1>Dissociation</h1>
<p><b>Artist: </b>Dillinger Escape Plan</p>
<table>
<tr bgcolor="linen">
<td>Limerent Death</td>
<td>4:06</td>
</tr>
<tr bgcolor="linen">
<td>Symptom Of Terminal Illness</td>
<td>4:03</td>
</tr>
<tr bgcolor="linen">
<td>Wanting Not So Much To As To</td>
<td>5:23</td>
</tr>
</table>
</body></html>
감사합니다. 그러나 왜 ''가''position()'테스트가 비 - 행 엘리먼트에서 평가되지 못하게하는지 분명히 이해하지 못합니까? 'match '를 사용하는 것과 (작동하지 않는)'select ='를 사용하는 것의 차이는 무엇입니까? (작동합니까?) –
amoe
' '(' '의 약자이므로) 처리를 위해 모든 자식 노드를 선택하면, ' '에 비해 처리되는 다른 노드 집합 (XSLT 1.0) 또는 시퀀스 (XSLT 2.0). 모든 자식 노드 내에서 특정'row' 요소는'row' 요소 만 보는 것과는 다른 위치를가집니다. 특히 요소 노드 사이에 공백 텍스트 노드가있는 형식이 지정된 XML을 사용하면됩니다. 자세한 내용은 https://www.w3.org/TR/xslt#section-Applying-Template-Rules를 참조하십시오. –
각각 https://www.w3.org/TR/xslt20/#applying-templates "정렬 된 순서에서 N 번째 노드와 일치하는 규칙은 노드를 컨텍스트 항목으로 평가하고 N을 컨텍스트 위치로 사용하여 평가됩니다. 컨텍스트 크기로 정렬 된 시퀀스의 길이로. " –