Domanda

I'm facing a stupid issue trying to output XML tags when processing a file.

My input xml is as simple as follows:

<?xml version="1.0" encoding="UTF-8"?>  
<body>
<information>  
<role_code>0003,3,0016</role_code>
</information>
</body>

My XSL is built to add an 'A' when the '3' token is found in the role_code tag

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="no" omit-xml-declaration="yes" />

    <xsl:template match="role_code" name="tokenize">
        <xsl:param name="text" select="."/>
        <xsl:param name="separator" select="','"/>
        <xsl:choose>
            <xsl:when test="not(contains($text, $separator))">
                 <xsl:if test="$text = '3'">
                    A<xsl:value-of select="$text"/>
                 </xsl:if>
                 <xsl:if test="not($text = '3')">
                    <xsl:value-of select="$text"/>
                 </xsl:if>          
              <xsl:text disable-output-escaping ="yes"><![CDATA[</role_code>]]></xsl:text>
            </xsl:when>
            <xsl:otherwise>
                 <xsl:if test="substring-before($text, $separator) = '3'">
                    A<xsl:value-of select="substring-before($text, $separator)"/>,
                 </xsl:if>
                 <xsl:if test="not(substring-before($text, $separator) = '3')">
                    <xsl:value-of select="substring-before($text, $separator)"/>,
                 </xsl:if>  
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-after($text, $separator)"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

My hopefully stupid issue is that I cannot output the initial tag without having errors on non matching tags

The current XSL outputs the following:

0003, A3, 0016</role_code> 

and I'm not facing where to include the opening "role_code" tag

È stato utile?

Soluzione 2

When your output is XML, you need not create tags using CDATA. You can put the tags directly in the template. In you case, you can call your "tokenize" template using another template by passing required paramters like this, and let the called template just do the processing:

<xsl:template match="/">
<role_code>
    <xsl:call-template name="tokenize">
        <xsl:with-param name="text" select="body/information/role_code"/>
    </xsl:call-template>
</role_code>
</xsl:template>

Called template needn't have match attribute(in this case):

<xsl:template name="tokenize">
   <xsl:param name="text" select="."/>
   <xsl:param name="separator" select="','"/> 
   <!-- something -->
</xsl:template>

Altri suggerimenti

Try it this way?

<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="/">
<body>
    <information>  
        <role_code>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="body/information/role_code"/>
            </xsl:call-template>
        </role_code>
    </information>
</body>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text" select="."/>
    <xsl:param name="separator" select="','"/>
    <xsl:choose>
        <xsl:when test="not(contains($text, $separator))">
            <xsl:if test="$text = '3'">
                <xsl:text>A</xsl:text>
            </xsl:if>
            <xsl:value-of select="$text"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:if test="substring-before($text, $separator) = '3'">
                <xsl:text>A</xsl:text>
            </xsl:if>
            <xsl:value-of select="substring-before($text, $separator)"/>
            <xsl:text>,</xsl:text>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $separator)"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

--
I have taken the liberty of streamlining your processing template.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top