Hello ,

I have a requirement to convert an xml document to another xml document using xslt. I am new to xml and xslt and need your help in solving this problem.

I have a xml file which looks like below
<Vendor>
<contact Id="1234" CmpnyNm="ABC" ContactNm="Sheryl">
<Phone Number="555-5555" Type="WorkPhone"/>
</Contact>
<contact Id="1234" CmpnyNm="ABC" ContactNm="Sheryl">
<Phone Number="666-6666" Type="WorkPhone"/>
</Contact>
<contact Id="1234" CmpnyNm="ABC" ContactNm="Sheryl">
<Phone Number="999-9999" Type="WorkPhone"/>
</Contact>
</Vendor>

Since the attributes in the contact element, Id,CmpnyNm and ContactNm are same for the three recs and only the phone number is different, I want to have the 3 phone numbers under one contact element itself.

Please see below how I need to have the output xml.

<vendor>
<contact Id="1234" cmpnyNm="ABC" ContactNm="Sheryl">
<Phone Number="555-5555" Type="WorkPhone"/>
<Phone Number="666-6666" Type="WorkPhone"/>
<Phone Number="999-9999" Type="WorkPhone"/>
</Contact>
</Vendor>

Is it possible to have the output in this format?. I tried xslt but looks like it needs complex logic in the xslt to achieve the output, but unfortunately I am not good at xml and xslt.

Any help in solving this problem would be greatly appreciated.

Thank you
Manoj

Hi,

In this case, you have to use a xsl variable which store the Id of the contact.

Then use an xsl foreach on the contacts and an xsl if to determine wether or not the Id is the same. If yes write the contact info . Then write the phone numbers and at the end of the foreach put the value of the current id into your var.

The logic is the same for another language except that's IMHO XSLT is more a templating language and the syntax is sometime really long to type. In any case you will have to understand the xslt basic syntax to achieve this, don't forget to use a good reference.

Good Luck

Hi

try this...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Vendor">
<xsl:element name="Vendor">
<xsl:element name="Contact">
<xsl:attribute name="Id">
<xsl:value-of select="Contact/@Id"/>
</xsl:attribute>
<xsl:attribute name="CmpnyNm">
<xsl:value-of select="Contact/@CmpnyNm"/>
</xsl:attribute>
<xsl:attribute name="ContactNm">
<xsl:value-of select="Contact/@ContactNm"/>
</xsl:attribute>
<xsl:for-each select="Contact">
<xsl:choose>
<xsl:when test="@Id = (preceding-sibling::*/@Id | following-sibling::*/@Id)">
<xsl:copy-of select="Phone"/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

Thanks,
Indy

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>XXXXXXXXX</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="contact">
<p>
Company Nmae: <xsl:apply-templates select="@cmpnyNm"/><br/>
Contact No.: <xsl:apply-templates select="@ContactNm"/>
</p>
</xsl:template>
<xsl:template match="Phone">
<p>
<xsl:apply-templates select="@Type"/> No.: <xsl:apply-templates select="@Number"/>
</p>
</xsl:template>
</xsl:stylesheet>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.