首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >展平第三方XML提要?

展平第三方XML提要?
EN

Stack Overflow用户
提问于 2012-03-08 06:04:43
回答 2查看 101关注 0票数 2

我正在尝试从第三方来源获取XML提要,并将其扁平化。

当前的XML提要如下所示:

代码语言:javascript
复制
<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扁平化为:

代码语言:javascript
复制
<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样式表来尝试这一点,然而,我只是在浪费我的时间,因为原始提要托管在第三方,我不能在其中编辑?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-08 11:04:50

这会做你想做的事。身份模板会将所有内容复制到输出中,而其他三个模板会导致忽略第二级标记,同时仍会复制其内容。

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2012-03-08 11:02:55

此转换

代码语言:javascript
复制
<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文档上应用时(更正为格式良好):

代码语言:javascript
复制
<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>

生成所需的、正确的输出

代码语言:javascript
复制
<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>

解释:正确使用和覆盖。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9609846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档