首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除,然后将文本添加到xml字段

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

Stack Overflow用户
提问于 2014-01-16 19:21:26
回答 1查看 27关注 0票数 0

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

这里是原始XML的一个示例

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

我想创造出这样的东西:

代码语言:javascript
复制
<?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:标识符字段,有一个链接或一个DOI,后面跟着一个数字。对于每一个具有DOI的用户,我都希望删除DOI和空格,然后在前面添加http://dx.doi.org/,从而在表中创建一个链接。

对于如何做到这一点,有什么建议吗?谢谢!

编辑:我以前从未处理过XSLT文件,而且我只有一个简单的模板:

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-16 19:35:22

与其尝试使用for-each,不如利用XSLT的模板匹配机制。

代码语言:javascript
复制
<?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/前缀的文本节点添加一个额外的模板。

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

https://stackoverflow.com/questions/21170935

复制
相关文章

相似问题

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