XML-XML with XSLT excluding certain value-based elements

I am trying to create an xml file that is almost identical to the first, but I need my xsl sheet to convert it to include only types> "apartments" or "units" and have a value of more than 1 in <numberofBedrooms> I'm completely lost. Any help would be greatly appreciated!

my initial xml sheet is as follows:

<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> </rentalproperties> 

My xsl:

 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:element name="RentalProperties"> <xsl:apply-templates select="rentalProperties/property"/> </xsl:element> </xsl:template> <xsl:template match="rentalProperties/property/type"> <xsl:element name="type" > <xsl:value-of select="."/> </xsl:element> </xsl:template> <xsl:template match="rentalProperties/property/price"> <xsl:element name="price" > <xsl:value-of select="."/> </xsl:element> </xsl:template> <xsl:template match="rentalProperties/property/address"> <xsl:element name="address" > <xsl:value-of select="streetNo"/><xsl:value-of select="street"/><xsl:value-of select="suburb"/><xsl:value-of select="state"/><xsl:value-of select="zipcode"/> </xsl:element> </xsl:template> <xsl:template match="rentalProperties/property/numberOfBedrooms"> <xsl:element name="numberOfBedrooms" > <xsl:value-of select="."/> </xsl:element> </xsl:template> <xsl:template match="rentalProperties/property/description"> <xsl:element name="description" > <xsl:value-of select="."/> </xsl:element> </xsl:template> 

The result will be something like this:

 <RentalProperties> <property> <type>apartment</type> <price>400</price> <address>4/3,Acheron Avenue,Camberwell,VIC,3124, Australia</address> <numberOfBedrooms>2</numberOfBedrooms> <description>This two bedroom apartment is located in quiet tree lined street, just minutes from tram and easy walk to Camberwell Junction and train. Positioned on the 1st floor with sunny north facing lounge and balcony. </description> </property> </RentalProperties> 

thanks

+4
source share
2 answers

This is probably one of the simplest possible solutions (without obvious conventions in general, all output is produced only by an identification template) :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <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) ] "/> </xsl:stylesheet> 

when applied to this XML document (one provided - corrected and enlarged):

 <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> 

creates the desired, correct result (only one of the properties that satisfy all the restrictions is displayed):

 <rentalProperties> <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> </rentalProperties> 

The explanation . Rule override with an empty template matching any unwanted property.

+3
source
 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="property"> <xsl:if test="normalize-space(./type/text()) = 'unit' or normalize-space(./type/text()) = 'apartment'"> <xsl:copy-of select="."/> </xsl:if> </xsl:template> </xsl:stylesheet> 

The above xml copies everything to the output, but in the case of the "property" element, it only copies it if the type contains a text flat or block. I hope I gave you a hint.

0
source

All Articles