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

Add Missing Elements 1

Status
Not open for further replies.

Elysium

Programmer
Aug 7, 2002
212
0
0
US
To all:

If there's an article on this already, please post the link. Okay, here's what I want to accomplish:

Convert this XML structure:
Code:
<employee>
   <empid>123456</empid>
   <firstname>Jon</firstname>
   <lastname>Doe</lastname>
</employee>
<employee>
   <firstname>Harry</firstname>
   <lastname>Carry</lastname>
</employee>

Into this XML structure:
Code:
<record>
   <empid>123456</empid>
   <firstname>Jon</firstname>
   <lastname>Doe</lastname>
</record>
<record>
   <empid>Null</empid>
   <firstname>Harry</firstname>
   <lastname>Carry</lastname>
</record>

Take note that the second employee was missing the "empid" element in the first XML structure. However, in the second XML structure, it is presenet with "Null" for the value.

I've been messing around with XSLT, but no success so far. Thanks for any and all help with this.

Randy
[afro]
 
This is the kind of thing you want

<xsl:stylesheet version="1.0" xmlns:xsl="
<xsl:eek:utput method="xml"/>

<xsl:template match="employees">

<xsl:apply-templates select="employee" />

</xsl:template>

<xsl:template match="employee">

<record>
<empid>
<xsl:choose>
<xsl:when test="empid"><xsl:value-of select="empid" /></xsl:when>
<xsl:eek:therwise>Null</xsl:eek:therwise>
</xsl:choose>
</empid>
<firstname><xsl:value-of select="firstname" /></firstname>
<lastname><xsl:value-of select="lastname" /></lastname>
</record>

</xsl:template>

</xsl:stylesheet>
 
Awesome!! Works great! Thank you.

Randy
[afro]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top