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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xslt conversion (from content to tag)

Status
Not open for further replies.

lowbk

Technical User
Nov 26, 2001
162
SG
Hi, I'm new to XML and need some advice on XSL.
suppose i want to convert the following xml to
---------------------------------------------------
<?xml version=&quot;1.0&quot;?>
<body>
<section>
<caption>previous history</caption>
<list>
<item>
<caption>diabetic ketoacidosis</caption>
<content>dk</content>
</item>
<item>
<caption>severe hypoglycaemia</caption>
<content>sh</content>
</item>
</list>
</section>
<section>
<caption>comments</caption>
<list>
<item>
<caption>disease description</caption>
<content>early stage</content>
</item>
</list>
</section>
</body>
-------------------------------------------------------
TO
-------------------------------------------------------
<body>
<previous history>
<list>
<diabetic ketoacidosis>dk</diabetic ketoacidosis>
<severe hypoglycaemia>sh</severe hypoglycaemia>
</list>
</previous history>
<comments>
<list>
<disease description>early stage</disease description>
</list>
</comments>
</body>
--------------------------------------------------------
My XSL is as below, but it is not correct

<?xml version=&quot;1.0&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;<xsl:eek:utput method=&quot;xml&quot; indent=&quot;yes&quot;/>

<xsl:template match=&quot;/&quot;>
<body>
<xsl:apply-templates select=&quot;/levelone/body/section&quot;/>
</body>
</xsl:template>

<xsl:template match=&quot;section&quot;>
<xsl:element name=&quot;{./caption}&quot;>
<list>
<xsl:element name=&quot;{../list/item/caption}&quot;>
<xsl:value-of select=&quot;../list/item/content&quot;/>
</xsl:element>
</list>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
-----------------------------------------------------------
any kind soul pls advise
 
in your output it is not correct xml to create tags which contain spaces.

<disease description> --> this is not a valid tag.

if you can change the design on the output xml then it would be best to structure the history section more like....

<PreviousHistory>
<list>
<condition id=&quot;dk&quot;>diabetic ketoacidosis</condition>
<condition id=&quot;sh&quot;>severe hypoglycaemia</condition>
</list>
</PreviousHistory>

From this the xsl should be easier and the rest of the document should be structured in a similar way.

it is possible to exactly recreate the output you want but it would start getting messy and wouldn't be xml any more.
:)
 
suppose I restructure the xml to be

<body>
<section caption=&quot;previous history&quot;>
<list>
<condition id=&quot;dk&quot;>diabetic ketoacidosis</condition>
<condition id=&quot;sh&quot;>severe hypoglycaemia</condition>
</list>
</section>
</body>

how to write the xslt for the &quot;<section caption=previous history>&quot;?
any kind soul pls advise.
 
<section caption=&quot;{caption}&quot;>

the &quot;{}&quot; are the bits to note. it's the equivalent of an <xsl:value-of>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top