Reputation: 4187
Currently I am redesigning an import process of XML files. The structure features a node which again has an attribute called "EntryMethod". Currently, the string value of this attribute is replaced by an int value during import using xsl:choose
:
<xsl:attribute name="EntryMethodKey">
<xsl:choose>
<xsl:when test="self::*[@EntryMethod = 'Scanned']">1</xsl:when>
....
However, decision has been made to keep the string value instead of the int value in order to dynamically receive new values as they arrive without extending the XSL. So - besides changing the column of the destination table - I changed the XSL as follows:
<xsl:attribute name="EntryMethodKey">
<xsl:value-of select="self::*[@EntryMethod]" />
</xsl:attribute>
But instead of 'Scanned' or whatever is the value of the attribute, I receive a whole lot of crap displaying anything but the entry method. Seems to be some kind of row from the XML, but I couldn't figure out which. But it doesn't include the EntryMethod attribute.
Anyways, if I change the XSL as follows, I receive (at least seemingly) the correct value:
<xsl:attribute name="EntryMethodKey">
<xsl:value-of select="@EntryMethod" />
</xsl:attribute>
But I don't understand why! Isn't there any risk in losing the correct value if I drop the self::*
? Is it guaranteed that select="@EntryMethod"
always returns the correct value? I tested it with something like 1.5k files and it looks OK - however, I'll have to reload millions of files and I want to reduce the risk of loosing information to an absolute minimum.
Upvotes: 0
Views: 53
Reputation: 70618
The expression self::*[@EntryMethod]
gets the current element, but only if it has an EntryMethod
attribute. The expression in square brackets is a condition to check. It is not actually selecting the attribute in this case, just checking it exists.
If you do xsl:value-of
on the current element, it will return the text value of that element. This means concatenating all the descendant text nodes together in one string. It will not include any attributes.
For example, if you do <xsl:value-of select="self:*" />
on the following XML (assuming you are matching the current_node
element) the result is AB
<current_node EntryMethod="scanned">
<child1>A</child1>
<child2>B</child2>
</current_node>
Now, you could do this...
<xsl:value-of select="self::*[@EntryMethod]/@EntryMethod" />
But this is unnecessary. You can just go with what you have got already to get the value of the attribute for the current element.
<xsl:value-of select="@EntryMethod" />
Upvotes: 1