2
votes

I want to convert the below XML file below into another XML file to be done by XSLT.

XML File 1

<?xml version="1.0"?>
    <rentalProperties>
    <property available="yes" contact="0423020892">
        <type>house</type>
        <price>800</price>
        <address>
            <streetNo>116</streetNo>
            <street>Warrigal Road</street>
            <suburb>Camberwell</suburb>
            <state>VIC</state>
        <zipcode>3124</zipcode>
        </address>
        <numberOfBedrooms>4</numberOfBedrooms>
        <description>Ideal for the familly is this charming Californian Bungalow. Comprising a spacious living area, formal dining room plus a huge family/meals area, bright modern well appointed kitchen with dishwasher, gas cooktop and electric oven and heaps of bench space, Four double bedrooms, two in a wing of their own, are served by a stylishly renovated central bathroom and second sky-lit bathroom to the rear.</description>
    </property>
    <property available="yes" contact="0423020899">
        <type>apartment</type>
        <price>500</price>
        <address>
            <streetNo>116</streetNo>
            <street>Water St.</street>
            <suburb>Hornsby</suburb>
            <state>NSW</state>
        <zipcode>2012</zipcode>
        </address>
        <numberOfBedrooms>2</numberOfBedrooms>
        <description>Ideal for the familly is this charming Californian Bungalow. Comprising a spacious living area, formal dining room plus a huge family/meals area, bright modern well appointed kitchen with dishwasher, gas cooktop and electric oven and heaps of bench space, Four double bedrooms, two in a wing of their own, are served by a stylishly renovated central bathroom and second sky-lit bathroom to the rear.</description>
    </property>
    <property available="yes" contact="0423011111">
        <type>unit</type>
        <price>800</price>
        <address>
            <streetNo>7</streetNo>
            <street>Ryan St</street>
            <suburb>Isacs</suburb>
            <state>ACT</state>
        <zipcode>2603</zipcode>
        </address>
        <numberOfBedrooms>1</numberOfBedrooms>
        <description>Ideal for the familly is this charming Californian Bungalow. Comprising a spacious living area, formal dining room plus a huge family/meals area, bright modern well appointed kitchen with dishwasher, gas cooktop and electric oven and heaps of bench space, Four double bedrooms, two in a wing of their own, are served by a stylishly renovated central bathroom and second sky-lit bathroom to the rear.</description>
    </property>
    <property available="yes" contact="04231234567">
        <type>hotel</type>
        <price>1200</price>
        <address>
            <streetNo>4</streetNo>
            <street>Bench St.</street>
            <suburb>Deakin</suburb>
            <state>ACT</state>
        <zipcode>2600</zipcode>
        </address>
        <numberOfBedrooms>4</numberOfBedrooms>
        <description>Ideal for the familly is this charming Californian Bungalow. Comprising a spacious living area, formal dining room plus a huge family/meals area, bright modern well appointed kitchen with dishwasher, gas cooktop and electric oven and heaps of bench space, Four double bedrooms, two in a wing of their own, are served by a stylishly renovated central bathroom and second sky-lit bathroom to the rear.</description>
    </property>
    </rentalProperties>

I am using the below XSL code to get the new XML to be returned.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

 <xsl:template match=
 "property
   [not(contains('|unit|apartment|',
                 concat('|',type,'|')
                 ))
    or
     not(numberofBedrooms > 1) or not(propertyavailable='yes')
   ]
 ">
</xsl:template>

<xsl:template match="property">
<xsl:element name="property">
<xsl:attribute name="contact">
<xsl:value-of select="contact" />
      </xsl:attribute>

<xsl:element name="type">
<xsl:value-of select="type" />
</xsl:element>
<xsl:element name="price">
<xsl:value-of select="price" />
</xsl:element>
<xsl:element name="address">
   <xsl:value-of select=
   "concat(streetNo, ' ', street, ', ',
           suburb,', ', state,' ' , zipcode,', Australia')
   "/>
</xsl:element>
<xsl:element name="numberofBedrooms">
<xsl:value-of select="numberofBedrooms" />
</xsl:element>
<xsl:element name="numberofBathrooms">
<xsl:value-of select="numberofBathrooms" />
</xsl:element>
<xsl:element name="description">
<xsl:value-of select="description" />
</xsl:element>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

I want to concatenate the StreetNo, street, suburb, state, zipcode into the address element with the above XSL but the output I am getting at the moment is:

<?xml version="1.0" encoding="UTF-8"?>
<rentalProperties>
   <property contact="0407106699">
      <type>apartment</type>
      <price>450</price>
      <address>, ,  , Australia</address>
      <numberofBedrooms>3</numberofBedrooms>
      <numberofBathrooms>1</numberofBathrooms>
      <description>Recently completed, the ultra-stylish and light-filled dimensions of this brand new first floor Scotch Hill apartment provide an enviable lifestyle of sophistication,...</description>
   </property>
   <property contact="0398187838">
      <type>unit</type>
      <price>460</price>
      <address>, ,  , Australia</address>
      <numberofBedrooms>2</numberofBedrooms>
      <numberofBathrooms>1</numberofBathrooms>
      <description>This generous 2 bedroom villa unit located close to Glenferrie Road boasts a bright living area with polished floor boards opening to a low maintenance courtyard, fully renovated kitchen with gas cooking and dishwasher, built in robes to both bedrooms, bathroom and separate laundry.</description>
   </property>
   <property contact="0398105000">
      <type>apartment</type>
      <price>420</price>
      <address>, ,  , Australia</address>
      <numberofBedrooms>2</numberofBedrooms>
      <numberofBathrooms>1</numberofBathrooms>
      <description>Benefiting beautifully from its 1st floor position directly overlooking the leafy environs of Urquhart St, this sun-bathed apartment is located near exciting eateries, boutiques, tram, train and Swinburne Uni.</description>
   </property>
</rentalProperties>

The address element is not picking up the other element values and is leaving the areas blank.

<address>, ,  , Australia</address>

Is there something wrong with my XSL code with this segment:

<xsl:element name="address">
   <xsl:value-of select=
   "concat(streetNo, ' ', street, ', ',
           suburb,', ', state,' ' , zipcode,', Australia')
   "/>
</xsl:element>
1

1 Answers

0
votes

Your template matches property, which is the current context for your XPath select expressions. Inside that template, you select streetNo, street, etc:

<xsl:template match="property">
    ...
        <xsl:element name="address">
            <xsl:value-of select=
                "concat(streetNo, ' ', street, ', ',
                suburb,', ', state,' ' , zipcode,', Australia')
                "/>
        </xsl:element>

That means your are actually selecting property/streetNo, property/street, etc.

According to your XML source:

<property available="yes" contact="0423020899">
        ...
        <address>
            <streetNo>116</streetNo>
            ...

there are no such elements, so the expression won't match anything, and you get empty strings as a result.

In the property context, the elements you want are inside address. You can fix the expression by adding that missing location step to your selectors:

<xsl:element name="address">
    <xsl:value-of select=
        "concat(address/streetNo, ' ', address/street, ', ',
        address/suburb,', ', address/state,' ' , address/zipcode,', Australia')
        "/>
</xsl:element>