XML文件1:
<?xml version="1.0"?>
<rentalProperties>
<property contact ="1">
<type>House </type>
<price>420</price>
<address>
<streetNo>1</streetNo>
<street>Wavell Street</street>
<suburb>Box Hill</suburb>
<state>VIC</state>
<zipcode>3128</zipcode>
</address>
<numberOfBedrooms>3</numberOfBedrooms>
<numberOfBathrooms>1</numberOfBathrooms>
<garage>1</garage>
</property>
XML文件2:
<?xml version="1.0"?>
<rentalProperties>
<property contact ="1">
<type>House </type>
<price>420</price>
<address>1 wavell street,Box Hill,VIC,Australia</address>
<numberOfBedrooms>3</numberOfBedrooms>
<numberOfBathrooms>1</numberOfBathrooms>
<garage>1</garage>
</property>
我应该如何使用xslt将xml文件1转换为xml文件? 我想将地址表示为单行,并将新属性[country- Australia]添加到行尾。我完成了剩下的工作。我正在努力解决地址线
XSLT文件:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" type="text/css" href="style.css">
<xsl:template match="/">
<rentalProperties>
<property>
<xsl:attribute name="contact"><xsl:value-of select='@contact'/></xsl:attribute>
<type><xsl:value-of select="type"/></type>
<price><xsl:value-of select="price"/></price>
<numberOfBedrooms><xsl:value-of select="numberOfBedrooms"/></numberOfBedrooms>
<numberOfBathrooms><xsl:value-of select="numberOfBathrooms"/></numberOfBathrooms>
<garage><xsl:value-of select="garage"/></garage>
</property>
</rentalProperties>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:29)
此转化:
<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="address">
<xsl:copy>
<xsl:value-of select=
"concat(streetNo, ' ', street, ',',
suburb,',', state,', Australia')
"/>
</xsl:copy>
</xsl:template>
<xsl:template match="address/node()"/>
</xsl:stylesheet>
应用于提供的XML文档:
<rentalProperties>
<property contact ="1">
<type>House </type>
<price>420</price>
<address>
<streetNo>1</streetNo>
<street>Wavell Street</street>
<suburb>Box Hill</suburb>
<state>VIC</state>
<zipcode>3128</zipcode>
</address>
<numberOfBedrooms>3</numberOfBedrooms>
<numberOfBathrooms>1</numberOfBathrooms>
<garage>1</garage>
</property>
</rentalProperties>
生成想要的正确结果:
<rentalProperties>
<property contact="1">
<type>House </type>
<price>420</price>
<address>1 Wavell Street,Box Hill,VIC, Australia</address>
<numberOfBedrooms>3</numberOfBedrooms>
<numberOfBathrooms>1</numberOfBathrooms>
<garage>1</garage>
</property>
</rentalProperties>
解释:使用并覆盖 identity rule 。
答案 1 :(得分:3)
您可以使用
为地址块引入新模板<xsl:template match="address">
<xsl:value-of select="streetNo" />
<xsl:text> </xsl:text>
<xsl:value-of select="street" />
<xsl:text>,</xsl:text>
<xsl:value-of select="suburb" />
<xsl:text>,</xsl:text>
<xsl:value-of select="state" />
<xsl:text>,</xsl:text>
<xsl:value-of select="zipcode" />
</xsl:template>
并用
调用它<xsl:apply-templates select="address" />
在<numberOfBedrooms>
元素之前。这也可以使用concat
函数来完成,而正确的语法我现在还不记得了。
答案 2 :(得分:1)
您可以尝试以下内容:
<address>
<xsl:for-each select="address/*">
<xsl:value-of select="."/>,
</xsl:for-each>
Australia
</address>
这将循环遍历xml1中地址标记的所有子项。
答案 3 :(得分:-3)
<rentalProperties>
<property contact="1">
<type>House </type>
<price>420</price>
<address>1 Wavell Street,Box Hill,VIC,3128</address>
<numberOfBedrooms>3</numberOfBedrooms>
<numberOfBathrooms>1</numberOfBathrooms>
<garage>1</garage>
</property>
</rentalProperties>