可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How to write xsl in the body of products.xsl that will get product name and condition with quantity > 10
products.xml:
<?xml version="1.0" encoding="iso-8859-1"?> <products> <product> <name>soaps</name> <quantity>10</quantity> <condition>ready</condition> </product> <product> <name>soaps</name> <quantity>15</quantity> <condition>ready</condition> </product> <product> <name>soaps</name> <quantity>20</quantity> <condition>ready</condition> </product> </products>
products.xsl
<?xml version="1.0"?><!-- DWXMLSource="products.xml" --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <HTML> <HEAD> <TITLE> products</TITLE> </HEAD> <BODY> products quantity greater than 10 : <BR/> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
回答1:
This should do the trick:
<xsl:for-each select="/products/product"> <xsl:if test="quantity > 10"> <xsl:value-of select="name" />: <xsl:value-of select="condition" /> <br/> </xsl:if> </xsl:for-each>
回答2:
<BODY> products quantity greater than 10 : <BR/> <xsl:apply-templates select="//product[quantity > 10]"/> </BODY>
Combined with e.g. this template:
<xsl:template match="product"> <P> <xsl:value-of select="name"/> <xsl:text>: </xsl:text> <xsl:value-of select="condition"/> </P> </xsl:template>
Just customize per your needs…
回答3:
This transformation (no <xsl:for-each>
and no conditional instructions):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="product[quantity > 10]"> <p> Product: <xsl:value-of select="name"/> contition: <xsl:value-of select="condition"/> quantity: <xsl:value-of select="quantity"/> </p> </xsl:template> <xsl:template match="product"/> </xsl:stylesheet>
when applied on the provided XML document:
<products> <product> <name>soaps</name> <quantity>10</quantity> <condition>ready</condition> </product> <product> <name>soaps</name> <quantity>15</quantity> <condition>ready</condition> </product> <product> <name>soaps</name> <quantity>20</quantity> <condition>ready</condition> </product> </products>
produces the wanted result:
<p> Product: soaps contition: ready quantity: 15</p> <p> Product: soaps contition: ready quantity: 20</p>