By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,081 Members | 800 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,081 IT Pros & Developers. It's quick & easy.

Convert dynamic XML using XSLT into HTML.

P: 9
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
Share this Question
Share on Google+
6 Replies

jkmyoung
Expert 100+
P: 2,057
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

P: 9
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
Expert 100+
P: 2,057
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

P: 9
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
Expert 100+
P: 2,057
sorry, try

*[not(*)]

I forget some processors are pickier then others.
Sep 25 '07 #6

P: 9
Thanks!!! That worked.
Oct 1 '07 #7

Post your reply

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