tags:

views:

24

answers:

3

Hi,

I need a help on getting primary key values repeated times from the xml.Here is the xml file.

<?xml version="1.0" encoding="UTF-8"?>
<request id="test">
<dataSets>
 <dataSet name="dim_table" friendlyName="dim" tableType="DIM">
 <fields>
  <field name="customer_id" dataType="varchar" primaryKey="true"/>
  <field name="customer_name" dataType="VARCHAR" primaryKey="false"/>
  <field name="customer_address" dataType="CHAR" primaryKey="false"/>
 </fields>
 </dataSet>
</dataSets>
</request>

the required output is

KeycolumnName,Columnname
customer_id,customer_id
customer_id,customer_name
customer_id,customer_address

Can anyone help me on this?

A: 

XML formatting is needed for a full answer, but:

<xsl:template match="/dataSets/dataSet/fields">
    <xsl:for-each select="field">
        <xsl:value-of select="@name" />
    </xsl:for-each>
</xsl:template>

(example code not tested)

Use XPath to define the content of the "select" attributes.

Reference: http://www.w3schools.com/xsl/el_for-each.asp

David
A: 

thanks for your reply David.here is my xslt code.i have tried to assign the primary column name as variable.But its returning null for non primary fields.

<xsl:for-each select="//dataSet">
<xsl:for-each select="fields/field">
<xsl:choose>            
<xsl:when test="@primaryKey = 'true'">
<xsl:variable name="primary" select="@name"/>
KeyColumnName="<xsl:value-of select="$primary"/>" ColumnName="<xsl:value-of select="@name"/>"<br/>
</xsl:when>
<xsl:otherwise>
KeyColumnName="<xsl:value-of select="$primary"/>" ColumnName="<xsl:value-of select="@name"/>"<br/>  
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
rajcog
XSLT does not have "variables" per se. I was confused with this concept when I first started. You may look into "xsl:call-template" and "xsl:with-param". Essentially you would call a template (similar to what most languages call "functions") with parameters.
David
"Note: Once you have set a variable's value, you cannot change or modify that value!" http://www.w3schools.com/xsl/el_variable.asp
David
also, your example is tantamount to saying: if($primary == true) print "foo"; else print "foo";both the if and else conditions output identical results.
David
More help: http://www.w3schools.com/xpath/xpath_syntax.asp
David
A: 

Updated code (previous answer couldn't handle this much new code):

This code works for me:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="/">
        <xsl:for-each select="//dataSet">
            <xsl:for-each select="fields/field">
                KeyColumnName="<xsl:value-of select="../field[@primaryKey='true']/@name" />" ColumnName="bar <xsl:value-of select="@name" />"
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
David
Note: The KeyColumnName will be a concatenation of _all_ KeyColumnNames (not just the first one) if there are multiple "field" nodes in the same "fields" node that match [primaryKey='true']
David
Thanks a lot David.I got my required output.
rajcog