Hello,

<rentalProperties>
    <property available="yes" contact="0455 455 014">
        <type>apartment</type>
        <price>500</price>
    <address>
        <streetNo>15</streetNo>
        <street>Armadale Street</street>
        <suburb>Armadale</suburb>
        <state>VIC</state>
        <ZipCode>3143</ZipCode>
    </address>
    <numberofBedrooms>2</numberofBedrooms>
    <numberofBathrooms>1</numberofBathrooms>
    <garage>1</garage>
    <description>Top floor 2 bed Apartment in a Leafy Street with 2 carspaces</description>
    </property>
</rentalProperty>

I have to transform this into xml format using xslt. The address should be separated by comma, and zipcode shouldnt be included.
Can you help me out?

Dani AI

Generated

The posted XML will transform fine once it’s well-formed (fix the mismatched root closing tag). To meet ’s requirement — collapse the address into a single comma-separated text node and omit ZipCode — use an identity transform plus a special template for address. This XSLT 1.0 stylesheet preserves attributes (for example available and contact) and leaves other nodes unchanged while producing a single address element made from the non-ZipCode child elements.

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

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

  <!-- collapse address children (skip ZipCode) into a single comma-separated text node -->
  <xsl:template match="address">
    <address>
      <xsl:for-each select="*[local-name() != 'ZipCode' and string-length(normalize-space(.)) > 0]">
        <xsl:if test="position() &gt; 1"><xsl:text>, </xsl:text></xsl:if>
        <xsl:value-of select="normalize-space(.)"/>
      </xsl:for-each>
    </address>
  </xsl:template>
</xsl:stylesheet>

Notes: the for-each skips empty children and inserts , only between items (no trailing comma). If you use an XSLT 2.0 processor you can replace the loop with string-join() for a shorter expression. Validate the input XML first — a mismatched closing tag will cause the XSLT to fail. This answers ’s request for an example while implementing ’s transformation rule.

Recommended Answers

All 3 Replies

should look like the result

Please an example

 <rentalProperties>
        <property available="yes" contact="0455 455 014">
            <type>apartment</type>
            <price>500</price>
        <address>
            <streetNo>15</streetNo>
            <street>Armadale Street</street>
            <suburb>Armadale</suburb>
            <state>VIC</state>
            <ZipCode>3143</ZipCode>
        </address>
        <numberofBedrooms>2</numberofBedrooms>
        <numberofBathrooms>1</numberofBathrooms>
        <garage>1</garage>
        <description>Top floor 2 bed Apartment in a Leafy Street with 2 carspaces</description>
        </property>
    </rentalProperty>

where is the orgin xml
you show me the output xml?

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.