I have the following xml file and wanted to generate another xml file. While using the xml:element to create elements on the fly, the xslt debugger is throwing an error saying expected Qname. How can I get away with this.

xsl code:

<?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="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:element name="some-root-node">
<xsl:apply-templates select="record/item/value/item[@name='form']/value"/>
</xsl:element>
</xsl:template>

<xsl:template match="record/item/value/item[@name='form']/value">
<xsl:param name="preDefined" select="item[@name='FieldName']/value"/>
<xsl:element name="{@preDefined}">
<xsl:value-of select="item[@name='required']/value"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

XML File:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="email-xml-output.xsl"?>
<record name="emailform.xml" type="content">
<item name="email">
<value>
<item name="form">
<value>
<item name="Field">
<value>
<item name="FieldName">
<value>First_Name</value>
</item>
<item name="validation">
<value>text_only</value>
</item>
<item name="min">
<value>1</value>
</item>
<item name="max">
<value>25</value>
</item>
<item name="size">
<value>25</value>
</item>
<item name="required">
<value>required</value>
</item>
</value>
</item>
</value>
</item>
</value>
</item>
</record>

expected result:

<some-root-node>
<First_Name>required</First_Name>
</some-root-node>

The following stylesheet gives the required output.

<?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="iso-8859-1" indent="yes"/>

<xsl:template match="/">
   <xsl:element name="some-root-node">
      <xsl:apply-templates select="record/item/value/item[@name='form']/value"/>
   </xsl:element>
</xsl:template>

<xsl:template match="record/item/value/item[@name='form']/value">
   <xsl:variable name="preDefined" select="//item[@name='FieldName']/value"/>
   <xsl:element name="{$preDefined}" >
      <xsl:value-of select="//item[@name='required']/value"/>
   </xsl:element>
</xsl:template>

</xsl:stylesheet>

Thank you for the much needed help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.