Question

Can someone tell me how to remove all the child elements and place them in a comma-separated line in the parent node, also removing thumbURl and title element of images completely I already have to do a transformation of this file and this is the xs:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="@* | node()">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()" />
   </xsl:copy>
   </xsl:template>
   <xsl:template match="propertyURL/text()">
     <xsl:call-template name="string-replace-all">
       <xsl:with-param name="text"    select="."/>
       <xsl:with-param name="replace" select="'?ref=0'"/>
       <xsl:with-param name="by"      select="'?ref=1000'"/>
     </xsl:call-template>
   </xsl:template>
   <xsl:template name="string-replace-all">
     <xsl:param name="text"/>
     <xsl:param name="replace"/>
     <xsl:param name="by"/>
     <xsl:choose>
       <xsl:when test="contains($text, $replace)">
         <xsl:value-of select="substring-before($text,$replace)"/>
         <xsl:value-of select="$by"/>
         <xsl:call-template name="string-replace-all">
           <xsl:with-param name="text" select="substring-after($text,$replace)"/>
           <xsl:with-param name="replace" select="$replace"/>
           <xsl:with-param name="by" select="$by"/>
         </xsl:call-template>
       </xsl:when>
        <xsl:otherwise>
         <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

this is what the data i need to modifiy looks like

 <p id="1">
  <amenities>
   <amenity>Air conditioning</amenity>
   <amenity>Lift/elevator</amenity> 
   <amenity>Pets accepted</amenity>
   <amenity>Guarded car park</amenity>
   <amenity>Private parking</amenity>
   <amenity>Garage</amenity>
   <amenity>Limousine service</amenity>
  </amenities>
  <images> 
   <image num="1">
    <imageURL>path/to/image.url1</imageURL>
    <thumbURL>..</thumbURL>  
    <title>..</title>
   </image>
   <image num="2">
    <imageURL>path/to/image.url2</imageURL>
    <thumbURL>..</thumbURL>
     <title>..</title>
    </image>
   </images>
  </p>

this is what i need to do with the data:

<p>
 <amenities>
   Air conditioning,Lift/elevator,Pets accepted,Guarded car park,....
 </amenities>
 <images>
  <imageURL>path/to/image.url1</imageURL>
  <imageURL>path/to/image.url2</imageURL>
 </images>
</p>
Was it helpful?

Solution

How about something like this...

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="amenity">
        <xsl:value-of select="."/>
        <xsl:if test="following-sibling::amenity">,</xsl:if>
    </xsl:template>

    <xsl:template match="image">
        <xsl:apply-templates select="imageURL"/>
    </xsl:template>

</xsl:stylesheet>

Output

<p>
   <amenities>Air conditioning,Lift/elevator,Pets accepted,Guarded car park,Private parking,Garage,Limousine service</amenities>
   <images>
      <imageURL>path/to/image.url1</imageURL>
      <imageURL>path/to/image.url2</imageURL>
   </images>
</p>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top