使用XSLT将XML文件转换为另一个XML文件

使用XSLT将XML文件转换为另一个XML文件,xml,xslt,Xml,Xslt,XML文件1: <?xml version="1.0"?> <rentalProperties> <property contact ="1"> <type>House </type> <price>420</price> <address> <streetNo>1</streetNo>

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>

房子
420
1.
韦维尔街
博克斯山
维克
3128
3.
1.
1.
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>

房子
420
澳大利亚维多利亚州Box Hill wavell街1号
3.
1.
1.
如何使用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>

您可以尝试以下方法:

<address>
    <xsl:for-each select="address/*">
       <xsl:value-of select="."/>, 
    </xsl:for-each>
    Australia
</address>

, 
澳大利亚

这将循环xml1中地址标记的所有子项。

您可以使用

<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" />


元素之前。这也可以使用
concat
函数来完成,而正确的语法我现在不记得了。

此转换

<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>
<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>

应用于提供的XML文档时

<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>
<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>

房子
420


房子
420
维多利亚州博克斯山韦维尔街1号,邮编:3128
3.
1.
1.

code-only答案几乎总是可以通过添加一些解释性语句来改进;您可以选择您的答案。这里的解决方案是什么?是否也可以在输出中包含xml头:@mgouin,它被称为“xml声明”。是,只需从
指令中删除
省略xml声明=“Yes”
属性