xslt how can I get where I am at in the xml?
So if I have xml that looks like the below and I am in the node "FULLTIME" how can I tell that I am in EMPLOYEE as opposed to contractor? So is there a function I can use that will return something like "ROOT/PERSON/EMPLOYEE" showing me that from the FULLTIME Node the parent nodes are ROOT, PERSON, and EMPLOYEE? I can't seem to find anything that will provide that. I am new to xslt so my ignorance is probably a big part of me not being able to find what I want.
<ROOT>
<PERSON>
<EMPLOYEE>
<FULLTIME>
<NAME>Mike</NAME>
<LAST_NAME>Thompson&l开发者_StackOverflowt;/LAST_NAME>
<EMPLOYEE_ID>1</EMPLOYEE_ID>
</FULLTIME>
<PARTTIME />
</EMPLOYEE>
<CONTRACTOR>
<FULLTIME>
<NAME>Mike</NAME>
<LAST_NAME>Olsen</LAST_NAME>
<EMPLOYEE_ID>2</EMPLOYEE_ID>
</FULLTIME>
<PARTTIME />
</CONTRACTOR>
</PERSON>
</ROOT>
Any suggestions would be greatly appreciated!
--S
You could get EMPLOYEE
or CONTRACTOR
from FULLTIME
node using name()
XPath 1.0 function (or local-name()
if you're utilizing namespaces):
<xsl:value-of select="name(..)"/> <!-- Returns EMPLOYEE or CONTRACTOR -->
If you want to get full absolute path like /ROOT/PERSON/EMPLOYEE
, then use following recipe:
<xsl:for-each select="ancestor::*">
<xsl:text>/</xsl:text>
<xsl:value-of select="name()"/>
</xsl:for-each>
The ancestor
axis can be used to 'look up' through the tree. It's not entirely clear what you want to do, but you can test if EMPLOYEE is your ancestor:
<xsl:if test="ancestor::EMPLOYEE" >
...
</xsl:if>
You can match EMPLOYEE/FULLTIME or CONTRACTOR/FULLTIME separately:
<xsl:template match="EMPLOYEE/FULLTIME">
...
</xsl:template>
<xsl:template match="CONTRACTOR/FULLTIME">
...
</xsl:template>
Then in each match you can do different things with the fulltime employees vs contractors.
This is, I think, the XSLT way instead of the procedural thinking from other programming languages.
精彩评论