Reputation: 491
I am attempting a workaround for the loss of custom XML in Word's Office Open XML, and I've run into a bit of a snag.
I am working on a way of tracking contributions to a repository of shared lesson plans that will reside in an Omeka site. What I want to be able to do is wrap each character in a bookmark, which would then be hidden from view when the file is opened in Word. This is so that token will carry across when things are copied and pasted offsite, and then reconnected to the original contributor when the new file is uploaded again.
To do this, my XML current looks like this:
<w:p w:rsidR="00196751" w:rsidRDefault="00956712">
<w:r>
<w:t>Some sample text.</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
What I need it to do is look like this:
<w:p w:rsidR="00196751" w:rsidRDefault="00956712">
<w:bookmarkStart w:id="0" w:name="_NAME_0001_20150616_1"/>
<w:r>
<w:t>S</w:t>
</w:r>
<w:bookmarkStart w:id="1" w:name="_NAME_0001_20150616_2"/>
<w:bookmarkEnd w:id="0"/>
<w:r>
<w:t>o</w:t>
</w:r>
<w:bookmarkStart w:id="2" w:name="_NAME_0001_20150616_3"/>
<w:bookmarkEnd w:id="1"/>
<w:r>
<w:t>m</w:t>
</w:r>
<w:bookmarkStart w:id="3" w:name="_NAME_0001_20150616_4"/>
<w:bookmarkEnd w:id="2"/>
<w:r>
<w:t>e</w:t>
</w:r>
<w:bookmarkStart w:id="4" w:name="_NAME_0001_20150616_5"/>
<w:bookmarkEnd w:id="3"/>
<w:r>
<w:t xml:space="preserve"> </w:t>
</w:r>
[...]
<w:bookmarkStart w:id="17" w:name="_GoBack"/>
<w:bookmarkEnd w:id="16"/>
<w:bookmarkEnd w:id="17"/>
</w:p>
It seems to me that what I'd need to do is run the tokenize function on the original w:t item, so I wrote the following xslt as a first step:
<xsl:template match="w:t">
<xsl:value-of select="tokenize(., '.??')"/>
</xsl:template>
However when I do that I get an error message. My assumption is that this is because there's no actual pattern for the function to tokenize on since I'm asking it to separate on each character. Is there a way to use the tokenize function to do this? If so, how would I do so to get the result I need? Thanks!
Upvotes: 0
Views: 297
Reputation: 163262
There are two errors I can imagine you getting (it's a shame you didn't think to tell us the error message). One is that the tokenize function isn't available, which would mean you are using an XSLT 1.0 processor; the other is that your regular expression is invalid.
If you're using an XSLT 2.0 processor then you can split a string into characters using string-to-codepoints() (and you can turn each character back ino a string using codepoints-to-string()).
If you're using an XSLT 1.0 processor then you need to do the splitting "by hand" using a recursive template as suggested by @yarivt.
Upvotes: 0
Reputation: 106
You should have a template for this, which you can apply on your original <w:t>
value:
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="string-length($text) = 1">
<w:r>
<w:t><xsl:value-of select="$text"/></w:t>
</w:r>
</xsl:when>
<xsl:otherwise>
<w:r>
<w:t><xsl:value-of select="substring($text, 1, 1)"/></w:t>
</w:r>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring($text, 2, string-length($text)-1)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
not sure how the w:bookmarkStart and w:bookmarkEnd logic work, but you can probably figure it out from this.
Upvotes: 1