我想将下面的XML文件转换为另一个由XSLT完成的XML文件。
XML文件1
<?xml version="1.0"?>
<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>
我使用下面的XSL代码来获取要返回的新XML。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/>
<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) or not(propertyavailable='yes')
]
">
</xsl:template>
<xsl:template match="property">
<xsl:element name="property">
<xsl:attribute name="contact">
<xsl:value-of select="contact" />
</xsl:attribute>
<xsl:element name="type">
<xsl:value-of select="type" />
</xsl:element>
<xsl:element name="price">
<xsl:value-of select="price" />
</xsl:element>
<xsl:element name="address">
<xsl:value-of select=
"concat(streetNo, ' ', street, ', ',
suburb,', ', state,' ' , zipcode,', Australia')
"/>
</xsl:element>
<xsl:element name="numberofBedrooms">
<xsl:value-of select="numberofBedrooms" />
</xsl:element>
<xsl:element name="numberofBathrooms">
<xsl:value-of select="numberofBathrooms" />
</xsl:element>
<xsl:element name="description">
<xsl:value-of select="description" />
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我想用上面的XSL将StreetNo,街道,郊区,州,邮政编码连接到地址元素,但我现在得到的输出是:
<?xml version="1.0" encoding="UTF-8"?>
<rentalProperties>
<property contact="0407106699">
<type>apartment</type>
<price>450</price>
<address>, , , Australia</address>
<numberofBedrooms>3</numberofBedrooms>
<numberofBathrooms>1</numberofBathrooms>
<description>Recently completed, the ultra-stylish and light-filled dimensions of this brand new first floor Scotch Hill apartment provide an enviable lifestyle of sophistication,...</description>
</property>
<property contact="0398187838">
<type>unit</type>
<price>460</price>
<address>, , , Australia</address>
<numberofBedrooms>2</numberofBedrooms>
<numberofBathrooms>1</numberofBathrooms>
<description>This generous 2 bedroom villa unit located close to Glenferrie Road boasts a bright living area with polished floor boards opening to a low maintenance courtyard, fully renovated kitchen with gas cooking and dishwasher, built in robes to both bedrooms, bathroom and separate laundry.</description>
</property>
<property contact="0398105000">
<type>apartment</type>
<price>420</price>
<address>, , , Australia</address>
<numberofBedrooms>2</numberofBedrooms>
<numberofBathrooms>1</numberofBathrooms>
<description>Benefiting beautifully from its 1st floor position directly overlooking the leafy environs of Urquhart St, this sun-bathed apartment is located near exciting eateries, boutiques, tram, train and Swinburne Uni.</description>
</property>
</rentalProperties>
地址元素没有获取其他元素值,并且将区域留空。
<address>, , , Australia</address>
此段的XSL代码是否有问题:
<xsl:element name="address">
<xsl:value-of select=
"concat(streetNo, ' ', street, ', ',
suburb,', ', state,' ' , zipcode,', Australia')
"/>
</xsl:element>
答案 0 :(得分:0)
您的模板与property
匹配,select
是XPath streetNo
表达式的当前上下文。在该模板中,您可以选择street
,<xsl:template match="property">
...
<xsl:element name="address">
<xsl:value-of select=
"concat(streetNo, ' ', street, ', ',
suburb,', ', state,' ' , zipcode,', Australia')
"/>
</xsl:element>
等:
property/streetNo
这意味着您实际上正在选择property/street
,<property available="yes" contact="0423020899">
...
<address>
<streetNo>116</streetNo>
...
等
根据您的XML来源:
property
没有这样的元素,因此表达式不会匹配任何内容,结果会得到空字符串。
在address
上下文中,您想要的元素位于<xsl:element name="address">
<xsl:value-of select=
"concat(address/streetNo, ' ', address/street, ', ',
address/suburb,', ', address/state,' ' , address/zipcode,', Australia')
"/>
</xsl:element>
内。您可以通过向选择器添加缺少的位置步骤来修复表达式:
{{1}}