Recker Recker - 1 year ago 110
Perl Question

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="" version="1.0"
xmlns:exsl="" 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-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:call-template name = "Me" select="$Message"/>

And an input file as follows

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

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
not getting printed? How do I retrieve it with the help of
or any other techniques? Also, can I include the attribute
in the trial statement in such a way that it will be in the output?

Answer Source

The following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0"
<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"/>


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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download