我想創建一個 xsl:if 陳述句,如果滿足某些條件,它將應用模板。
這是我的 XML 檔案。我想找到可以滑冰的公園并按字母順序排序:
<?xml version="1.0"?>
<parks>
<park>
<park_name>MCGUANE (JOHN)</park_name>
<acres>10.3</acres>
<iceskating>0</iceskating>
</park>
<park>
<park_name>ARMOUR (PHILIP) SQUARE</park_name>
<acres>9.05</acres>
<iceskating>0</iceskating>
</park>
<park>
<park_name>FULLER (MELVILLE)</park_name>
<acres>11.31</acres>
<iceskating>1</iceskating>
</park>
<park>
<park_name>CORNELL (PAUL) SQUARE</park_name>
<acres>8.8</acres>
<iceskating>1</iceskating>
</park>
<park>
<park_name>RUSSELL (MARTIN) SQUARE</park_name>
<acres>10.05</acres>
<iceskating>2</iceskating>
</park>
</parks>
這是我的 XSL 檔案。如果子元素“iceskating”包含大于 0 的值,我希望它應用模板:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xsl:element name="results">
<xsl:element name="iceskating_parks">
<xsl:apply-templates select="parks/park">
<xsl:sort select="park_name" order="ascending" />
</xsl:apply-templates>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="iceskating" >
<xsl:if test="iceskating > 0">
<park name="{park_name}" acres="{acres}" />
</xsl:if>
</xsl:template>
我希望轉換后的 XML 檔案的格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<iceskating_parks>
<park name="PARK NAME" acres="99.99"/>
<park name="PARK NAME" acres="99.99"/>
<park name="PARK NAME" acres="99.99"/>
</iceskating_parks>
</results>
uj5u.com熱心網友回復:
將條件放入應用模板中,例如
<xsl:apply-templates select="parks/park[iceskating > 0]">
并保留match="park"以前的建議,而不是切換到match="iceskating".
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363075.html
上一篇:如何將新的xml節點附加到具有php類DOMDocument的現有.xml檔案?
下一篇:XmlSchemaValidationException:這是一個無效的xsi:type'Book'嘗試反序列化XML并使用XSD架構對其進行驗證
