1

I am trying to convert XML file to another XML file using XSLT.

Input file is:

    <?xml version="1.0" encoding="UTF-8"?>
    <listings>
      <listing>
        <listingid>4789231</listingid>
        <listingtype>Land</listingtype>
        <saletype>Sale</saletype>
        <originalsource>CBC Online</originalsource>
        <sourcekey>4789231</sourcekey>
        <cbcofficekey>EDG7023975</cbcofficekey>
        <status>Active</status>
        <name>7218 Bailey Rd</name>
        <address1>7218 Bailey Rd</address1>
        <address2></address2>
        <address3></address3>
        <city>Pearland</city>
        <county>Brazoria</county>
        <stateprovince>TX</stateprovince>
        <postalcode>77584</postalcode>
        <country>US</country>
        <nearestmsa>Yuba City, CA</nearestmsa>
        <baseprice currency="USD">185000.00</baseprice>
        <pricetype>saleprice</pricetype>
        <displayprice>true</displayprice>
        <geolocation>
          <latitude>29.5298</latitude>
          <longitude>-95.3322</longitude>
        </geolocation>
        <preferredunitsofmeasure>IMPERIAL</preferredunitsofmeasure>
        <displayonwww>true</displayonwww>
        <create_ts>2014-06-20T09:09:21Z</create_ts>
        <lastmod_ts>2014-06-26T11:53:14Z</lastmod_ts>
        <overview>
          <propertyoverview>
            <buildingarea>3691.00</buildingarea>
            <yearbuilt>1960</yearbuilt>
            <priceperarea currency="USD">4.99</priceperarea>
          </propertyoverview>
        </overview>
     </listing>
    </listings>

Output I want is:

<?xml version="1.0" encoding="UTF-8"?>
<listings>
  <listing>
    <listingid>4789231</listingid>
    <listingtype>Land</listingtype>
    <saletype>Sale</saletype>
    <originalsource>CBC Online</originalsource>
    <sourcekey>4789231</sourcekey>
    <cbcofficekey>EDG7023975</cbcofficekey>
    <status>Active</status>
    <name>7218 Bailey Rd</name>
    <address1>7218 Bailey Rd</address1>
    <address2></address2>
    <address3></address3>
    <city>Pearland</city>
    <county>Brazoria</county>
    <stateprovince>TX</stateprovince>
    <postalcode>77584</postalcode>
    <country>US</country>
    <nearestmsa>Yuba City, CA</nearestmsa>
    <baseprice currency="USD">185000.00</baseprice>
    <pricetype>saleprice</pricetype>
    <displayprice>true</displayprice>
    <geolocation_latitude>29.5298</geolocation_latitude>
    <geolocation_longitude>-95.3322</geolocation_<longitude>
    <preferredunitsofmeasure>IMPERIAL</preferredunitsofmeasure>
    <displayonwww>true</displayonwww>
    <create_ts>2014-06-20T09:09:21Z</create_ts>
    <lastmod_ts>2014-06-26T11:53:14Z</lastmod_ts>
    <overview_propertyoverview_buildingarea>3691.00</overview_propertyoverview_buildingarea>
    <overview_propertyoverview_yearbuilt>1960</overview_propertyoverview_yearbuilt>
    <overview_propertyoverview_priceperarea>4.99</overview_propertyoverview_priceperarea>      
 </listing>
</listings>

If parent has child nodes then output xml should contain node with parent plus child node name. Here I want parent nodes should be concatenated with child node name according to their hierarchy.

1 Answer 1

2

Use the identity template and these additional templates.

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

<xsl:template match="listing//*[not(*)]">
  <xsl:variable name="newName">
    <xsl:for-each select="ancestor-or-self::*[count(ancestor::*) &gt; 1]">
      <xsl:value-of select="name()" />
      <xsl:if test="position() &lt; last()">_</xsl:if>
    </xsl:for-each>
  </xsl:variable>
  <xsl:element name="{$newName}">
    <xsl:apply-templates select="node() | @*" />
  </xsl:element>    
</xsl:template>

The expression ancestor-or-self::*[count(ancestor::*) &gt; 1], as seen from the context of a <yearbuilt> node:

<listings>                            <!-- not selected -->
  <listing>                           <!-- not selected -->
    <overview>                        <!-- selected     -->
      <propertyoverview>              <!-- selected     -->
        <yearbuilt>1960</yearbuilt>   <!-- selected     -->
Sign up to request clarification or add additional context in comments.

10 Comments

Hi Tomalak.. thanks for your answer. I tried with your XSL file. But it's not generating XML elements. It generates only value in file not a with node. I am transforming using c sharp.
I tried Tomalak's solution and it works. You should add an identity template, though, per his suggestion.
Joel. I found the issue because of which i am not getting the desired output. Its because of this line- <listings xmlns="cbcworldwide.com/ns/listings" If i remove the attribute xmlns then its working fine. Plus listings/listing tag is not coming in final output. I am trying for that. Please help as i am new to XSLT.
Add the namespace declaration to your <xsl:stylesheet> and you should be all-right.
@Tomalak Tried.. But no luck.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.