Reputation: 25
How do I reference the value passed in the ID field of an XML in a corresponding when statement in an XSLT?
Snippet of XML:
<UserArea>
<Id idOwner="Timekeeper">
<IdValue>Time</IdValue>
</Id>
</UserArea>
I want to build a conditional statement (using Choose Statement) based on the IdValue that is passed. Everything I build does not seem to recognize the conditional statement I build for the When statement. I've tried several different syntax but nothing seems to work, however, I do not get a syntax error- it just seems to never go into the node and evaluate it as True. The latest I have is:
<xsl:when test="hr:Timekeeper = 'Time'">
I'm also not sure what the "hr" means in this statement.. I copied that from a previous person that worked on this XSLT.
All my IF statements work but not the above WHEN statement. A sample IF statement that works perfectly in the same XSLT is:
<xsl:if test="@idOwner = 'Timekeeper' and hr:IdValue !=''">
....do something
</xsl:if>
In this syntax is it correct that the '@' standards for attribute?
I'm a novice at building XSLT's and I mainly only have to make very small edits to existing XSLT's but this is the first one I have to do much more than just small edits.
thxs!
Upvotes: 1
Views: 591
Reputation: 25
Not sure if anyone is even viewing this post any longer but I ended up having to build a function in the XSLT to reference the conditions that I need to address. I had several senior people working on this with me (been coding far far far longer than myself) and they also could not get our choose statement to work. It was resolved by abandoning our original plan and building a function call in the XSLT to determine what name should populate the node.
Upvotes: 0
Reputation: 122414
XPath expressions rely on prefixes to denote namespaced elements. Unprefixed names always refer to elements in no namespace. I suspect your input XML has a default namespace declaration like
xmlns="http://ns.hr-xml.org"
which puts all the elements in a namespace. So your XSLT has a matching declaration on the xsl:stylesheet
to bind the same namespace URI to a prefix
xmlns:hr="http://ns.hr-xml.org"
so you can use hr:IdValue
in xpath to select the IdValue
element in the http://ns.hr-xml.org
namespace.
An xmlns="..."
doesn't apply to attributes, which is why you don't need to prefix the idOwner
attribute name.
The exact expressions you need to use depend on the current context. If you're in a context where the current node is the Id
element (e.g. directly inside an <xsl:template match="hr:Id">
) and you want to test the value of the idOwner you'd use
test="@idOwner = 'Timekeeper'"
For the IdValue
test="hr:IdValue = 'Time'"
and for both together use and
test="@idOwner = 'Timekeeper' and hr:IdValue = 'Time'"
If the current context is the parent UserArea element and you want to check whether it has an Id matching these constraints then you'd use
test="hr:Id[@idOwner = 'Timekeeper' and hr:IdValue = 'Time']"
Upvotes: 1
Reputation: 407
the hr: is a namespace you won't need it for the example in addition to the answer of ColinE the content of a node is the textnode, so your test statement should look like this:
<xsl:when test="@idOwner = 'Timekeeper' and IdValue/text() != ''>
Upvotes: 1
Reputation: 70160
XSLT uses a query syntax called XPath, so I would recommend reading up on XPath i order to fully understand how this works.
In XPath you use the @
prefix to denote attributes, so the following should work:
<xsl:when test="@idOwner = 'Timekeeper'">
This will test whether the idOwner
attribute is equal to Timekeeper
for the current node.
Upvotes: 3