删除然后将文本添加到xml字段

时间:2014-01-16 19:21:26

标签: xml xslt

我对更改XML文件以创建带表的html文件感兴趣。

这是原始XML

的示例
<?xml version="1.0" encoding="UTF-8"?>
<library xmlns:dc="http://purl.org/dc/elements/1.1/">
  <item>
    <title>Electronic Payments for State Taxes and Fees</title>
    <link>http://mesharpe.metapress.com/link.asp?id=T861142L5675QQW2</link>
    <dc:identifier>DOI 10.2753/PMR1530-9576360406</dc:identifier>
  </item>
  <item>
    <title>The Determinants of Union Attitudes among Community College Professors</title>
    <link>http://baywood.metapress.com/link.asp?id=R216M2L6263165N1</link>
    <dc:identifier>DOI 10.2190/CN.32.4.a</dc:identifier>
  </item>
  <item>
    <title>Using Dedicated Nurses to Improve Core Measures Compliance</title>
    <link>http://rss.sciencedirect.com/...link>
    <dc:identifier>http://rss.sciencedirect.com/...</dc:identifier>
  </item>
</library>

我想创建这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:h="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"     xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/"         xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:mn="http://usefulinc.com/rss/manifest/"     xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <body>
    <h1 align="center">Something</h1>
    <table border="1" width="700" align="center">
      <tr bgcolor="#CC661F">
        <th>Title</th>
        <th>Identifier</th>
      </tr>
      <tr>
        <td>Electronic Payments for State Taxes and Fees</td>
        <td>http://dx.doi.org/10.2753/PMR1530-9576360406</td>
      </tr>
      <tr>
        <td>The Determinants of Union Attitudes among Community College Professors</td>
        <td>http://dx.doi.org/10.2190/CN.32.4.a</td>
      </tr>
      <tr>
        <td>Using Dedicated Nurses to Improve Core Measures Compliance</td>
        <td>http://rss.sciencedirect.com/...</td>
      </tr>
    </table>
  </body>
</html>

因此对于字段 dc:identifier ,有一个链接或带有数字的DOI。对于每个有DOI的人,我想删除DOI和空格,然后在前面添加http://dx.doi.org/,从而在表格中创建一个链接。

有关如何完成此任务的任何提示?谢谢!

编辑:我以前从未使用过XSLT文件而且我只有一个准系统模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:h="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/"     xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:mn="http://usefulinc.com/rss/manifest/"     xmlns:content="http://purl.org/rss/1.0/modules/content/"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <h1 align="center">Title</h1>
        <table border="1" width="700" align="center">
          <tr bgcolor="#CC661F">
            <th>Title</th>
            <th>Link</th>
          </tr>
          <xsl:for-each select="//h:item">
            <tr>
              <td>
                <xsl:value-of select="h:title" disable-output-escaping="yes"/>
              </td>
              <td>
                <xsl:value-of select="dc:identifier"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

您应该利用XSLT的模板匹配机制,而不是尝试使用for-each

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:h="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/"     xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:mn="http://usefulinc.com/rss/manifest/"     xmlns:content="http://purl.org/rss/1.0/modules/content/"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <h1 align="center">Title</h1>
        <table border="1" width="700" align="center">
          <tr bgcolor="#CC661F">
            <th>Title</th>
            <th>Link</th>
          </tr>
          <xsl:apply-templates select="//h:item" />
        </table>
      </body>
    </html>
  </xsl:template>

  <!-- create one row for each item -->
  <xsl:template match="h:item">
    <tr>
      <xsl:apply-templates select="h:title | dc:identifier" />
    </tr>
  </xsl:template>

  <!-- title and identifier become table columns -->
  <xsl:template match="h:title | dc:identifier">
    <td><xsl:apply-templates /></td>
  </xsl:template>

  <!-- text nodes that start DOI are massaged to add the appropriate prefix -->
  <xsl:template match="text()[starts-with(., 'DOI ')]">
    <xsl:value-of select="concat('http://dx.doi.org/', substring(., 5))" />
  </xsl:template>
</xsl:stylesheet>

这里的诀窍是处理文本节点的默认模板只是打印出节点的值,我在这里做的是添加一个额外的模板,仅用于启动“DOI”以添加http://dx.doi.org/前缀的文本节点