2011-05-14 105 views
13

XML文件1另一个XML文件:转换XML文件来使用XSLT

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

回答

1

你可以尝试这样的:

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

这遍历在XML1地址标签的所有孩子。

+0

感谢ü非常感谢 – shavinda 2011-05-14 15:46:17

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> 

地址块和<numberOfBedrooms>元素之前

<xsl:apply-templates select="address" /> 

调用它。这也可以使用concat函数完成,而我现在不记得的正确语法。

+0

ü非常 – shavinda 2011-05-14 15:45:50

25

该转化

<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

+0

特殊的参考和这里良好的工作 – Eon 2014-12-02 14:26:15

-1
<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> 
+2

代码只有答案几乎总是可以通过添加一些说明语句提高;你可以[编辑]你的答案来这样做。 – 2016-01-28 16:18:25

+0

这里有什么解决方案? – 2017-12-06 23:10:37