2011-05-14 121 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