[英]XML to XML (without parent nodes) conversion using XSLT
I am trying to convert XML file to another XML file using XSLT. 我正在尝试使用XSLT将XML文件转换为另一个XML文件。
Input file is: 输入文件为:
<?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>
Output I want is: 我想要的输出是:
<?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>
If parent has child nodes then output xml should contain node with parent plus child node name. 如果父节点具有子节点,则输出xml应包含具有父节点和子节点名称的节点。 Here I want parent nodes should be concatenated with child node name according to their hierarchy.
在这里,我希望根据父节点的层次结构将父节点与子节点名称连接在一起。
Use the identity template and these additional templates. 使用身份模板和这些其他模板。
<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::*) > 1]">
<xsl:value-of select="name()" />
<xsl:if test="position() < last()">_</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:element name="{$newName}">
<xsl:apply-templates select="node() | @*" />
</xsl:element>
</xsl:template>
The expression ancestor-or-self::*[count(ancestor::*) > 1]
表达式“
ancestor-or-self::*[count(ancestor::*) > 1]
ancestor-or-self::*[count(ancestor::*) > 1]
, as seen from the context of a <yearbuilt>
node: ancestor-or-self::*[count(ancestor::*) > 1]
,如从<yearbuilt>
节点的上下文所看到的:
<listings> <!-- not selected -->
<listing> <!-- not selected -->
<overview> <!-- selected -->
<propertyoverview> <!-- selected -->
<yearbuilt>1960</yearbuilt> <!-- selected -->
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.