Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9049619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:26:40+00:00 2026-06-16T12:26:40+00:00

i am trying to output the Form using XML & XSLT in php but

  • 0

i am trying to output the Form using XML & XSLT in php but i am unable to fullfill the condition somehow.here is what my XML look like:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="form.xsl"?>

<root>

    <formset>
            <field>
                <type>text</type>                
                <value>This is test value</value>
            </field>
            <field>
                <type>radio</type>                
                <value>Male</value>
            </field>
            <field>
                <type>checkbox</type>                
                <value>Hobby</value>
            </field>
            <field>
                <type>button</type>                
                <value>Click Me</value>
            </field>                                    
    </formset>

</root>

and Here is my XSLT file:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Rendering of Form</title>
            </head>
            <body>
                <form action="" method="post">
                    <xsl:for-each select="/root/formset/field">

                        <xsl:choose>
                            <xsl:when test="type = text">
                                <input type="text" value="{value}" />
                            </xsl:when>
                            <xsl:when test="type = radio">
                                <input type="radio" value="{value}" /> <xsl:value-of select="value"/>
                            </xsl:when>
                            <xsl:when test="type = checkbox">
                                <input type="checkbox" value="{value}" /> <xsl:value-of select="value"/>
                            </xsl:when>
                            <xsl:when test="type = button">
                                <input type="button" value="{value}" />
                            </xsl:when>                
                            <xsl:otherwise>
                             <span>Unknown Type</span>
                            </xsl:otherwise>
                        </xsl:choose>             

                    </xsl:for-each>

                </form>



            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

my PHP code is this simple echo the output:

<?php

// LOAD XML FILE
$XML = new DOMDocument();

$XML->load('form.xml');

// START XSLT
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();

$XSL->load('form.xsl');
$xslt->importStylesheet( $XSL );
echo $xslt->transformToXML( $XML );

?>

but somehow my output renders xsl:otherwise condition can anyone tell me why ?? i am new to XSLT

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:26:43+00:00Added an answer on June 16, 2026 at 12:26 pm

    This is not actually to do with PHP, but simply to do with the fact you have missed out some apostrophes in your xsl:when test statements. You are currently doing this

     <xsl:when test="type = text">
    

    But this is comparing an element called type against an element called text, which doesn’t actually exist in your XML. You need to do this instead

     <xsl:when test="type = 'text'">
    

    i.e. You need to compare against a literal string, enclosed in apostrophes.

    If you are keen to learn XSLT, here is a way to do the same thing without the need for xsl:for-each of xsl:choose

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
       <xsl:output method="html"/>
       <xsl:template match="/">
          <html>
             <head>
                <title>Rendering of Form</title>
             </head>
             <body>
                <form action="" method="post">
                   <xsl:apply-templates select="root/formset/field"/>
                </form>
             </body>
          </html>
       </xsl:template>
    
       <xsl:template match="field">
          <input type="{type}" value="{value}"/>
       </xsl:template>
    
       <xsl:template match="field[type != 'text'][type != 'radio'][type != 'checkbox'][type != 'button']">
          <xsl:text>Unknown Type</xsl:text>
       </xsl:template>
    </xsl:stylesheet>
    

    Using templates in this way is usually preferred as it helps re-use code, and cuts down on indentation to make it more readable. Note that the more specific template will always get chosen first when matching elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying comment a multiline section of xml to an output file using
I'm trying to generate a form using the form_for helper in RoR but I
I am trying to construct google charts using a ajax response form PHP file.
Trying to output a pointer's address in decimal form using the %zu conversion specifier.
I am trying to do multiple product add with a form using php and
I am trying to capture the contents of my php page using output buffering:
I am using zend framework and am trying to output a simple login form
I've noticed strange behavior when trying to redirect the output form a node.js script
I am trying to output the comments of a facebook page in PHP. For
I m trying to output introtext of article selected by their alias <?php $db

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.