使用XSLT将XML文件转换为另一个XML文件
发布时间:2020-12-16 07:53:21 所属栏目:百科 来源:网络整理
导读:XML文件1: ?xml version="1.0"?rentalProperties property contact ="1" typeHouse /type price420/price address streetNo1/streetNo streetWavell Street/street suburbBox Hill/suburb stateVIC/state zipcode3128/zipcode /address numberOfBedrooms3/n
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 fle 2? 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,Australia</address> <numberOfBedrooms>3</numberOfBedrooms> <numberOfBathrooms>1</numberOfBathrooms> <garage>1</garage> </property> </rentalProperties> 说明:使用和覆盖identity rule. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |