Skip to Content
author's profile photo Former Member
Former Member

XSLT for dynamic target field names

Hello,

I got a requiremt where I need to create target field names from input values.

Input XML -

<?xml version="1.0" encoding="UTF-8"?>
<Createelement_MT>
<Field Name="A">
<Value>1</Value>
</Field>
<Field Name="B">
<Value>1</Value>
</Field>
...
</Createelement_MT>

Expected OutPut XML -

<?xml version="1.0" encoding="UTF-8"?>
<Createelement_MT>
<Statement><TableName><table>XYZ</table>
<access>
<A> 1 </A>
<B> 2 </B>
..
</access>
</Statement></TableName>
</Createelement_MT>

The value of the attribute 'Name' of field 'Field' should be the name of target field.

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Createelement_MT">
<xsl:element name="statement">
<xsl:element name="TableName">
<xsl:element name="table">ABC</xsl:element>
<xsl:element name="access">
<xsl:for-each select="Field"> 
<xsl:element name="{@Name}">
<xsl:value-of select="Value"></xsl:value-of></xsl:element></xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Error:

Error: at xsl:element on line 15 of file:///......: XTDE0820: Invalid element name. Invalid QName {}

Please help me with the xslt code for this requirement.

Thanks!

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Jul 19, 2011 at 02:39 AM

    Hello,

    If this is not a case study, it would be easier to use SQL QUERY instead 😊

    Inside the access field, you just write the SQL statement as is.

    http://help.sap.com/saphelp_nwpi711/helpdata/en/44/747eda12dd3676e10000000a114a6b/frameset.htm

    Hope this helps,

    Mark

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Anupam Ghosh

      Thanks Anupam. Its working perfect now. The attribute might be missing value in the earlier XML that I used for testing.

      Good to know the normalize-space function.

      Thanks Everyone who provided valuable suggestions!!!

      *Closing the thread**

  • Posted on Jul 19, 2011 at 10:28 AM

    Hi,

    Please try this code

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="xml"/>
    	<xsl:template match="/">
    		<Createelement_MT>
    			<Statement>
    				<TableName>
    					<table>XYZ</table>
    				<access>
    					<xsl:for-each select="Createelement_MT/Field">
    						<xsl:variable name="field" select="@Name"></xsl:variable>
    						<xsl:element name="{$field}">
    						<xsl:value-of select="Value"></xsl:value-of>
    						</xsl:element>
    					</xsl:for-each>
    				</access>
    				</TableName>
    			</Statement>
    		</Createelement_MT>
    	</xsl:template>
    </xsl:stylesheet>
    

    input xml

     <?xml version="1.0" encoding="UTF-8" ?> 
    - <Createelement_MT>
    - <Field Name="A">
      <Value>1</Value> 
      </Field>
    - <Field Name="B">
      <Value>1</Value> 
      </Field>
      </Createelement_MT>
    

    output xml

    <?xml version="1.0" encoding="UTF-8" ?> 
    - <Createelement_MT>
    - <Statement>
    - <TableName>
      <table>XYZ</table> 
    - <access>
      <A>1</A> 
      <B>1</B> 
      </access>
      </TableName>
      </Statement>
      </Createelement_MT>
    

    One small request, if you think your question has been answered,could you please kindly, if possible, close this thread.

    regards

    Anupam

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 19, 2011 at 08:09 AM

    Chk this for xslt mapping...

    create a variable with name "Field" which will read dynamic attribut value from the source and will act as a target element name...

    <xsl:variable name: "Field">

    <xsl:for-each select ="Createelement_MT/Field/@Name"/>

    </xsl:variable>

    <access>

    <xsl:element name="{$Field}">

    <xsl:value-of select =Createelement_MT/Field/value"/>

    </xsl:element>

    </access>

    Note: please chk for syntax errors

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.