使用XSLT转换XML到XML(没有父节点)

时间:2014-07-21 15:57:30

标签: xml xslt xslt-1.0

我正在尝试使用XSLT将XML文件转换为另一个XML文件。

输入文件为:

    <?xml version="1.0" encoding="UTF-8"?>
    <listings>
      <listing>
        <listingid>4789231</listingid>
        <listingtype>Land</listingtype>
        <saletype>Sale</saletype>
        <originalsource>CBC Online</originalsource>
        <sourcekey>4789231</sourcekey>
        <cbcofficekey>EDG7023975</cbcofficekey>
        <status>Active</status>
        <name>7218 Bailey Rd</name>
        <address1>7218 Bailey Rd</address1>
        <address2></address2>
        <address3></address3>
        <city>Pearland</city>
        <county>Brazoria</county>
        <stateprovince>TX</stateprovince>
        <postalcode>77584</postalcode>
        <country>US</country>
        <nearestmsa>Yuba City, CA</nearestmsa>
        <baseprice currency="USD">185000.00</baseprice>
        <pricetype>saleprice</pricetype>
        <displayprice>true</displayprice>
        <geolocation>
          <latitude>29.5298</latitude>
          <longitude>-95.3322</longitude>
        </geolocation>
        <preferredunitsofmeasure>IMPERIAL</preferredunitsofmeasure>
        <displayonwww>true</displayonwww>
        <create_ts>2014-06-20T09:09:21Z</create_ts>
        <lastmod_ts>2014-06-26T11:53:14Z</lastmod_ts>
        <overview>
          <propertyoverview>
            <buildingarea>3691.00</buildingarea>
            <yearbuilt>1960</yearbuilt>
            <priceperarea currency="USD">4.99</priceperarea>
          </propertyoverview>
        </overview>
     </listing>
    </listings>

我想要的输出是:

<?xml version="1.0" encoding="UTF-8"?>
<listings>
  <listing>
    <listingid>4789231</listingid>
    <listingtype>Land</listingtype>
    <saletype>Sale</saletype>
    <originalsource>CBC Online</originalsource>
    <sourcekey>4789231</sourcekey>
    <cbcofficekey>EDG7023975</cbcofficekey>
    <status>Active</status>
    <name>7218 Bailey Rd</name>
    <address1>7218 Bailey Rd</address1>
    <address2></address2>
    <address3></address3>
    <city>Pearland</city>
    <county>Brazoria</county>
    <stateprovince>TX</stateprovince>
    <postalcode>77584</postalcode>
    <country>US</country>
    <nearestmsa>Yuba City, CA</nearestmsa>
    <baseprice currency="USD">185000.00</baseprice>
    <pricetype>saleprice</pricetype>
    <displayprice>true</displayprice>
    <geolocation_latitude>29.5298</geolocation_latitude>
    <geolocation_longitude>-95.3322</geolocation_<longitude>
    <preferredunitsofmeasure>IMPERIAL</preferredunitsofmeasure>
    <displayonwww>true</displayonwww>
    <create_ts>2014-06-20T09:09:21Z</create_ts>
    <lastmod_ts>2014-06-26T11:53:14Z</lastmod_ts>
    <overview_propertyoverview_buildingarea>3691.00</overview_propertyoverview_buildingarea>
    <overview_propertyoverview_yearbuilt>1960</overview_propertyoverview_yearbuilt>
    <overview_propertyoverview_priceperarea>4.99</overview_propertyoverview_priceperarea>      
 </listing>
</listings>

如果父节点具有子节点,则输出xml应包含具有父节点和子节点名称的节点。在这里,我希望父节点应该根据它们的层次结构与子节点名称连接。

1 个答案:

答案 0 :(得分:2)

使用身份模板和其他模板。

<xsl:template match="listing//*[*]">
  <xsl:apply-templates select="*" />
</xsl:template>

<xsl:template match="listing//*[not(*)]">
  <xsl:variable name="newName">
    <xsl:for-each select="ancestor-or-self::*[count(ancestor::*) &gt; 1]">
      <xsl:value-of select="name()" />
      <xsl:if test="position() &lt; last()">_</xsl:if>
    </xsl:for-each>
  </xsl:variable>
  <xsl:element name="{$newName}">
    <xsl:apply-templates select="node() | @*" />
  </xsl:element>    
</xsl:template>

表达式ancestor-or-self::*[count(ancestor::*) &gt; 1],从<yearbuilt>节点的上下文中可以看出:

<listings>                            <!-- not selected -->
  <listing>                           <!-- not selected -->
    <overview>                        <!-- selected     -->
      <propertyoverview>              <!-- selected     -->
        <yearbuilt>1960</yearbuilt>   <!-- selected     -->