xslt如何在xml中找到我的位置?

时间:2021-08-05 19:38:50

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.

如果我有一个看起来像下面的xml,并且我在节点“FULLTIME”中,我怎么知道我是在雇员而不是承包商呢?那么,我是否可以使用一个函数来返回诸如“ROOT/PERSON/EMPLOYEE”之类的内容,向我显示从FULLTIME节点开始,父节点是ROOT、PERSON和EMPLOYEE?我似乎找不到任何能提供这些的东西。我是xslt新手,所以我的无知很可能是我无法找到自己想要的东西的很大一部分。

<ROOT>
  <PERSON>
    <EMPLOYEE>
      <FULLTIME>
        <NAME>Mike</NAME>
        <LAST_NAME>Thompson</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

——年代

3 个解决方案

#1


1  

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>

#2


3  

You could get EMPLOYEE or CONTRACTOR from FULLTIME node using name() XPath 1.0 function (or local-name() if you're utilizing namespaces):

您可以使用名称()XPath 1.0函数(或本地名称())从FULLTIME节点获得雇员或承包商(如果您正在使用名称空间):

<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:

如果你想获得完整的绝对路径,比如/ROOT/PERSON/EMPLOYEE,那么使用以下方法:

<xsl:for-each select="ancestor::*">
    <xsl:text>/</xsl:text>
    <xsl:value-of select="name()"/>
</xsl:for-each>

#3


1  

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.

我认为,这是XSLT方法,而不是其他编程语言的过程思维。

#1


1  

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>

#2


3  

You could get EMPLOYEE or CONTRACTOR from FULLTIME node using name() XPath 1.0 function (or local-name() if you're utilizing namespaces):

您可以使用名称()XPath 1.0函数(或本地名称())从FULLTIME节点获得雇员或承包商(如果您正在使用名称空间):

<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:

如果你想获得完整的绝对路径,比如/ROOT/PERSON/EMPLOYEE,那么使用以下方法:

<xsl:for-each select="ancestor::*">
    <xsl:text>/</xsl:text>
    <xsl:value-of select="name()"/>
</xsl:for-each>

#3


1  

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.

我认为,这是XSLT方法,而不是其他编程语言的过程思维。