Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie with XSLT, need to create ID's for hierarchical tree 1

Status
Not open for further replies.

jtrembla

ISP
Jul 6, 2006
104
US
I have an xml file and I want to create unique id's for all the nodes and then i want to determine if a node is a child then give it a parentid. Based on my research I presume it requires a recursive use of xslt with the generateid function. However, I could not find an example to suit my needs. Can anyone help.

So for example Executive Summary would get a ID=1 and since it is the parent of itself, the parentID will also be 1.

Here is the xml file
Code:
<Nodes>
  <DbControls 
      TopicId="Executive Summary" 
      Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
  <DbControls Type="Data"
      TopicId="Prescriber Weighting of Clinical Endpoints"
      Url="~/DiseaseAnalysis/DisAnPrescriberWeightingData.aspx">
    <DbControls  Type="Data"
        TopicId="Survey Data" 
        Url="~/DiseaseAnalysis/DisAnPrescriberWeightingData.aspx" />
    <DbControls 
        TopicId="Analysis of Prescriber Weighting"
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" >
      <DbControls 
          TopicId="Clinical Trial End Points" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Physician Weighting" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    </DbControls>
  </DbControls>
  <DbControls Type="Data"
      TopicId="Thought Leader Scoring of Drugs"
      Url="~/DiseaseAnalysis/DisAnThoughtLeaderData.aspx">
    <DbControls Type="Data"
        TopicId="Clinical Data and Thought Leader-Based Scores"
        Url="~/DiseaseAnalysis/DisAnThoughtLeaderData.aspx">
      <DbControls Type="Sources"
          TopicId="Sources"
          Url="~/DiseaseAnalysis/DisAnThoughtLeaderSources.aspx" />
    </DbControls>
    <DbControls 
        TopicId="Analysis of Thought Leader Perception and Scores"
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx">
      <DbControls 
          TopicId="Benchmark Drug/Regimen"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Efficacy"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Safety"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Delivery"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Current and Future Clinical Gold-Standard Therapies "
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    </DbControls>
  </DbControls>
  <DbControls  Type="Data"
      TopicId="Regulatory Path and Competition in Pipeline"
      Url="~/DiseaseAnalysis/DisAnRegulatoryPipelineData.aspx">
    <DbControls Type="Data" 
        TopicId="Data" 
        Url="~/DiseaseAnalysis/DisAnRegulatoryPipelineData.aspx" />
    <DbControls 
        TopicId="Methods for Regulatory, Competition in Pipeline"
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    <DbControls
        TopicId="Analysis of Labeling Differences, Order of Entry"
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
  </DbControls>
  <DbControls Type="Data" 
      TopicId="Pricing and Reimbursement"
      Url="~/DiseaseAnalysis/DisAnPricingReimbursementData.aspx">
    <DbControls Type="Data" 
        TopicId="Data" 
        Url="~/DiseaseAnalysis/DisAnPricingReimbursementData.aspx" />
    <DbControls 
        TopicId="Pricing Analysis"
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx">
      <DbControls 
          TopicId="Generic Erosion"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    </DbControls>
    <DbControls 
        TopicId="Reimbursement"
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx">
      <DbControls 
          TopicId="US Payor and Reimbursement graphic"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Europe"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Japan"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    </DbControls>
  </DbControls>
  <DbControls Type="Data" 
      TopicId="Promotional Activity"
      Url="~/DiseaseAnalysis/DisAnPromotionalActivityData.aspx">
    <DbControls Type="Data" 
        TopicId="Data" 
        Url="~/DiseaseAnalysis/DisAnPromotionalActivityData.aspx" />
    <DbControls 
        TopicId="Methods and Analysis"
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
  </DbControls>
  <DbControls Type="Data" 
      TopicId="Current Drugs Impact on Quality of Life"
      Url="~/DiseaseAnalysis/DisAnCurrentDrugsImpactData.aspx">
    <DbControls Type="Data" 
        TopicId="Survey Data" 
        Url="~/DiseaseAnalysis/DisAnCurrentDrugsImpactData.aspx" />
    <DbControls 
        TopicId="Methods and Analysis" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
  </DbControls>
  <DbControls Type="Data" 
      TopicId="Prescriber-Defined Opportunity and Unmet Need"
      Url="~/DiseaseAnalysis/DisAnUnmetNeedData.aspx">
    <DbControls Type="Data" 
        TopicId="Survey Data" 
        Url="~/DiseaseAnalysis/DisAnUnmetNeedData.aspx" />
    <DbControls 
        TopicId="Top Areas of Opportunity" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    <DbControls 
        TopicId="Physician Level of Dissatisfaction with Key Therapies" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    <DbControls 
        TopicId="Unmet Need" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx">
      <DbControls 
          TopicId="Efficacy"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Safety"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Delivery"
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    </DbControls>
  </DbControls>
  <DbControls Type="Data" 
      TopicId="Target Product Profile"
      Url="~/DiseaseAnalysis/DisAnTPPSurveyData.aspx">
    <DbControls Type="Data" 
        TopicId="Survey Data" 
        Url="~/DiseaseAnalysis/DisAnTPPSurveyData.aspx" />
    <DbControls 
        TopicId="Analysis" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx">
      <DbControls 
          TopicId="Assessing Drug Development Opportunities" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Clinical Trial End Points" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Physicians Survey Results" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Pricing Methodology" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Competitors to Watch" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    </DbControls>
  </DbControls>
  <DbControls Type="Data" 
      TopicId="Emerging Product Profiles"
      Url="~/DiseaseAnalysis/DisAnEmergingProductProfilesData.aspx">
    <DbControls Type="Data" 
        TopicId="Survey Data" 
        Url="~/DiseaseAnalysis/DisAnEmergingProductProfilesData.aspx" />
    <DbControls 
        TopicId="Analysis" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
  </DbControls>
  <DbControls Type="Data" 
      TopicId="Patient Population Size"
      Url="~/DiseaseAnalysis/DisAnPatientPopulationData.aspx">
    <DbControls Type="Data" 
        TopicId="Data" 
        Url="~/DiseaseAnalysis/DisAnPatientPopulationData.aspx" />
    <DbControls 
        TopicId="Methodology" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
  </DbControls>
  <DbControls 
      TopicId="Decision Trees and Medical Practice"
      Url="~/DiseaseAnalysis/DisAnAnalysis.aspx">
    <DbControls 
        TopicId="Decision Trees" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx">
      <DbControls 
          TopicId="United States" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="France" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Germany" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Italy" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Spain" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="United Kingdom" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="Japan" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    </DbControls>
    <DbControls 
        TopicId="Medical Practice" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
  </DbControls>
  <DbControls 
      TopicId="Key Prescriber Segmentation" 
      Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
  <DbControls Type="Data" 
      TopicId="Market Forecast"
      Url="~/DiseaseAnalysis/DisAnMarketForecastData.aspx">
    <DbControls Type="Data" 
        TopicId="Data" 
        Url="~/DiseaseAnalysis/DisAnMarketForecastData.aspx" />
    <DbControls 
        TopicId="Market Forecast Analysis" 
        Url="~/DiseaseAnalysis/DisAnAnalysis.aspx">
      <DbControls 
          TopicId="Outlook for Emerging Therapies" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
      <DbControls 
          TopicId="2005-2015 Sales" 
          Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
    </DbControls>
  </DbControls>
  <DbControls Type="Data" 
      TopicId="Disease Severity in the Untreated Patient"
      Url="~/DiseaseAnalysis/DisAnDiseaseSeverityData.aspx">
    <DbControls Type="Data" 
        TopicId="Data" 
        Url="~/DiseaseAnalysis/DisAnDiseaseSeverityData.aspx" />
  </DbControls>
  <DbControls 
     TopicId="Physician List" 
     Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
  <DbControls 
     TopicId="Bibliography" 
     Url="~/DiseaseAnalysis/DisAnAnalysis.aspx" />
</Nodes>
 
While you have provided an (overly) extensive sample document, I find the problem statement a bit too terse. Now, having said that...

This would seem a place for a recursive solution, which is normal for many/most XSLT. During a single transformation, generate-id() will generate an identical id for a given node each time it is called. So, while processing a child node, I can invoke generate-id(..) and get the same id that I got when I invoked generate-id(.) when processing the parent.

Now, for the singularity of having a self reference for the Executive Summary (which I don't totally understand) it would seen that a well placed xsl:if might do the job.

Tom Morrison
 
Ok, I want the generateid to be numerical and not string based. How do I go about that? Also, I agee with your statement

"Now, for the singularity of having a self reference for the Executive Summary (which I don't totally understand)"

I don't need to assign a parentid to a parent.

Can you give me an xslt example to do this, thus far I have this.

<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="html" indent="yes" encoding="UTF-8" />
<xsl:strip-space elements="*" />

<xsl:template match="/Nodes">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="DbControls">

<p>
<a href="{generate-id()}"></a>
</p>
<xsl:apply-templates />

</xsl:template>

</xsl:stylesheet>
 
Here is a text producing stylesheet to illustrate a mechanism which might prove useful.
Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="text"/>
<xsl:variable name="nodeList" select="//*"/>
<xsl:variable name="nodeListLength" select="count($nodeList)"/>

<xsl:template match="/">
	<xsl:apply-templates select="*" />
</xsl:template>

<xsl:template match="*">
	<xsl:variable name="myID" select="generate-id()"/>
	<xsl:variable name="parentID" select="generate-id(..)"/>
	<xsl:text>I am </xsl:text>
	<xsl:value-of select="concat(concat(local-name(), ' '), count(preceding::*) + count(ancestor::*))"/>
	<xsl:text>  and my parent is </xsl:text>
	<xsl:value-of select="concat(concat(local-name(..), ' '), count(../preceding::*) + count(../ancestor::*))"/>
	<xsl:text>
</xsl:text>
	<xsl:apply-templates select="*"/>
</xsl:template>

</xsl:stylesheet>
I used your XML document from the original post.
Code:
I am Nodes 0  and my parent is #document 0
I am DbControls 1  and my parent is Nodes 0
I am DbControls 2  and my parent is Nodes 0
I am DbControls 3  and my parent is DbControls 2
I am DbControls 4  and my parent is DbControls 2
I am DbControls 5  and my parent is DbControls 4
I am DbControls 6  and my parent is DbControls 4
I am DbControls 7  and my parent is Nodes 0
I am DbControls 8  and my parent is DbControls 7
I am DbControls 9  and my parent is DbControls 8
I am DbControls 10  and my parent is DbControls 7
I am DbControls 11  and my parent is DbControls 10
I am DbControls 12  and my parent is DbControls 10
I am DbControls 13  and my parent is DbControls 10
I am DbControls 14  and my parent is DbControls 10
I am DbControls 15  and my parent is DbControls 10
I am DbControls 16  and my parent is Nodes 0
I am DbControls 17  and my parent is DbControls 16
I am DbControls 18  and my parent is DbControls 16
I am DbControls 19  and my parent is DbControls 16
I am DbControls 20  and my parent is Nodes 0
I am DbControls 21  and my parent is DbControls 20
I am DbControls 22  and my parent is DbControls 20
I am DbControls 23  and my parent is DbControls 22
I am DbControls 24  and my parent is DbControls 20
I am DbControls 25  and my parent is DbControls 24
I am DbControls 26  and my parent is DbControls 24
I am DbControls 27  and my parent is DbControls 24
I am DbControls 28  and my parent is Nodes 0
I am DbControls 29  and my parent is DbControls 28
I am DbControls 30  and my parent is DbControls 28
I am DbControls 31  and my parent is Nodes 0
I am DbControls 32  and my parent is DbControls 31
I am DbControls 33  and my parent is DbControls 31
I am DbControls 34  and my parent is Nodes 0
I am DbControls 35  and my parent is DbControls 34
I am DbControls 36  and my parent is DbControls 34
I am DbControls 37  and my parent is DbControls 34
I am DbControls 38  and my parent is DbControls 34
I am DbControls 39  and my parent is DbControls 38
I am DbControls 40  and my parent is DbControls 38
I am DbControls 41  and my parent is DbControls 38
I am DbControls 42  and my parent is Nodes 0
I am DbControls 43  and my parent is DbControls 42
I am DbControls 44  and my parent is DbControls 42
I am DbControls 45  and my parent is DbControls 44
I am DbControls 46  and my parent is DbControls 44
I am DbControls 47  and my parent is DbControls 44
I am DbControls 48  and my parent is DbControls 44
I am DbControls 49  and my parent is DbControls 44
I am DbControls 50  and my parent is Nodes 0
I am DbControls 51  and my parent is DbControls 50
I am DbControls 52  and my parent is DbControls 50
I am DbControls 53  and my parent is Nodes 0
I am DbControls 54  and my parent is DbControls 53
I am DbControls 55  and my parent is DbControls 53
I am DbControls 56  and my parent is Nodes 0
I am DbControls 57  and my parent is DbControls 56
I am DbControls 58  and my parent is DbControls 57
I am DbControls 59  and my parent is DbControls 57
I am DbControls 60  and my parent is DbControls 57
I am DbControls 61  and my parent is DbControls 57
I am DbControls 62  and my parent is DbControls 57
I am DbControls 63  and my parent is DbControls 57
I am DbControls 64  and my parent is DbControls 57
I am DbControls 65  and my parent is DbControls 56
I am DbControls 66  and my parent is Nodes 0
I am DbControls 67  and my parent is Nodes 0
I am DbControls 68  and my parent is DbControls 67
I am DbControls 69  and my parent is DbControls 67
I am DbControls 70  and my parent is DbControls 69
I am DbControls 71  and my parent is DbControls 69
I am DbControls 72  and my parent is Nodes 0
I am DbControls 73  and my parent is DbControls 72
I am DbControls 74  and my parent is Nodes 0
I am DbControls 75  and my parent is Nodes 0

Hope this helps.

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top