Converting XML file to another XML file using XSLT

后端 未结 4 1543
青春惊慌失措
青春惊慌失措 2020-12-08 01:15

XML file 1:



    
        House 
        

        
相关标签:
4条回答
  • 2020-12-08 01:49

    This transformation:

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

    when applied on the provided XML document:

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

    produces the wanted, correct result:

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

    Explanation: Using and overriding the identity rule.

    0 讨论(0)
  • 2020-12-08 01:55

    You could introduce a new template for the address block using

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

    and call it with

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

    before the <numberOfBedrooms> element. This can also be done using the concat function, whereas the correct syntax I don't remember right now.

    0 讨论(0)
  • 2020-12-08 02:00

    You can try something like:

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

    This loops over all the children of the address tag in xml1.

    0 讨论(0)
  • 2020-12-08 02:11
    <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>
    
    0 讨论(0)
提交回复
热议问题