Help/Advice with XSL / XPath needed

Discussion in 'XML' started by Philip, Feb 27, 2004.

  1. Philip

    Philip Guest

    Hi,

    I am trying to output certain nodes inside another. I have an xml
    template with field definitions for a form, and this includes
    textfields, labels, checkboxes etc plus fieldssets. I defined them
    like
    this:

    >>>> XML SNIPPET >>>>

    <fields>
    <field>
    <name>lAccount_Num</name>
    <id>lAccount_Num</id>
    <legend />
    <caption>Account Number:</caption>
    <type>label</type>
    <size />
    <style>LEFT: 17px; WIDTH: 112px; POSITION: absolute; TOP: 35px;
    HEIGHT:
    18px</style>
    <FOR>tAccount_Num</FOR>
    <maxLength />
    <parentFieldSet>fstFields</parentFieldSet>
    <value />
    </field>

    ....

    <field>
    <name>lEnabled</name>
    <id>lEnabled</id>
    <caption>Enabled:</caption>
    <legend />
    <type>label</type>
    <size />
    <style>LEFT: 330px; WIDTH: 120px; POSITION: absolute; TOP: 100px;
    HEIGHT: 18px</style>
    <FOR>chEnable</FOR>
    <parentFieldSet />
    <maxLength />
    <value />
    </field>

    ....

    <field>
    <name>fstFields</name>
    <id>fstFields</id>
    <legend>Account Date</legend>
    <caption />
    <type>fieldset</type>
    <size />
    <style>LEFT: 8px; WIDTH: 484px; POSITION: absolute; TOP: 12px; HEIGHT:
    124px</style>
    <FOR />
    <parentFieldSet>fstFields</parentFieldSet>
    <maxLength />
    <value />
    </field>
    </fields>
    <<<<< END XML SNIPPET <<<

    so you can see I have a field nodes with a defined parentFieldSet, a
    field nodes with no defined fieldset, and a fieldset node.

    What I want is to use an XPath expresion in my match / apply-templates
    to output the start tag of the FieldSet, then go and find all the
    nodes
    with that parentFieldSet name defined, then output the end tag of the
    fieldset, following that we do the next fieldset (if any) and finally
    output all the fields with no parentFieldSet defined.

    Here is my XSL so far:
    >>> XSL >>>>

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:eek:utput method="html" version="1.0" omit-xml-declaration="no"
    standalone="no" indent="yes" />
    <xsl:template match="/">
    <html>
    <body bgcolor="#dddddd">
    <!-- field[parentFieldSet='fstFields']/-->
    <xsl:apply-templates select="fields/field"/>
    </body>
    </html>

    </xsl:template>

    <xsl:template match="field">
    <xsl:choose>
    <xsl:when test="./type='Text'">
    <input type="text">
    <xsl:attribute name="name">
    <xsl:value-of select="name" />
    </xsl:attribute>
    <xsl:attribute name="id">
    <xsl:value-of select="id" />
    </xsl:attribute>
    <xsl:attribute name="FOR">
    <xsl:value-of select="FOR" />
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:value-of select="style" />
    </xsl:attribute>
    <xsl:if test="./maxlength !=''">
    <xsl:attribute name="maxlength">
    <xsl:value-of select="maxlength" />
    </xsl:attribute>
    </xsl:if>
    <xsl:if test="./size != ''">
    <xsl:attribute name="size">
    <xsl:value-of select="size" />
    </xsl:attribute>
    </xsl:if>
    </input>
    </xsl:when>
    <xsl:when test="./type='fieldset'">
    <fieldset>
    <xsl:attribute name="name">
    <xsl:value-of select="name" />
    </xsl:attribute>
    <xsl:attribute name="id">
    <xsl:value-of select="id" />
    </xsl:attribute>
    <xsl:attribute name="FOR">
    <xsl:value-of select="FOR" />
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:value-of select="style" />
    </xsl:attribute>
    <xsl:if test="./maxlength !=''">
    <xsl:attribute name="maxlength">
    <xsl:value-of select="maxlength" />
    </xsl:attribute>
    </xsl:if>
    <xsl:if test="./size != ''">
    <xsl:attribute name="size">
    <xsl:value-of select="size" />
    </xsl:attribute>
    </xsl:if>
    <xsl:if test="./legend != ''">
    <legend>
    <xsl:value-of select="legend" />
    </legend>
    </xsl:if>
    </fieldset>
    </xsl:when>
    <xsl:when test="./type = 'label'">
    <label>
    <xsl:attribute name="name">
    <xsl:value-of select="name" />
    </xsl:attribute>
    <xsl:attribute name="id">
    <xsl:value-of select="id" />
    </xsl:attribute>
    <xsl:attribute name="FOR">
    <xsl:value-of select="FOR" />
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:value-of select="style" />
    </xsl:attribute>
    <xsl:if test="./maxlength !=''">
    <xsl:attribute name="maxlength">
    <xsl:value-of select="maxlength" />
    </xsl:attribute>
    </xsl:if>
    <xsl:if test="./size != ''">
    <xsl:attribute name="size">
    <xsl:value-of select="size" />
    </xsl:attribute>
    </xsl:if>
    <xsl:value-of select="caption" />
    </label>
    </xsl:when>
    <xsl:when test="./type='Checkbox'">
    <input type="checkbox">
    <xsl:attribute name="id">
    <xsl:value-of select="id" />
    </xsl:attribute>
    <xsl:attribute name="name">
    <xsl:value-of select="name" />
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:value-of select="style" />
    </xsl:attribute>
    </input>
    </xsl:when>
    </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>
    <<<< END XSL <<<

    It outputs all the fields, but doesn't care which ones are supposed to
    be inside the fielddset.

    This is so that I can reuse my XSL/ASP with multiple form definition
    templates built in XML...then I can realize the age old dream of
    separating content from structure, and even processing code from
    content
    and structure...yay

    I'd be grateful for any assistance or help or ideas

    Philip
    Philip, Feb 27, 2004
    #1
    1. Advertising

  2. "Philip" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to output certain nodes inside another. I have an xml
    > template with field definitions for a form, and this includes
    > textfields, labels, checkboxes etc plus fieldssets. I defined them
    > like
    > this:
    >

    Hi,

    Seems to me that what you need is :

    <xsl:for-each select="fields">
    <xsl:apply-templates select="field" />
    </xsl:for-each>

    ...and this should replace the line that you currently have :

    <xsl:apply-templates select="fields/field"/>

    Regards,
    Kenneth
    Kenneth Stephen, Mar 1, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,968
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,403
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    520
  4. Replies:
    1
    Views:
    3,598
    A. Bolmarcich
    May 27, 2005
  5. Candle

    XPath or XSL Transform Help

    Candle, Dec 11, 2006, in forum: XML
    Replies:
    2
    Views:
    408
    Candle
    Dec 12, 2006
Loading...

Share This Page