XML to XML with XSLT 根据值省略特定元素 [英] XML to XML with XSLT omitting specific elements based on value

查看:25
本文介绍了XML to XML with XSLT 根据值省略特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个与初始文件几乎相同的 xml 文件,但我需要我的 xsl 表将其转换为仅包含带有 < 的文件.'apartment' 或 'unit' 的类型 > 并且在 < 中的值大于 1numberofBedrooms > 我完全迷失了,任何帮助将不胜感激!

我的初始 xml 表如下:

I'm trying to produce an xml file that is pretty much identical to the initial one, but I need my xsl sheet to transform it to only include those with a < type > of either 'apartment' or 'unit' 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 following:

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

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>

输出将类似于:

<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 文档时(提供的文档 - 更正和放大):

when applied on this XML document (the provided one -- 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>

产生想要的、正确的结果(只输出满足所有约束的属性之一):

produces the wanted, correct result (only one of the properties that meets all constraints is output):

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

说明:覆盖身份规则 带有与任何不需要的属性匹配的空模板.

Explanation: Overriding the identity rule with an empty template matching any unwanted property.

这篇关于XML to XML with XSLT 根据值省略特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆