473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Convert dynamic XML using XSLT into HTML.

Hello Experts,
I'm trying to convert an dynamic XML doc into HTML using XSLT.
In the example I have shown one form(form1)...But there could be multiple forms....names are unknown and each one could have o or more childforms .I want to display form1 as <th> element followed by attributes,then display Childform1 as header(if there is a child form) .See the XSl below.
Is there a way to check if form1 has childforms...so i could render them differently than children of form1.
Pls help.

<root>
<forms>
<form1 attr1= "p" attr2 = "q">
<childForm1>
<SOURCE_CD>17</SOURCE_CD>
<MAIN_FORM_ID>00023</MAIN_FORM_ID>
<DISTRICT_CD>00</DISTRICT_CD>
</childForm1>
<childForm2>
<SOURCE_CD>17</SOURCE_CD>
<MAIN_FORM_ID>00023</MAIN_FORM_ID>
<DISTRICT_CD>00</DISTRICT_CD>
</childForm2>
<childForm3>
<SOURCE_CD>17</SOURCE_CD>
<MAIN_FORM_ID>00023</MAIN_FORM_ID>
<DISTRICT_CD>00</DISTRICT_CD>
</childForm3>
<childForm4>
<SOURCE_CD>17</SOURCE_CD>
<MAIN_FORM_ID>00023</MAIN_FORM_ID>
<DISTRICT_CD>00</DISTRICT_CD>
</childForm4>

<INT_DCMT_ID>242563</INT_DCMT_ID>
<EXT_TP_ID>214201050</EXT_TP_ID>
<TAX_TYPE_CD>PI</TAX_TYPE_CD>
<TAX_SUB_TYPE_CD>01</TAX_SUB_TYPE_CD>
</form1>

</forms>
</root>

<xsl:template name="label-value">
<tr><th><xsl:value-of select="name()" /></th></tr>
<xsl:for-each select="@*">
<tr>
<td>@ <xsl:value-of select="name()" /></td>
<td><xsl:value-of select="." /></td>
</tr>
</xsl:for-each>
<xsl:for-each select="*">
<tr>
<td><xsl:value-of select="name()" /></td>
<td><xsl:value-of select="." /></td>
</tr>
</xsl:for-each>
</xsl:template>


<xsl:template match="forms">
<xsl:for-each select="*">
<xsl:call-template name="label-value" />
</xsl:for-each>
</xsl:template>
Sep 20 '07 #1
6 3379
jkmyoung
2,057 Expert 2GB
I would suggest using local-name() like:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="forms">
  2.   <xsl:for-each select="*[not contains(local-name(), 'childForm')]">
  3.     <xsl:call-template name="label-value" />
  4.   </xsl:for-each> 
  5.   <xsl:apply-templates select="*[contains(local-name(), 'childForm')]"/>
  6. </xsl:template>
  7.  
  8. <xsl:template match="*[contains(local-name(), 'childForm')]">
  9. ...childForm rendering here.
  10. </xsl:template>
Sep 20 '07 #2
I would suggest using local-name() like:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="forms">
  2.   <xsl:for-each select="*[not contains(local-name(), 'childForm')]">
  3.     <xsl:call-template name="label-value" />
  4.   </xsl:for-each> 
  5.   <xsl:apply-templates select="*[contains(local-name(), 'childForm')]"/>
  6. </xsl:template>
  7.  
  8. <xsl:template match="*[contains(local-name(), 'childForm')]">
  9. ...childForm rendering here.
  10. </xsl:template>
Hi jkmyoung,
Thank you ver much for u'r response.I totally understand your logic.But my problem is I don't know the childform names ...so I cannot use contains to compare if the local node contains "childForm".
In my scenario under root ,there are n (approx 100)number of forms and each form has 0 or more child forms along with its own elements.... So using the XPath say //forms/Form1/childForm1 is ruled out b'cos that would take a lot of analysis and forms could change periodically...So i'm trying to create an XSl which would work for the dynamic XML ...I could get upto //forms/form1/ ...
So I need a way to figure out if a childform /child node exists inside form1...
In simple words I'm unabel to access an unknown node()....
Will the code below work / PLS help.

<xsl:choose>
<xsl when test="boolean( form1/node() )" >
render ...
</xsl: when>
<xsl: otherwise>
render differently...
</xsl:otherwise>
</xsl:choose>
Sep 21 '07 #3
jkmyoung
2,057 Expert 2GB
Ok, it's simpler then:
So child forms have children elements, whereas regular elements do not?
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="forms">
  2.   <xsl:for-each select="*[not *]">
  3.     <xsl:call-template name="label-value" />
  4.   </xsl:for-each> 
  5.   <xsl:apply-templates select="*[*]"/>
  6. </xsl:template>
  7.  
  8. <xsl:template match="*[*]">
  9. ...childForm rendering here.
  10. </xsl:template>
Sep 21 '07 #4
Ok, it's simpler then:
So child forms have children elements, whereas regular elements do not?
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="forms">
  2.   <xsl:for-each select="*[not *]">
  3.     <xsl:call-template name="label-value" />
  4.   </xsl:for-each> 
  5.   <xsl:apply-templates select="*[*]"/>
  6. </xsl:template>
  7.  
  8. <xsl:template match="*[*]">
  9. ...childForm rendering here.
  10. </xsl:template>
Hi jkmyoung,
I tried the above code ,but its not acceptiong the syntax *[not *] says unexpected Token.
How can I test if the current node has child elements on not?When i go into the forms level I need to display the form Name as the Header (I can easily do this) but the next step is to find out if the current element/node(inside forms) has children or not,if it has then I need to display them diffrently than the elements directly under form.
<forms>
<form1>
<childForm1>
elements...
</childform1>
elements...
</form1>
<forms>
How do I find out element inside form1,in this case childform1 has children or not.

Pls help .
Thanks.
Sep 25 '07 #5
jkmyoung
2,057 Expert 2GB
sorry, try

*[not(*)]

I forget some processors are pickier then others.
Sep 25 '07 #6
Thanks!!! That worked.
Oct 1 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Matt | last post by:
I want to use XML to store a document's configurations. And I can convert to different file format by using XSL. For example, convert to HTML, PDF, or RTF. But the contents are all stored in single...
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
2
by: Alan Searle | last post by:
I find that I can display structured data very nicely using XML with an XSL template. As an extra 'goodie', I would like to give users the ability to sort that data (for example with a button...
2
by: Henrik Skak Pedersen | last post by:
Hi, I am trying to convert a very basic WML document to HTML using the word2HTML.xsl stylesheet. But I get an exception when I am trying to convert it: The code looks like this: ...
5
by: Daniel Frey | last post by:
Hello I'd like to match a dynamic node, given as a parameter to the stylesheet. Something like: <xsl:stylesheet ...> <xsl:param name="tomatch"/> <xsl:template match="{$tomatch}"> Hallo...
4
by: elsigh | last post by:
I'm wondering if anyone has any ideas about a way to quickly convert an HTML DOM Node into an XML Document. The goal is that I want to perform XSLT on the Node, which is coded correctly as XHTML....
12
by: Chris | last post by:
Hi, Just wondering if anyone out there knows if it is possible to convert a CSV to xml using XSLT? I've seen a lot of examples of xml to CSV, but is it possible to go back the other way? I...
2
by: Ch Pravin | last post by:
Hi All: I am having the following xml which i need to convert to excel using xslt. Please help me out. Afghanistan.xml <?xml version="1.0" encoding="utf-16"?> <Languages...
0
by: miamikk | last post by:
I am XML newbie. I have question about inserting dynamic text in the header of HTML table. This is the site I have created (Only Report Type 1 is working)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.