使用 XSLT 将 XML 文件转换为另一个 XML 文件 [英] Converting XML file to another XML file using XSLT
本文介绍了使用 XSLT 将 XML 文件转换为另一个 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 文件 2?我想将地址表示为单行,并在行尾添加一个新属性 [country-Australia].我做了剩下的.我在地址行上苦苦挣扎
How should i convert xml file 1 to xml fle 2 using xslt? i want to represent the address as the single line and add a new attribute [country- Australia] to end of the line. i did the rest of it . i'm struggling with address line
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>
推荐答案
这种转变:
<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>
说明:使用和覆盖身份规则.
Explanation: Using and overriding the identity rule.
这篇关于使用 XSLT 将 XML 文件转换为另一个 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文