XSLT need a couple of fields added + quickie tutorial

Hi All

I'm building an SSIS package that consumes an XML file, then uses an XLST file to 'flatten' it into a single row.

Question:  How do I handle the elements not in the XLSD below?  Fuels-Unit,  All the Fuels-Costs values, and Dispatcher.

Here's the XML:
<FlightInfo>
    <FlightKey>
        <Alc>SCX</Alc>
        <Number>0117</Number>
        <STD>2014-06-30T11:00:00Z</STD>
        <OriginIcao>KMSP</OriginIcao>
    </FlightKey>
    <Compute Timestamp="2014-06-30 09:09:00">
        <Fuels Unit="LBS">
            <Pre>27151</Pre>
            <Release>10651</Release>
            <Tankerage>16500</Tankerage>
            <Burn>3830</Burn>
            <Arrival>22996</Arrival>
            <Costs>
                <Origin Currency="USD" Quantity="USG">3.11</Origin>
                <Destination Currency="USD" Quantity="USG">4.00</Destination>
                <Savings Currency="USD">2098.98</Savings>
            </Costs>
        </Fuels>
        <Dispatcher>NATHAN ROOD</Dispatcher>
    </Compute>
</FlightInfo>

Open in new window

Here's the XLSD, with the columns I was able to piece together..
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
   <xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
   
   <xsl:template match="/FlightInfo">
      <xsl:text>Alc,Number,STD,OriginIcao,Pre,Release,Tankerage,Burn,Fuel&#13;&#10;</xsl:text>
     
      <xsl:for-each select="FlightKey">
         <xsl:value-of select="Alc" /><xsl:text>","</xsl:text>
         <xsl:value-of select="Number" /><xsl:text>","</xsl:text>
         <xsl:value-of select="STD" /><xsl:text>","</xsl:text>
         <xsl:value-of select="OriginIcao" /><xsl:text>","</xsl:text>
      </xsl:for-each> 

      <xsl:for-each select="Compute/Fuels">
         <xsl:value-of select="Pre" /><xsl:text>","</xsl:text>
         <xsl:value-of select="Release" /><xsl:text>","</xsl:text>
         <xsl:value-of select="Tankerage" /><xsl:text>","</xsl:text>
         <xsl:value-of select="Burn" /><xsl:text>","</xsl:text>
         <xsl:value-of select="Arrival" /><xsl:text>","</xsl:text>
      </xsl:for-each> 
      
      <xsl:value-of select="/Compute/Dispatcher" /><xsl:text>","</xsl:text>
      
   </xsl:template>
</xsl:stylesheet> 

Open in new window

Thanks in advance.
Jim
LVL 66
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zc2Commented:
I've redesigned your XSLT a little, while making it to read all the requested values.
A general rule - it's almost always better to use templates instead of xsl:for-each

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
   <xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
   
   <xsl:template match="/FlightInfo">
      <xsl:text>Alc,Number,STD,OriginIcao,Pre,Release,Tankerage,Burn,Fuel,.......&#13;&#10;</xsl:text>
     
     <xsl:apply-templates select="FlightKey"/>
     <xsl:apply-templates select="Compute/Fuels"/>
     <xsl:apply-templates select="Compute/Fuels/Costs"/>
     <xsl:apply-templates select="Compute/Dispatcher" mode="out">
	 	<xsl:with-param name="last" select="true()"/>
     </xsl:apply-templates>
   </xsl:template>

	<xsl:template match="FlightKey">
		<xsl:apply-templates select="Alc" mode="out"/>
		<xsl:apply-templates select="Number" mode="out"/>
		<xsl:apply-templates select="STD" mode="out"/>
		<xsl:apply-templates select="OriginIcao" mode="out"/>
 	</xsl:template>

	<xsl:template match="Fuels">
		<xsl:apply-templates select="@Unit" mode="out"/>
		<xsl:apply-templates select="Pre" mode="out"/>
		<xsl:apply-templates select="Release" mode="out"/>
		<xsl:apply-templates select="Tankerage" mode="out"/>
		<xsl:apply-templates select="Burn" mode="out"/>
		<xsl:apply-templates select="Arrival" mode="out"/>
 	</xsl:template>
	<xsl:template match="Costs">
		<xsl:apply-templates select="Origin"/>
		<xsl:apply-templates select="Destination"/>
		<xsl:apply-templates select="Savings"/>
 	</xsl:template>

	<xsl:template match="Origin|Destination|Savings">
		<xsl:apply-templates select="." mode="out"/>
		<xsl:apply-templates select="@Currency" mode="out"/>
		<xsl:apply-templates select="@Quantity" mode="out"/>
 	</xsl:template>
   
   <xsl:template match="*|@*" mode="out">
    <xsl:param name="last"/>
   	<xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text>
   	<xsl:if test="not($last)">,</xsl:if>
   </xsl:template>
</xsl:stylesheet> 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
Couple of follow-on questions...

What is a 'template'?
Is there any way to specify the data type of each column?
0
zc2Commented:
You could think of a template as a "subroutine" - a chunk of your XSLT script which processes only given node(s) matched by the specified pattern.

In my example the template: <xsl:template match="FlightKey"> starts a template which is able to processes only the "FlightKey" element, nothing else.
It could be applied explicitly by <xsl:apply-templates select="FlightKey"/> or implicitly, when the XSLT traverses the tree and tries to find a best matching template for each node.
In my example the <xsl:template match="/FlightInfo"> template is applied implicitly.
Also a template could be called just by name, like a regular subroutine.

You can pass parameters to a template. For an instance, you could pass the data type parameter to the  <xsl:template match="*|@*" mode="out"> template making it format the output data differently depending on the provided data type parameter.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
Thanks much.  -Jim
0
zc2Commented:
You're welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.