Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:31:04+00:00 2026-06-15T10:31:04+00:00

I’m attempting to write an XSLT stylesheet that will handle a Dublin Core (XML)

  • 0

I’m attempting to write an XSLT stylesheet that will handle a Dublin Core (XML) cataloging record and create Chicago, APA, and MLA versions of the citation for each book. I’ve got everything worked out fine except the APA author one. APA’s style for authors requires the author’s last name (done), comma, first initial (done), any other initials (my stuck place problem).

What I have right now (and sample DC element is below):

<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>

For instances of the following format (what out catalog uses):

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

this works just fine and returns: Friend, N.

But for

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

it returns: Tolkien, J.

In a lot of cases it doesn’t matter, but there will be cases where it really needs to return authors’ middle initial(s), like J.R.R. Tolkien or J.K. Rowling.

So, I need to be able to return the letter that occurs after each space, not just the first instance of a space.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:31:07+00:00Added an answer on June 15, 2026 at 10:31 am

    I. Here is an easy and natural XSLT 2.0 solution:

    <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>
    

    When this transformation is applied on the following XML document:

    <t xmlns:dc="some:dc">
     <dc:creator >Friend, Natasha</dc:creator>
    
     <dc:creator>Tolkien, J. R. R.</dc:creator>
    </t>
    

    the wanted, correct result is produced:

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

    II. An XSLT 1.0 solution:

    <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>
    

    When this transformation is applied on the same XML document (above), again the same correct result is produced:

    Friend, N
    Tolkien, J., R., R.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.