开发者

wrirting XSL to perform some operations on xml data

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"?><!-- DWXML开发者_高级运维Source="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>


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>


This should work: (if provided with a well-formed XML – see comment on question)

<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…


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜