2

有人可以告诉我如何删除所有子元素并将它们放在父节点中的逗号分隔行中,同时完全删除thumbURl图像title元素我已经必须对此文件进行转换,这是 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>

这就是我需要修改的数据的样子

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

这就是我需要对数据做的事情:

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

1 回答 1

1

像这样的东西怎么样...

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>

输出

<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>
于 2012-08-04T20:51:22.797 回答