2012-12-02 21 views
3

我正在尝试编写一个处理都柏林核心(XML)编目记录并为每本书创建芝加哥,APA和MLA版本的引文的XSLT样式表。除了APA作者之外,我已经将所有的工作都做好了。 APA对作者的风格要求作者的姓氏(完成),逗号,第一首字母(完成),任何其他首字母(我的卡住地点问题)。使用XSLT在字符串/子串中的每个实例后选择

我现在所拥有的(下面,品尝DC元素):

<xsl:value-of select="substring-before(dc:creator[1],',')" /><xsl:text>, </xsl:text><xsl:value-of select="substring(substring-after(dc:creator[1],' '),1,1)" /><xsl:for-each select="dc:creator[position()!=1]"><xsl:choose><xsl:when test="position()=last()"><xsl:text>., &amp; </xsl:text></xsl:when><xsl:otherwise>., </xsl:otherwise></xsl:choose><xsl:value-of select="substring-before(.,',')" /><xsl:text>, </xsl:text><xsl:value-of select="substring(substring-after(.,' '),1,1)" /><xsl:if test="position()=last()">.</xsl:if></xsl:for-each> 

对于以下格式的情况下(出什么目录使用):

<dc:creator>Friend, Natasha</dc:creator> 

这工作得很好,返回:Friend, N.

<dc:creator>Tolkien, J. R. R.</dc:creator> 

返回:Tolkien, J.

在很多也没关系的情况下,反而会出现情况下,它确实需要返回作者的中间名首字母(S),像J.R.R. TolkienJ.K. Rowling

因此,我需要能够返回每个空间后发生的字母,而不仅仅是空间的第一个实例。

任何想法?

回答

3

一,下面是一个简单的和自然的XSLT 2.0解决方案:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/*/*"> 
    <xsl:value-of separator=", " select= 
    "substring-before(., ',') 
    , for $n in tokenize(substring-after(., ','), '\s')[.] 
     return 
      substring($n, 1,1) 
    "/> 
    <xsl:text>&#xA;</xsl:text> 
</xsl:template> 
</xsl:stylesheet> 

当下面的XML文档应用这种转变:

<t xmlns:dc="some:dc"> 
<dc:creator >Friend, Natasha</dc:creator> 

<dc:creator>Tolkien, J. R. R.</dc:creator> 
</t> 

想要的,正确的结果产生:

Friend, N 
Tolkien, J., R., R. 

二,一个XSLT 1.0溶液

<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="/*/*/text()"> 
    <xsl:value-of select="substring-before(., ',')"/> 

    <xsl:call-template name="replaceTokenDelims"> 
    <xsl:with-param name="pStr" select= 
    "concat(normalize-space(substring-after(., ',')), ' ')"/> 
    <xsl:with-param name="pToken" select="' '"/> 
    <xsl:with-param name="pReplacement" select="', '"/> 
    </xsl:call-template> 
    <xsl:text>&#xA;</xsl:text> 
</xsl:template> 

<xsl:template name="replaceTokenDelims"> 
    <xsl:param name="pStr"/> 
    <xsl:param name="pToken"/> 
    <xsl:param name="pReplacement"/> 

    <xsl:if test="$pStr"> 
    <xsl:value-of select="$pReplacement"/> 
    <xsl:value-of select= 
     "substring(substring-before($pStr, $pToken), 1, 1)"/> 

    <xsl:call-template name="replaceTokenDelims"> 
     <xsl:with-param name="pStr" 
      select="substring-after($pStr, $pToken)"/> 
     <xsl:with-param name="pToken" select="$pToken"/> 
     <xsl:with-param name="pReplacement" select="$pReplacement"/> 
    </xsl:call-template> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

当这种转化是在同一个XML文档(以上)应用,再次同样正确的结果产生

Friend, N 
Tolkien, J., R., R. 
+0

我一直使用1.0自我更熟悉它,但我会检查一下,谢谢。除了每个之后,回报看起来都很棒,但我会把它拿出来。 – Talia

+0

@Talia:我认为'','是被要求作为新的分隔符。如果不是什么应该是分隔符?我还将提供一个XSLT 1.0解决方案。 –

+0

@Talia,用新增的XSLT 1.0解决方案更新了答案。 –

相关问题