微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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 fle 2?
我想将地址表示为单行,并将一个属性[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>
这种转变:
<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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。