我正在尝试从第三方来源获取XML Feed并将其展平。
当前的XML Feed类似于:
<properties>
<property>
<idnumber></idnumber>
<location>
<region></region>
<street-address></street-address>
<city-name></city-name>
<state-code></state-code>
<zipcode></zipcode>
<latitude></latitude>
<longitude></longitude>
</location>
<details>
<name></name>
<status></status>
<price></price>
<bedrooms></num-bedrooms>
<bathrooms></bathrooms>
<lot-size></lot-size>
<square-feet></square-feet>
<property-type></property-type>
<attributes></attributes>
<description></description>
</details>
<pictures>
<picture>
<picture-url></picture-url>
</picture>
</property>
</properties>
但是,我真的需要将XML展平为:
<properties>
<property>
<idnumber></idnumber>
<region></region>
<street-address></street-address>
<city-name></city-name>
<state-code></state-code>
<zipcode></zipcode>
<latitude></latitude>
<longitude></longitude>
<name></name>
<status></status>
<price></price>
<bedrooms></num-bedrooms>
<bathrooms></bathrooms>
<lot-size></lot-size>
<square-feet></square-feet>
<property-type></property-type>
<attributes></attributes>
<description></description>
<picture>
<picture-url></picture-url>
</picture>
</property>
</properties>
我一直在阅读XLST样式表以尝试这一点,但是我只是浪费时间,因为原始供稿是在我无法编辑的第三方托管的?
答案 0 :(得分:1)
此转化:
<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="/*/*/*[*]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档(更正为格式良好):
<properties>
<property>
<idnumber></idnumber>
<location>
<region></region>
<street-address></street-address>
<city-name></city-name>
<state-code></state-code>
<zipcode></zipcode>
<latitude></latitude>
<longitude></longitude>
</location>
<details>
<name></name>
<status></status>
<price></price>
<bedrooms></bedrooms>
<bathrooms></bathrooms>
<lot-size></lot-size>
<square-feet></square-feet>
<property-type></property-type>
<attributes></attributes>
<description></description>
</details>
<pictures>
<picture>
<picture-url></picture-url>
</picture>
</pictures>
</property>
</properties>
生成所需的正确输出:
<properties>
<property>
<idnumber/>
<region/>
<street-address/>
<city-name/>
<state-code/>
<zipcode/>
<latitude/>
<longitude/>
<name/>
<status/>
<price/>
<bedrooms/>
<bathrooms/>
<lot-size/>
<square-feet/>
<property-type/>
<attributes/>
<description/>
<picture>
<picture-url/>
</picture>
</property>
</properties>
解释:正确使用并覆盖 the identity rule 。
答案 1 :(得分:1)
这将做你想要的。身份模板将所有内容复制到输出,而其他三个模板导致第二级标记在复制其内容时被省略。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="property/location">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="property/details">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="property/pictures">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>