Page 1 of 1

Getting all elements from XML

Posted: Tue Feb 13, 2007 2:35 pm
by olster
Here is my problem:

Here is a bit of XML being fed into a XSL transform. I've added spaces for easy reading.

<rootElement>

<ns0:rootData>
<ns0:SYSTEM_NAME>TBA</ns0:SYSTEM_NAME>
<ns0:TAGGED_ITEM_CODE>A-1032</ns0:TAGGED_ITEM_CODE>
</ns0:rootData>

<ns0:rootData>
<ns0:SYSTEM_NAME>TBA2</ns0:SYSTEM_NAME>
<ns0:TAGGED_ITEM_CODE>A-1033</ns0:TAGGED_ITEM_CODE>
</ns0:rootData>

</rootElement>

What i need from the out put is the name of each element and it's variable in a list, with the date and a single result from each data set.
Here is what i need:

<StgEngineeringDataCollection>
<ns0:StgEngineeringData>

<ns0:engName>SYSTEM_NAME</ns0:engName>
<ns0:engValue>TBA</ns0:engValue>
<ns0:importDate>2007-02-09</ns0:importDate>
<ns0:tagNo>A-1032</ns0:tagNo>

<ns0:engName>TAGGED_ITEM_CODE</ns0:engName>
<ns0:engValue>A-1032</ns0:engValue>
<ns0:importDate>2007-02-09</ns0:importDate>
<ns0:tagNo>A-1032</ns0:tagNo>

</ns0:StgEngineeringData>
<ns0:StgEngineeringData>

<ns0:engName>SYSTEM_NAME</ns0:engName>
<ns0:engValue>TBA2</ns0:engValue>
<ns0:importDate>2007-02-09</ns0:importDate>
<ns0:tagNo>A-1033</ns0:tagNo>

<ns0:engName>TAGGED_ITEM_CODE</ns0:engName>
<ns0:engValue>A-1033</ns0:engValue>
<ns0:importDate>2007-02-09</ns0:importDate>
<ns0:tagNo>A-1033</ns0:tagNo>

</ns0:StgEngineeringData>
</StgEngineeringDataCollection>

Ok, notice two things here, that each line of the XML is being outputted and i have no problem writing the XSL for this. In each data set each element's name and value is outputted as <engName> and <engValue>, finally the date is added and a constant <tagNo>.
But the problem is, is the <tagNo> element.
This is constant throughout the process of outputting each element within the set, but changes when you get to the next set of elements.

Here is my current XSL:

<xsl:template match="/">
<ns0:StgEngineeringDataCollection>
<xsl:for-each select="/tns:rootElement/tns:rootData">
<ns0:StgEngineeringData>
<xsl:for-each select="*">
<ns0:engName>
<xsl:value-of select="substring-after(name(.),':')"/>
</ns0:engName>
<ns0:engValue>
<xsl:value-of select="."/>
</ns0:engValue>
<ns0:importDate>
<xsl:value-of select="xp20:current-date()"/>
</ns0:importDate>
<ns0:tagNo>
<xsl:value-of select="tns:TAGGED_ITEM_CODE"/>
</ns0:tagNo>
</xsl:for-each>
</ns0:StgEngineeringData>
</xsl:for-each>
</ns0:StgEngineeringDataCollection>
</xsl:template>

As you can see i've set all paths as relative and the inital for-each loops over the data sets, then the second for-each loops round each element.
BUT tns:TAGGED_ITEM_CODE does not output anything. WHY? Well i guess that's becuase my actually position at each point is on random element which tns:TAGGED_ITEM_CODE doesn't exist on. I can understand that...

So why not make the path to tns:TAGGED_ITEM_CODE fixed, like:
<xsl:value-of select="/tns:rootElement/tns:rootData/tns:TAGGED_ITEM_CODE"/>

BUT, here when the series is outputted it looks like this:


<StgEngineeringDataCollection>
<ns0:StgEngineeringData>

<ns0:engName>SYSTEM_NAME</ns0:engName>
<ns0:engValue>TBA</ns0:engValue>
<ns0:importDate>2007-02-09</ns0:importDate>
<ns0:tagNo>A-1032</ns0:tagNo>

<ns0:engName>TAGGED_ITEM_CODE</ns0:engName>
<ns0:engValue>A-1032</ns0:engValue>
<ns0:importDate>2007-02-09</ns0:importDate>
<ns0:tagNo>A-1032</ns0:tagNo>

</ns0:StgEngineeringData>
<ns0:StgEngineeringData>

<ns0:engName>SYSTEM_NAME</ns0:engName>
<ns0:engValue>TBA2</ns0:engValue>
<ns0:importDate>2007-02-09</ns0:importDate>
<ns0:tagNo>A-1032</ns0:tagNo>

<ns0:engName>TAGGED_ITEM_CODE</ns0:engName>
<ns0:engValue>A-1033</ns0:engValue>
<ns0:importDate>2007-02-09</ns0:importDate>
<ns0:tagNo>A-1032</ns0:tagNo>

</ns0:StgEngineeringData>
</StgEngineeringDataCollection>

NOTE: The <ns0:tagNo> doesn't change, it just stays the same as the first data set, even though i have now looped onto the second data set, with a tagNo as A-1033 NOT A-1032.

So how do i do this. I need to loop down each element outputting the value, but also keep one element fixed as to which set i am in.

Thanks!!!!

o