Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Form Fields from XML Tags

I'm trying to figure out a way to utilize a PHP script that will:

  1. Open an XML document when a link to that document is clicked (from an HTML page).
  2. Scan the XML document for tags.
  3. Create an HTML form with input fields based on the tags.
  4. Post the input data back to the XML within the tags (when form is submitted) and print the XML to HTML.

So, if I had an XML file that went like this:

<profile>
Hello, my name is <name></name>.  I am <age></age> years old.  I live in <place></place>
</profile>

Upon clicking a link to that file, PHP would generate a form like so:

<form> 
Name:
Age:
Place:
</form>

Then upon completing and submitting the form (let's say the person is Joel, 25, from Boston), the following would be written to the screen:

Hello, my name is Joel. I am 25 years old. I live in Boston.

Any code or points to good tutorials would be appreciated.

THX

E.

like image 424
user633264 Avatar asked Mar 08 '26 10:03

user633264


1 Answers

You should use XSLT for this..

With browser:

xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<profile>
Hello, my name is <name></name>.  I am <age></age> years old.  I live in <place></place>
</profile>

xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>

    <xsl:template match="profile">
        <form>
            <xsl:for-each select="child::*">
                <label>
                    <xsl:value-of select="name()"/>: 
                    <input name="{name()}" type="text" />
                </label>
                <br />
            </xsl:for-each>
        </form>
    </xsl:template>
</xsl:stylesheet>

output:

<form>
    <label>name: <input type="text" name="name"></label><br />
    <label>age: <input type="text" name="age"></label><br />
    <label>place: <input type="text" name="place"></label><br />
</form>

There is an xsl extension for php you can use.

like image 109
inf3rno Avatar answered Mar 10 '26 22:03

inf3rno