Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT for unknown Element Names 1

Status
Not open for further replies.

stephenk

Programmer
Mar 12, 2001
21
AU
I want to transform an XML document into RTF. The RTF side is no problem, but the XML document varies constantly in respect of the element names. For example:

<REQUEST>
<REQUEST_DETAIL>
<REQ_TYPE>P</REQ_TYPE>
<DOC_NAME>TEST</DOC_NAME>
<DOC_PATH>C:\PRINTDOC\TEST.DOC</DOC_PATH>
</REQUEST_DETAIL>
<REQUEST_DATA>
<DATA>
<KEY>234</KEY>
<FNAME>MARY</FNAME>
<SURNAME>BROWN</SURNAME>
</DATA>
<DATA>
<KEY>123</KEY>
<FNAME>JOHN</FNAME>
<SURNAME>SMITH</SURNAME>
</DATA>
</REQUEST_DATA>
</REQUEST>

In this case the resulting format I need is as follows:

KEY,FNAME,SURNAME
234,MARY,BROWN
123,JOHN,SMITH

and this would be fine if the elements always had the same names. But in another example where the elements were as follows:

<DATA>
<KEY>123</KEY>
<COMPANYNAME>XYZ INC</COMPANYNAME>
<CONTACT>JOHN SMITH</CONTACT>
</DATA>

How could I use the same XSLT. I have tried doing this and suspect that I may need to use the &quot;name()&quot; function but I cannot get it right.

The basics of the issue are that for any XML document the REQUEST, REQUEST_DETAIL, REQUEST_DATA and DATA element names always exist. The elemnt names within the DATA element can change from document to document. The result I need is the first line containing the element names within the DATA element and then the actual values for each of those elements for each DATA element in the document.

I think....
 
>> I may need to use the &quot;name()&quot; function but I cannot get
>> it right.

Seems likely, show a small code sample of using name() where it does not work for you.

-pete
 
In my latest XSLT incarnation I have found node() to be somewhat apprppriate. As it goes:

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot; <xsl:eek:utput method=&quot;text&quot;/>
<xsl:template match=&quot;DATA&quot;>
<xsl:for-each select=&quot;node()&quot;>
<xsl:value-of select=&quot;node()&quot;/>
<xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:text>&lt;br&gt;</xsl:text>
</xsl:template>
</xsl:stylesheet>

This provides me with the following result:

PTESTC:\PRINTDOC\TEST.DOCKEY,FNAME,SURNAME,DEBTOS,NAD,OP_NAME,
123,JOHN,SMITH,100.00,12/02/03,MR COLLECTOR,

The issues I still have are:

Why is the REQUEST_DETAIL data appearing?
How can I avoid the last commad on each line.

What I want is:

KEY,FNAME,SURNAME,DEBTOS,NAD,OP_NAME
123,JOHN,SMITH,100.00,12/02/03,MR COLLECTOR

So close, yet so far....
 
What Transform engine are you using? That xsl file isn't even valid xml. And <xsl:template match=&quot;DATA&quot;> doesn't match anything as a root template.

-pete
 
I use XMLSpy but when it pasted, there appears to be an additional ';' at the end of the namespave closing tag.

Being my first attempt at transforming, I am not aware of why or why not this works except that i created the XML and the XSLT using this software.

Regardless of the XSLT above, is there a preferred way of 'looping' through child elemnts on unknown names and displaying their values?
 
try this in your stylesheet
Code:
<xsl:template match=&quot;/&quot;>

<xsl:apply-templates select=&quot;//DATA[1]&quot; />

<xsl:for-each select=&quot;//DATA&quot;>
	<xsl:for-each select=&quot;*&quot;>
		<xsl:value-of select=&quot;.&quot;/>
		<xsl:if test=&quot;position()!=last()&quot;>
			<xsl:text>,</xsl:text>
		</xsl:if>
	</xsl:for-each>
	<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>

<xsl:template match=&quot;DATA&quot;>
<xsl:for-each select=&quot;*&quot;>
	<xsl:value-of select=&quot;name()&quot;/>
	<xsl:if test=&quot;position()!=last()&quot;>
		<xsl:text>,</xsl:text>
	</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
 
Like I said - so close, yet so far!

Thanks for your effort. It's amazing how easy things are when someone knows what they are doing!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top