Convert dynamic XML using XSLT into HTML.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Muthu08
    New Member
    • Sep 2007
    • 9

    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)...B ut 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>0 0023</MAIN_FORM_ID>
    <DISTRICT_CD>00 </DISTRICT_CD>
    </childForm1>
    <childForm2>
    <SOURCE_CD>17 </SOURCE_CD>
    <MAIN_FORM_ID>0 0023</MAIN_FORM_ID>
    <DISTRICT_CD>00 </DISTRICT_CD>
    </childForm2>
    <childForm3>
    <SOURCE_CD>17 </SOURCE_CD>
    <MAIN_FORM_ID>0 0023</MAIN_FORM_ID>
    <DISTRICT_CD>00 </DISTRICT_CD>
    </childForm3>
    <childForm4>
    <SOURCE_CD>17 </SOURCE_CD>
    <MAIN_FORM_ID>0 0023</MAIN_FORM_ID>
    <DISTRICT_CD>00 </DISTRICT_CD>
    </childForm4>

    <INT_DCMT_ID>24 2563</INT_DCMT_ID>
    <EXT_TP_ID>2142 01050</EXT_TP_ID>
    <TAX_TYPE_CD>PI </TAX_TYPE_CD>
    <TAX_SUB_TYPE_C D>01</TAX_SUB_TYPE_CD >
    </form1>

    </forms>
    </root>

    <xsl:template name="label-value">
    <tr><th><xsl:va lue-of select="name()" /></th></tr>
    <xsl:for-each select="@*">
    <tr>
    <td>@ <xsl:value-of select="name()" /></td>
    <td><xsl:valu e-of select="." /></td>
    </tr>
    </xsl:for-each>
    <xsl:for-each select="*">
    <tr>
    <td><xsl:valu e-of select="name()" /></td>
    <td><xsl:valu e-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>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    I would suggest using local-name() like:
    [CODE=xml]<xsl:template match="forms">
    <xsl:for-each select="*[not contains(local-name(), 'childForm')]">
    <xsl:call-template name="label-value" />
    </xsl:for-each>
    <xsl:apply-templates select="*[contains(local-name(), 'childForm')]"/>
    </xsl:template>

    <xsl:template match="*[contains(local-name(), 'childForm')]">
    ...childForm rendering here.
    </xsl:template>[/CODE]

    Comment

    • Muthu08
      New Member
      • Sep 2007
      • 9

      #3
      Originally posted by jkmyoung
      I would suggest using local-name() like:
      [CODE=xml]<xsl:template match="forms">
      <xsl:for-each select="*[not contains(local-name(), 'childForm')]">
      <xsl:call-template name="label-value" />
      </xsl:for-each>
      <xsl:apply-templates select="*[contains(local-name(), 'childForm')]"/>
      </xsl:template>

      <xsl:template match="*[contains(local-name(), 'childForm')]">
      ...childForm rendering here.
      </xsl:template>[/CODE]
      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>

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Ok, it's simpler then:
        So child forms have children elements, whereas regular elements do not?
        [CODE=xml]<xsl:template match="forms">
        <xsl:for-each select="*[not *]">
        <xsl:call-template name="label-value" />
        </xsl:for-each>
        <xsl:apply-templates select="*[*]"/>
        </xsl:template>

        <xsl:template match="*[*]">
        ...childForm rendering here.
        </xsl:template>[/CODE]

        Comment

        • Muthu08
          New Member
          • Sep 2007
          • 9

          #5
          Originally posted by jkmyoung
          Ok, it's simpler then:
          So child forms have children elements, whereas regular elements do not?
          [CODE=xml]<xsl:template match="forms">
          <xsl:for-each select="*[not *]">
          <xsl:call-template name="label-value" />
          </xsl:for-each>
          <xsl:apply-templates select="*[*]"/>
          </xsl:template>

          <xsl:template match="*[*]">
          ...childForm rendering here.
          </xsl:template>[/CODE]
          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.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            sorry, try

            *[not(*)]

            I forget some processors are pickier then others.

            Comment

            • Muthu08
              New Member
              • Sep 2007
              • 9

              #7
              Thanks!!! That worked.

              Comment

              Working...