如何简化xml文档。

时间:2015-08-03 08:18:48

标签: xml

我在一个名为'artists'的drupal站点上有节点我使用模块将所有这些节点导出到xml文档中。 代码如下所示:

<node>
<vid>304</vid>
<uid>102</uid>
<title>Hanne Hoejfeldt</title>
<log></log>
<status>1</status>
<comment>2</comment>
<promote>0</promote>
<sticky>0</sticky>
<vuuid>060c67ab-fea1-484e-9a5b-f551840d48b1</vuuid>
<ds_switch></ds_switch>
<nid>304</nid>
<type>artist</type>
<language>und</language>
<created>1352825291</created>
<changed>1438587438</changed>
<tnid>0</tnid>
<translate>0</translate>
<uuid>03eb47d0-fb45-4ad5-bde2-0c5b762d8506</uuid>
<revision_timestamp>1438587438</revision_timestamp>
<revision_uid>11</revision_uid>
<body>
  <und _numeric_keys="1">
    <n0>
      <value>&lt;p&gt;&lt;span style=&quot;font-family: Arial, sans-serif; line-height: 1.538em;&quot;&gt;Hanne Hoejfeldt was born in 1949 in Hjorring, Denmark.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;b style=&quot;line-height: 1.538em; font-family: Arial, sans-serif;&quot;&gt;Solo Exhibitions&lt;/b&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;1994 Pandrup Town Hall, Pandrup, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2006 Nibe Museum, Nibe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;&lt;b&gt;Group Exhibitions&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;1997 Billedstedet, Fosdalen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;1998 Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Kirsten Kjaers Museum of Art, Frostrup, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Inuit, Aalborg, Denmark &lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;1999 Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery SO-TO, Aalborg, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Inuit, Aalborg, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Danish Art-Fair, Viborg, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2000 Gallery Knud Grothe, Copenhagen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Billedstedet, Fosdalen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Inuit, Aalborg, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2001 Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Bronderslev Highschool, Bronderslev, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2002 Gallery Knud Grothe, Copenhagen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Billedstedet, Fosdalen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Inuit, Aalborg, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2003 Gallery Knud Grothe, Copenhagen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Inuit, Aalborg, Denmark &lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Jarsbo, Aarthus, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Artgallery Odense, Odense, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Varmegalleriet, Copenhagen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2004 Artgallery Odense, Odense, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2005 Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Billedstedet, Fosdalen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Fussingo Castle, Fussingo, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Inuit, Aalborg, Denmark &lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Jarsbo, Aarthus, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Galerie Horn, Lolland, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Vejle, Vejle, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Artgallery Odense, Odense, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2006 Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Jarsbo, Aarthus, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Copenhagen, Copenhagen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2007 Gallery Knud Grothe, Copenhagen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Inuit, Aalborg, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Jarsbo, Aarthus, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Copenhagen, Copenhagen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Galerie Horn, Lolland, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2008 Gallery Knud Grothe, Copenhagen, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Galleri Fyns Arthouse, Ringe, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Jarsbo, Aarthus, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Inuit, Aalborg, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Gilberg, Fredericia, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Artgallery Odense, Odense, Denmark&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Gallery Annorlunda, xxxx, Sweden&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;GINA Gallery of International Na෥ Art, Tel Aviv, Israel&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;2009 Fete de L&amp;#39;Art Naif, Verneuil, Paris, France&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Katowice Festival, Katowice, Poland&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;text-indent: 0.5in; margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Magdeburg Festival, Magdeburg, Germany&lt;/font&gt;&lt;/p&gt;&lt;p style=&quot;text-indent: 0.5in; margin-bottom: 0in&quot;&gt;&lt;font face=&quot;Arial, sans-serif&quot;&gt;Galleri Gilberg, Fredericia, Denmark&lt;/font&gt;&lt;/p&gt;</value>
      <summary></summary>
      <format>full_html</format>
    </n0>
  </und>
</body>
<field_profile_photo>
  <und _numeric_keys="1">
    <n0>
      <fid>1166</fid>
      <uid>1</uid>
      **<filename>Hanne_Hoejfeldt.jpg</filename>**
      <uri>public://Hanne_Hoejfeldt.jpg</uri>
      <filemime>image/jpeg</filemime>
      <filesize>6406</filesize>
      <status>1</status>
      <timestamp>1360494813</timestamp>
      <type>image</type>
      <uuid>8f0d8a1c-721c-47eb-b237-01c4b76f55a1</uuid>
      <field_file_image_alt_text _numeric_keys="1"></field_file_image_alt_text>
      <field_file_image_title_text _numeric_keys="1"></field_file_image_title_text>
      <rdf_mapping _numeric_keys="1"></rdf_mapping>
      <image_dimensions>
        <width>140</width>
        <height>140</height>
      </image_dimensions>
      <alt>Hanne Hoejfeldt</alt>
      <title>Hanne Hoejfeldt</title>
      <width>140</width>
      <height>140</height>
    </n0>
  </und>
</field_profile_photo>
<field_country>
  <und _numeric_keys="1">
    <n0>
      <lid>199</lid>
      <name></name>
      <street></street>
      <additional></additional>
      <city></city>
      <province></province>
      <postal_code></postal_code>
      <country>dk</country>
      <latitude>0.000000</latitude>
      <longitude>0.000000</longitude>
      <source>0</source>
      <is_primary>0</is_primary>
      <province_name></province_name>
      **<country_name>Denmark</country_name>**
      <fax></fax>
      <phone></phone>
    </n0>
  </und>
</field_country>
<field_country_location>
  <und _numeric_keys="1">
    <n0>
      <nid>1528</nid>
    </n0>
  </und>
</field_country_location>
<field_first_name_artist>
  <und _numeric_keys="1">
    <n0>
      **<value>Hanne</value>**
      <format type="NULL"></format>
      <safe_value>Hanne</safe_value>
    </n0>
  </und>
</field_first_name_artist>
<field_last_name_artist>
  <und _numeric_keys="1">
    <n0>
      <**value>Hoejfeldt</value>**

但是我只想要标签内的值

 <title>Hanne Hoejfeldt</title>, <value>&lt;p&gt;&lt;span style=&quot;font-family: Arial, sans-serif; line-height: 1.538em;&quot;&gt;Hanne Hoejfeldt was born in 1949 in Hjorring, Denmark.&lt;/span&gt;&lt;/p&gt;&lt;... </value>, <filename>Hanne_Hoejfeldt.jpg</filename>,  <country_name>Denmark</country_name>,   <value>Hanne</value, <value>Hoejfeldt</value>

如何从xml文件中仅提取这些值?

2 个答案:

答案 0 :(得分:0)

将一个XML转换为另一个XML是XSLT的自然任务。例如,使用XSLT 2.0

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <!-- create one root element, 
         and process elements in the source XML as content of the root -->
    <xsl:template match="/">
      <root>
          <xsl:apply-templates select="*"/>
      </root>
    </xsl:template>

    <!-- copy required elements -->
    <xsl:template match="*[name()=('title','value','filename','country_name')]">
        <xsl:copy>
            <xsl:value-of select="."/>
        </xsl:copy>
    </xsl:template>

    <!-- remove unnecessary elements -->
    <xsl:template match="*">
        <xsl:apply-templates select="*"/>
    </xsl:template>
</xsl:transform>

<强> Demo

答案 1 :(得分:0)

使用XML Linq

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>

<div ng-app='app' ng-controller='myController'>
  <div id="locationField">
    <input id="autocomplete" placeholder="Enter your address" type="text">
  </div>
  <table id="address">
    <tr>
      <td class="label">Street address</td>
      <td class="slimField">
        <input ng-model="formData.street_number" class="field" id="street_number" disabled="true" type="text">
      </td>
      <td class="wideField" colspan="2">
        <input ng-model="formData.route" class="field" id="route" disabled="true">
      </td>
    </tr>
    <tr>
      <td class="label">City</td>
      <td class="wideField" colspan="3">
        <input ng-model="formData.locality" class="field" id="locality" disabled="true">
      </td>
    </tr>
    <tr>
      <td class="label">State</td>
      <td class="slimField">
        <input ng-model="formData.administrative_area_level_1" class="field" id="administrative_area_level_1" disabled="true">
      </td>
      <td class="label">Zip code</td>
      <td class="wideField">
        <input ng-model="formData.postal_code" class="field" id="postal_code" disabled="true">
      </td>
    </tr>
    <tr>
      <td class="label">Country</td>
      <td class="wideField" colspan="3">
        <input ng-model="formData.country" class="field" id="country" disabled="true">
      </td>
    </tr>
  </table>
  <pre>{{ formData | json }}</pre>
  <pre>{{ ll | json }}</pre>
</div>