?
Solved

XSL : XML Transformation - I want to permorm some operations when a particular tag occures else other operation

Posted on 2003-12-09
3
Medium Priority
?
308 Views
Last Modified: 2010-04-17
Hi Experts !!!
   
    Pls Help me out of this prob. What i am trying to do is to transform one xml to another using xsl. my input xml look like
        <root>
              <child>
                     <attrib1>value</attrib1>
                     <attrib1>value</attrib1>
                     <subchild>
                                  <subattrib>value</subattrib>  
                     </subchild>
             </child>
              <child>
                     <attrib1>value</attrib1>
                     <attrib1>value</attrib1>
                    <grandchild>
                              <gattrib>value</gattrib>
                    </grandchild>                    
             </child>
      </root>    

           Here note that inside child tag either subchild can occure or grandchild but not both.
     and in xsl i want to perform something when inside child tag i have subchild tag
& somthing else when i have grandchild inside childtag. & i want to make sure that atleast one exists .

           So could pls suggest me how can i determine in xsl that inside child tag wheather subchild exists or grandchild ??
 
0
Comment
Question by:Suyash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9904964
What would you do if you see both subchild and grandchild? What about when none of them exists?  

I would make sure that these conditions are met in the XML file, and not leave it up to the transformation to do the error checking. In this case, you can use something like this:

     <xsl:for-each select="root">
      <xsl:if test="child/subchild">
     ... do something ...
      </xsl:if>
      <xsl:if test="child/grandchild">
     ... do something else ...
      </xsl:if>
      </xsl:for-each>

This would not correctly handle the case where you have both, or where none of them exists.

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9905453
... here is a better solution:

<xsl:for-each select="root/child">
      <xsl:variable name="subchild" select="count(child::subchild)"/>
      <xsl:variable name="grandchild" select="count(child::grandchild)"/>
 
      <xsl:if test="$subchild = 0 and $grandchild >= 1">
... handle the case with one or more grandchildren
      </xsl:if>

    <xsl:if test="$subchild >= 1 and $grandchild = 0">
... handle the case with one or more subchildren
      </xsl:if>

        <xsl:if test="$subchild > 0 and $grandchild > 0">
... handle the case with subchild and granchild
        </xsl:if>

        <xsl:if test="$subchild = 0 and $grandchild = 0">
... handle the case with no subchild and no granchild
        </xsl:if>

      </xsl:for-each>
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 600 total points
ID: 9982805
Did this work for you?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question