我對 xml 相當陌生,而且我在使用模板時遇到了一個奇怪的問題。對于空資料節點,我想在 html 頁面上顯示諸如“未找到”或“無資料”之類的訊息,而不是顯示資料應位于的空白區域。我已經完成了一個 xsl:choose 陳述句和 2 個 xsl:if 陳述句,但都沒有成功。我已經知道這個特定的節點是空的,我只想顯示一條訊息而不是空格。否則或 False 陳述句似乎根本沒有運行。請幫忙!!我在這里做錯了什么?
XML 代碼
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<findings>
<ssns/>
<dobs>
<dob>
<data>082967</data>
</dob>
</dobs>
<dobs>
<dob>
<data>020568</data>
</dob>
</dobs>
<names>
<name>
<full>Homer J Simpson</full>
<first>Homer</first>
<last>Simpson</last>
<middle>J</middle>
</name>
</names>
<names>
<name>
<full>Marge H Simpson</full>
<first>Marge</first>
<last>Simpson</last>
<middle>H</middle>
</name>
</names>
</findings>
</Root>
XSL 代碼
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<hmtl>
<head>
<title>Person Results</title>
</head>
<div style="font-weight:bold">Alias(es):</div>
<xsl:apply-templates select="Root/findings/names"/>
<div style="font-weight:bold">SSNs:</div>
<xsl:apply-templates select="Root/findings/ssns"/>
<div style="font-weight:bold">DOBs:</div>
<xsl:apply-templates select="Root/findings/dobs"/>
</hmtl>
</xsl:template>
<xsl:template match="Root/findings/names">
<p>
<xsl:variable name="nmesHasData" select="boolean(normalize-space(name))"/>
<xsl:for-each select="name">
<xsl:choose>
<xsl:when test="$nmesHasData">
<ul> <xsl:apply-templates select="full"/> </ul>
</xsl:when>
<xsl:otherwise>
<xsl:text>None found</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</p>
</xsl:template>
<xsl:template match="Root/findings/ssns">
<p>
<xsl:variable name="ssnHasData" select="boolean(normalize-space(ssns))"/>
<xsl:for-each select="ssn">
<xsl:choose>
<xsl:when test="$ssnHasData">
<ul><xsl:apply-templates select="data"/></ul>
</xsl:when>
<xsl:otherwise>
<xsl:text>None found</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</p>
</xsl:template>
<xsl:template match="Root/findings/dobs">
<p>
<xsl:variable name="dobHasData" select="boolean(normalize-space(dob))"/>
<xsl:for-each select="dob">
<xsl:choose>
<xsl:when test="$dobHasData">
<ul><xsl:apply-templates select="data"/></ul>
</xsl:when>
<xsl:otherwise>
<xsl:text>None found</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</p>
</xsl:template>
</xsl:transform>
HTML 結果
<!DOCTYPE html
PUBLIC "XSLT-compat">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Person Results</title>
</head>
<div style="font-weight:bold">Alias(es):</div>
<p>
<ul>Homer J Simpson</ul>
</p>
<p>
<ul>Marge H Simpson</ul>
</p>
<div style="font-weight:bold">SSNs:</div>
<p></p>
<div style="font-weight:bold">DOBs:</div>
<p>
<ul>082967</ul>
</p>
<p>
<ul>020568</ul>
</p>
</html>
uj5u.com熱心網友回復:
將模板 match="Root/findings/dobs" 更改為:
<xsl:template match="Root/findings/dobs">
<p>
<xsl:for-each select="dob">
<xsl:variable name="dobHasData" select="boolean(normalize-space(.))"/>
<xsl:choose>
<xsl:when test="$dobHasData">
<ul><xsl:apply-templates select="data"/></ul>
</xsl:when>
<xsl:otherwise>
<xsl:text>None found</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</p>
</xsl:template>
甚至更多模板匹配,例如:
<xsl:template match="Root/findings/dobs">
<p>
<xsl:apply-templates select="dob"/>
</p>
</xsl:template>
<xsl:template match="dob">
<xsl:variable name="dobHasData" select="boolean(normalize-space(.))"/>
<xsl:choose>
<xsl:when test="$dobHasData">
<ul><xsl:apply-templates select="data"/></ul>
</xsl:when>
<xsl:otherwise>
<xsl:text>None found</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
最后一個模板也可以分成兩個短的:
<xsl:template match="dob[normalize-space(.)]">
<ul><xsl:apply-templates select="data"/></ul>
</xsl:template>
<xsl:template match="dob[not(normalize-space(.))]">
<xsl:text>None found</xsl:text>
</xsl:template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/464015.html
上一篇:02-CubeMx+Keil+Proteus仿真STM32 - GPIO(一)
下一篇:第一步:分析XML的目錄層次結構
