Jump to content

How to match child attributes with different parent tags


wickey

Recommended Posts

Hello,I'm new to xml and xslt, and have a question, probably an easy fix, but I'm not finding a solution very quickly at all.I'm trying to transform a doxygen xml file, just transforming it to the standard xml format I need for some other final transform.Basically, I need to match two child elements with one another, ( paramname with parammsg ) but, both of these have different parent nodes. I can do a transform that will giveeach ( paramname with parammsg ) an unique attribute, but I still don't know how to match those together.Here is a small piece of the xml. I want to extract the paramname and it's respective parammsg, <memberdef><paramlist><paramitem><paramname kind="1"><para>double num1</para></paramname></paramitem><paramitem><paramname kind="2"><para>double num2</para></paramname></paramitem><paramitem><paramname kind="3"><para>double num3</para></paramname></paramitem></paramlist><detaileddescription><parammsg kind="1"><para>This is number 1</para></parammsg><parammsg kind="2"><para>This is number 2</para></parammsg><parammsg kind="3"><para>This is number 3</para></parammsg></detaileddescription></memberdef> So, after the transform I would like to have a form, something like this: <memberdef> <param><row><entry><code>double num1</code></entry><entry>This is number 1 </entry></row><row><entry><code>double num2</code></entry><entry>This is number 2 </entry></row><row><entry><code>double num3</code></entry><entry>This is number 3</entry></row></param></memberdef> Also, if there is a way to match these without using the attribute, that would be of great help aswell.Thank-You for any help or suggestions! Edit: I found a solution to my issue, dont know if this is the right way to do it, but it works for my situation.I used a variable to keep a count <xsl:variable name="cnt"><xsl:number/></xsl:variable> then selected the correct param descriptionwhen it's position() = $cnt. This is my template: <xsl:template match="paramitem"><xsl:variable name="cnt"><xsl:number/></xsl:variable><row><entry> <!-- Parameters and brief descriptions -\-> <!-\- Name -\-> <!-\- Description --><code><xsl:value-of select="paramname "/></code></entry><entry><xsl:value-of select="para/../../../detaileddescription/paramsg[position()= $cnt]/para"/></entry></row></xsl:template> Hope this helps anyone else thats looking for something like this, but if there are better solutions, let me know.

Edited by wickey
Link to comment
Share on other sites

You can omit the variable and use current() instead, like:

<xsl:template match="paramitem"><row><entry> <!-- Parameters and brief descriptions -\-> <!-\- Name -\-> <!-\- Description --><code><xsl:value-of select="paramname "/></code></entry><entry><xsl:value-of select="para/../../../detaileddescription/paramsg[position()=current()/position()]/para"/></entry></row></xsl:template>

Doing that should improve performance a little.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...