wrirting XSL to perform some operations on xml data

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

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 &gt; 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>


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!