perlxsltexslt

exsl:node-set not retrieving attribute's value


Here is a toned down version of my use case. I have

XSL file for transformation

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">

<xsl:output method="text"/>
<xsl:template match="Message">
    <xsl:for-each select="ent">
        <xsl:variable name="current_key" select="@key"/>
        <xsl:variable name="current_type" select="@type"/>
        <xsl:variable name="Match" select="exsl:node-set(msg)/ent"/>
        <xsl:copy>
            <xsl:copy-of select="exsl:node-set($Match)/@type"/>
            <xsl:copy-of select="exsl:node-set($Match)/@key|exsl:node-set($Match)/translation/text()"/>
            <!--- <xsl:copy-of select="exsl:node-set($Match)/@key|exsl:node-set($Match)/translation/text()|exsl:node-set($Match)/@type"/>  Trial statement -->
        </xsl:copy>
    </xsl:for-each>
    <xsl:call-template name = "Me" select="$Message"/>
</xsl:template>
</xsl:stylesheet>

And an input file as follows

<?xml version="1.0" encoding="utf-8"?>
<msg>
    <ent key="key1" type="error">
        <text>Error: Could not find </text>
        <translation>Another Error similar to previous one.</translation>
    </ent>
    <ent key="key2" type="damage">
        <text>Error2: Could not find2 </text>
        <translation>Another Error2 similar to previous one.</translation>
    </ent>
</msg>

I am using libXSLT in Perl as my transformation engine. My transformation script is already mentioned in this answer. Whenever I execute the script, I get the output as follows.

Error: Could not find 
Another Error similar to previous one.

Error2: Could not find2 
Another Error2 similar to previous one.

Why is the attribute type not getting printed? How do I retrieve it with the help of exsl:node-set or any other techniques? Also, can I include the attribute type in the trial statement in such a way that it will be in the output?


Solution

  • The following stylesheet:

    XSLT 1.0

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    
    <xsl:template match="/msg">
        <xsl:for-each select="ent">
            <xsl:text>KEY: </xsl:text>
            <xsl:value-of select="@key"/>
            <xsl:text>&#10;TYPE: </xsl:text>
            <xsl:value-of select="@type"/>
            <xsl:text>&#10;TEXT: </xsl:text>
            <xsl:value-of select="text"/>
            <xsl:text>&#10;TRANSLATION: </xsl:text>
            <xsl:value-of select="translation"/>
            <xsl:text>&#10;&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
    
    </xsl:stylesheet>
    

    when applied to your input example, will produce:

    KEY: key1
    TYPE: error
    TEXT: Error: Could not find 
    TRANSLATION: Another Error similar to previous one.
    
    KEY: key2
    TYPE: damage
    TEXT: Error2: Could not find2 
    TRANSLATION: Another Error2 similar to previous one.