String replace and concatenation

2020-05-01 08:55发布

问题:

I have this structure

<ROWS>
  <ROW>
    <TEXT> This is a @good@ @day@ </TEXT>
    <good>great</good>
    <day>month</day>
  </ROW>
  <ROW>
    <TEXT> This is a @good@ @day@ </TEXT>
    <good>Fun</good>
    <day>morning</day>
  </ROW>
</ROWS>

How do I change that to

<statement> This is a great month, this is a Fun morning </statement>

Using only XSLT 1.0?

The original XML can change tag name. But not the structure! Any ideas?

回答1:

This seems somewhat similar to creating form letters from a template. Assuming the example is not to be meant literally, you could try something like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <statements>
        <xsl:for-each select="ROWS/ROW/TEXT">
            <statement>
                <xsl:call-template name="merge">
                    <xsl:with-param name="string" select="."/>
                </xsl:call-template>
            </statement>
        </xsl:for-each>
    </statements>
</xsl:template>

<xsl:template name="merge">
    <xsl:param name="string"/>
    <xsl:param name="sep" select="'@'"/>
    <xsl:choose>
        <xsl:when test="contains($string, $sep) and contains(substring-after($string, $sep), $sep)">
            <xsl:value-of select="substring-before($string, $sep)" />
            <xsl:variable name="placeholder" select="substring-before(substring-after($string, $sep), $sep)" />
            <xsl:value-of select="../*[name() = $placeholder]" />
            <!-- recursive call -->
            <xsl:call-template name="merge">
                <xsl:with-param name="string" select="substring-after(substring-after($string, $sep), $sep)" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$string" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Given an input of:

<ROWS>
  <ROW>
    <TEXT>The quick brown @animal@ jumps over the @property@ dog.</TEXT>
    <animal>fox</animal>
    <property>lazy</property>
  </ROW>
  <ROW>
    <TEXT>A journey of a @number@ miles @action@ with a single @act@.</TEXT>
    <number>thousand</number>
    <action>begins</action>
    <act>step</act>
  </ROW>
</ROWS>

the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<statements>
   <statement>The quick brown fox jumps over the lazy dog.</statement>
   <statement>A journey of a thousand miles begins with a single step.</statement>
</statements>


回答2:

Look at the XSLT 1.0 spec; find the section on string functions. Study the contains, substring-before, and substring-after functions. The solution to your problem should become clear; if it doesn't, you should at least be able to get far enough on your problem to pose a question that does not look as if it could be paraphrased as "Please do my homework for me."