Martin, et al.,
Thanks for the reply - I forgot about using templates like that.
I've tried to take the `template match="subjects[contains(., 'HIDDEN')]"` a step further, but I'm running into a string-join() issue. The string '==>' denotes a change and I thought I could apply the following template to process those changes. I tokenize the subjects in a variable, and then, after creating a second variable, I try to apply a choose/when/otherwise on the resulting tokens. I want to discard tokens that contain '[Dd]elete' and update the tokens that indicate 'Old value ==> New value'. After that, I'm trying to put them back together. I can put them back together, but I can't get string-join() to insert commas between the values.
XSL:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
exclude-result-prefixes="#all">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="subjects[starts-with(., 'HIDDEN')]">
<xsl:variable name="tHIDDEN" select="tokenize(., ',')"/>
<xsl:variable name="tHIDDEN2">
<xsl:for-each select="$tHIDDEN">
<xsl:choose>
<xsl:when test="contains(., '==>') and not(contains(substring-after(., '==>'), '[Dd]elete'))">
<xsl:value-of select="normalize-space(substring-after(., '==>'))"/>
</xsl:when>
<xsl:when test="contains(., '==>') and contains(substring-after(., '==>'), '[Dd]elete')"/>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="$tHIDDEN2">
<subjectH2>
<xsl:value-of select="string-join(., ', ')"/>
</subjectH2>
</xsl:for-each>
</xsl:template>
<xsl:template match="subjects[not(starts-with(., 'HIDDEN'))]">
<xsl:for-each select="tokenize(., ',')">
<xsl:choose>
<xsl:when
test="contains(., '==>') and not(contains(substring-after(., '==>'), '[Dd]elete'))">
<subject>
<xsl:value-of select="normalize-space(substring-after(., '==>'))"/>
</subject>
</xsl:when>
<xsl:otherwise>
<subject>
<xsl:value-of select="normalize-space(.)"/>
</subject>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet>
updated XML:
Code:
<root>
<row>
<subjects>foo, bar, baz</subjects>
<subjects>qux, grault ==> fred, thud</subjects>
</row>
<row>
<subjects>waldo, wobble</subjects>
<subjects>HIDDEN: garply</subjects>
</row>
<row>
<subjects>spam, hash</subjects>
<subjects>eggs ==> bacon</subjects>
<subjects>HIDDEN: garply, wibble ==> wubble</subjects>
</row>
<row>
<subjects>wobble, wibble, wubble</subjects>
<subjects>qux ==> delete</subjects>
</row>
</root>
When I apply the provided template against the example XML, I get the following output:
Code:
<root>
<row>
<subject>foo</subject>
<subject>bar</subject>
<subject>baz</subject>
<subject>qux</subject>
<subject>fred</subject>
<subject>thud</subject>
</row>
<row>
<subject>waldo</subject>
<subject>wobble</subject>
<subjectH2>HIDDEN: garply</subjectH2>
</row>
<row>
<subject>spam</subject>
<subject>hash</subject>
<subject>bacon</subject>
<subjectH2>HIDDEN: garplywubble</subjectH2>
</row>
<row>
<subject>wobble</subject>
<subject>wibble</subject>
<subject>wubble</subject>
<subject>delete</subject>
</row>
</root>
I also just noticed that my attempt at deleting the subjects containing ==> delete isn't working, either. I'm guessing that these are problems with the way I'm structuring my when tests, but I'm not sure how else to go about this.
Thanks, again, for any suggestions you provide.