将href插入XSLT翻译的XML文件HTML输出

时间:2014-05-27 15:43:20

标签: html xml xslt

拥有以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<core:renderedItem renderedContentUUID="e8c957a5-03e0-41f4-83a5-297e43bd660f" family="ResearchOutput" type="ContributionToJournal" rendering="harvard" workflows="researchoutput" workflowStates="approved" external="false" classification="/dk/atira/pure/researchoutput/researchoutputtypes/contributiontojournal/article" state="/dk/atira/pure/publication/status/inpress">
  <div class="rendering rendering_researchoutput  rendering_researchoutput_harvard rendering_contributiontojournal rendering_harvard rendering_contributiontojournal_harvard">
    <span>Reid, CT</span>
    &amp; Nsoh, W 2014, '
    <span class="harvard_title">Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiversity Conservation</span>
    '
    <span>
      <em>Journal of Human Rights and the Environment</em>
    </span>
    .
  </div>
</core:renderedItem>

我是XSLT的新手,我正在尝试使用以下HTML格式的XSLT输出XML:

<div class="publications">
  <p>
    <span>Reid, CT</span>
        &amp; Nsoh, W 2014, '
        <span class="harvard_title"><a href="http://some.website.com/portal/en/research/whose-ecosystem-is-it-anyway(e8c957a5-03e0-41f4-83a5-297e43bd660f).html">Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiversity Conservation</a></span>
    '
    <span><em>Journal of Human Rights and the Environment</em></span>
    .
  </p>
</div>

href链接始终以“http://some.website.com/portal/en/research/”开头,并且需要从该base + renderedContentUUID构建URL。我已经在其他人的帮助下管理了这个(有点!),但我仍然需要更改HTML输出以在href等中构建。此外,“harvard_title”字符串必须更改为全部小写如果标题中有冒号,我需要把字符串带到冒号。然后所有空间都必须变成' - '。这就是URL目前的样子:

http://somewebsite.com/portal/en/research/whose-ecosystem-is-it-anyway(e8c957a5-03e0-41f4-83a5-297e43bd660f).html

到目前为止我的xslt:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:core="http://atira.dk/schemas/pure4/model/core/stable"
  xmlns:x="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="x">

  <xsl:output method="html"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="lower">
    abcdefghijklmnopqrstuvwxyz
  </xsl:variable>

  <xsl:variable name="upper">
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
  </xsl:variable>

  <xsl:variable name="pubURL"/>

  <xsl:template match="//*">
    <html>
      <body>
        <h1>Staff Publications</h1>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="//*">
    <h2>Journal Articles</h2>
    <xsl:for-each select="//core:renderedItem">
      <xsl:sort select="@type"/>
      <xsl:choose>

        <xsl:when test ="@type = 'ContributionToJournal'">

      <xsl:variable name="uuid" select ="@renderedContentUUID"/>
      <xsl:variable name="show" select="translate(translate((div/span[@class = 'harvard_title']),' ','-'), $upper, $lower)" />
      <xsl:if test="contains($show, ':')">
        <xsl:variable name="pubURL" select="concat('http://somewebsite.com/portal/en/research/', substring-before($show, ':'),'(',$uuid,').html')"/>
      </xsl:if>
      <xsl:if test="not(contains($show, ':'))">
        <xsl:variable name="pubURL" select="concat('http://somewebsite.com/portal/en/research/',$show,'(',$uuid,').html')"/>
      </xsl:if>
      <xsl:value-of select ="$pubURL"/>
      <div class="publications">
        <p>
          <xsl:copy-of select="div"/>
        </p>
       </div>
     </xsl:when>
   </xsl:choose>
  </xsl:for-each>

</xsl:template>


</xsl:stylesheet>

任何人都可以为我揭开这一点。我不确定如何从pubURL继续构造href并将其“插入”HTML输出中。

1 个答案:

答案 0 :(得分:0)

回答你的问题非常困难,因为(1)你的XML无效;(2)你当前的样式表令人困惑(缺乏更好的词)。

要确定您询问的完全问题的答案:如果您的样式表具有以下模板(在适当的位置):

<xsl:template match="span[@class='harvard_title']">
    <span class="harvard_title">
        <a>
            <xsl:attribute name="href">
                <xsl:text>http://some.website.com/portal/en/research/</xsl:text>
                <xsl:value-of select="translate(translate(substring-before(., ':'), $upper, $lower), ' ', '-') "/>
                <xsl:value-of select="concat('(', ../../@renderedContentUUID, ').html')"/>
            </xsl:attribute>
            <xsl:copy-of select="node()"/>
        </a>
    </span>
</xsl:template>

然后该模板将以下输出写入结果树:

<span class="harvard_title"><a href="http://some.website.com/portal/en/research/whose-ecosystem-is-it-anyway(e8c957a5-03e0-41f4-83a5-297e43bd660f).html">Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiversity Conservation</a></span>