微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用XSLT的XML到XML省略了基于值的特定元素

我正在尝试生成一个与初始文件几乎完全相同的xml文件,但我需要将我的xsl表单转换为仅包含带有<类型>无论是“公寓”还是“单位”,其价值都大于1. numberofbedrooms>我完全失去了,任何帮助将不胜感激!
我的初始xml表如下:

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

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

输出将是这样的:

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

谢谢

解决方法

这可能是最简单的解决方案之一(根本没有显式条件,所有输出仅由标识模板生成):

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

当应用于此XML文档时(提供的 – 已更正和放大):

<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,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,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,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,are served by a stylishly renovated central bathroom and second sky-lit bathroom to the rear.</description>
</property>
</rentalProperties>

生成所需的正确结果(只输出满足所有约束的属性之一):

<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,are served by a stylishly renovated central bathroom and second sky-lit bathroom to the rear.</description>
   </property>
</rentalProperties>

说明:使用匹配任何不需要的属性的空模板覆盖identity rule.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。