Dimitre와 마찬가지로 XSLT에서 일치하지 않는 태그를 허용 할 방법이 없습니다. 태그가 일치하지 않아야합니다.
템플릿을 보면 XML 인스턴스의 <location>
요소 중 html 테이블을 만드는 것처럼 보입니다. 처음으로 <location>
에서 테이블을 열고 마지막으로 <location>
에서 테이블을 닫으려고합니다.
가장 쉬운 방법은 상위 수준 (상위/상위)에서 테이블을 연 다음 <location>
데이터로 테이블을 채우는 것입니다.
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Country</th>
</tr>
<tr>
<td>name 1</td>
<td>city 1</td>
<td>state 1</td>
<td>zip 1</td>
<td>country 1</td>
</tr>
<tr>
<td>name 2</td>
<td>city 2</td>
<td>state 2</td>
<td>zip 2</td>
<td>country 2</td>
</tr>
<tr>
<td>name 3</td>
<td>city 3</td>
<td>state 3</td>
<td>zip 3</td>
<td>country 3</td>
</tr>
</table>
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="doc">
<!--The table is inserted here.-->
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Country</th>
</tr>
<!--This is where we apply the templates to populate the rows.-->
<xsl:apply-templates select="location"/>
</table>
</xsl:template>
<!--This template populates the row(s).-->
<xsl:template match="location">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="city"/>
</td>
<td>
<xsl:value-of select="state"/>
</td>
<td>
<xsl:value-of select="zip"/>
</td>
<td>
<xsl:value-of select="country"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
이 출력입니다 : 여기에 테이블을 생성하는 스타일 시트의
<doc>
<location>
<name>name 1</name>
<city>city 1</city>
<state>state 1</state>
<zip>zip 1</zip>
<country>country 1</country>
</location>
<location>
<name>name 2</name>
<city>city 2</city>
<state>state 2</state>
<zip>zip 2</zip>
<country>country 2</country>
</location>
<location>
<name>name 3</name>
<city>city 3</city>
<state>state 3</state>
<zip>zip 3</zip>
<country>country 3</country>
</location>
</doc>
: 여기
3 개
<location>
의가있는 샘플 XML 파일입니다 어떤 이유로 든에서
<table>
을 생성해야하는 경우, 여전히 그렇게 할 수 있습니다. 그것은 더 많은 코드가 필요합니다. - 스타일 시트 요소 노드는 결과 트리 불가분 요소 노드를 작성 불가분 명령이다
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/doc">
<xsl:apply-templates/>
</xsl:template>
<!--The table is created at the first location and
the first row is populated.-->
<xsl:template match="location[1]">
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Country</th>
</tr>
<xsl:call-template name="location-row"/>
<!--Here is where we apply the other template to populate the other rows.
Notice we use a "mode" to differentiate the template from the generic
"location" template.-->
<xsl:apply-templates select="following-sibling::location" mode="not-first"/>
</table>
</xsl:template>
<!--This template will output the other rows.-->
<xsl:template match="location" mode="not-first" name="location-row">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="city"/>
</td>
<td>
<xsl:value-of select="state"/>
</td>
<td>
<xsl:value-of select="zip"/>
</td>
<td>
<xsl:value-of select="country"/>
</td>
</tr>
</xsl:template>
<!--This generic template matches locations other than the first one.
Basically it is consuming it so we don't get duplicate output.-->
<xsl:template match="location"/>
</xsl:stylesheet>
가능한 복제본 [XSLT : 종료 태그가 아님] (http://stackoverflow.com/questions/2872396/xslt-opening-but-not-closing-tags) –
그리고 http://stackoverflow.com/questions/3701708/how-can-i-print-a-single-div-closing-it-in-xslt 및 http://stackoverflow.com/questions/2202377/xslt-dynamically-start-and- 닫기 태그 및 ... –