[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Tag Replacement
It's much easier to do this with templates than to use for-each: <xsl:template match="/"> <vxml version="1.0" application="your-app-root.vxml"> <xsl:apply-templates/> </vxml> </xsl:template> <xsl:template match="card"> <form id="{@id}"> <field name="youNeedToHaveANameForYourFieldsToBeValidVXML"> <!-- :) --> <prompt> <xsl:apply-templates/> </prompt> </field> </form> </xsl:template> <xsl:template match="a"> <!-- special handling for anchors. Unless you explicitely define a template for something, it's text will be output (so the text in the <p> and <strong> will still be output, even though there is no template matching "p" and "strong") --> <voice gender="female"> <xsl:apply-templates/> </voice> </xsl:template> Using xsl:apply-templates does basically the same thing as your for-each, but it allows you to specify a different template for each node. The default template, which is used if you don't have one that matches a node, is similar to what you have already, which is basically to do <value-of select="."/>. Each time xsl:apply-templates is used, it basically does a for-each on each of the child nodes of the current node, and for each node it finds a template that matches that node and outputs the contents of the template. If you still want to use xsl:for-each (though I don't recommend it in almost all cases; templates are much simpler once you get the hang of them), you could replace your inner for-each with: <xsl:for-each select="*"> <xsl:choose> <xsl:when test="self::a"> <voice gender="female"> <xsl:value-of select="."/> </voice> </xsl:when> <xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose> </xsl:for-each> Once you get the hang of templates, though, you won't want to go back except in relatively rare cases. On Thursday 14 February 2002 15:43, Greg Gerou wrote: > Hi, > I'm attempting to use XSL to do the following: > > Input document: > > <?xml version="1.0"?> > <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" > "http://www.wapforum.org/DTD/wml_1.1.xml"> > <wml> > <card id="welcome" title="Welcome"> > <p><strong>Welcome to this WAP page!</strong></p> > <a href="products.wml">Products</a> > <p><a href="links.wml"><small>Links</small></a></p> > </card> > </wml> > > Output document: > > <?xml version="1.0" encoding="UTF-8"?> > <vxml> > <form id="welcome"> > <field> > <prompt>Welcome to this WAP page! > <voice gender="female">Products</voice> > <voice gender="female">Links</voice></prompt> > </field> > </form> > </vxml> > > In other words, I'm attempting to strip all the displayed text from the > original document and display it in the transformed document, but each > anchor tag ("<a...") is replaced by a <voice> tag. The following XSL will > do all this, except handle the anchor cases (it will ouput the above > "output document" without the voice tags): > > <?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="/"> > <vxml version="1.0"> > <xsl:for-each select="*/card"> > <form id="{@id}"> > <field> > <xsl:for-each select="*"> > > <xsl:value-of select="."/> > </xsl:for-each> > </field> > </form> > </xsl:for-each> > </vxml> > </xsl:template> > </xsl:template> > </xsl:stylesheet> > > What changes are necessary to get it to work in the desired fashion? Thanks > in advance! > > Greg Gerou > ggerou@xxxxxxxxxxxxxx > > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list -- Peter Davis algorithm, n.: Trendy dance for hip programmers. XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|