xml - 查找所有子元素的最大值,并在xslt中获取其父元素

标签 xml xslt xpath

这个问题与此帖子Find maximum value of all child elements and get its parent element in XSLT有关。我问错了问题。实际上,我需要的输出几乎没有什么不同。我需要将站点节点ID与工作站点节点进行匹配,并找出为该站点工作了更多时间的人。

<root>
    <Site id="S1">
        <othernodes></othernodes>
    </Site>
    <Site id="S2">
        <othernodes></othernodes>
    </Site>
    <Site id="S3">
        <othernodes></othernodes>
    </Site> 
    <WorkSite Person="P1" Site="S1">
        <Hours>8</Hours>
    </WorkSite>
    <WorkSite Person="P1" Site="S2">
        <Hours>2</Hours>
    </WorkSite>
    <WorkSite Person="P1" Site="S3">
        <Hours>20</Hours>
    </WorkSite>
    <WorkSite Person="P2" Site="S1">
        <Hours>6</Hours>
    </WorkSite>
    <WorkSite Person="P2" Site="S2">
        <Hours>10</Hours>
    </WorkSite>
    <WorkSite Person="P2" Site="S3">
        <Hours>21</Hours>
    </WorkSite>
</root>


转换后的内容应该是这样的

<root>
    <site id="S1">
            <othernodes></othernodes>
            <person>P1</person>
    </site>
    <site id="S2">
            <othernodes></othernodes>
            <person>P2</person>
    </site>
    <site id="S3">
            <othernodes></othernodes>
            <person>P1</person>
    </site>
</root>


有人可以帮忙吗?

最佳答案

像这样吗

<xsl:template match="/">
  <root>
    <xsl:apply-templates select="root/Site" />
  </root>
</xsl:template>

<xsl:template match="/root/Site">
  <xsl:variable name="sid" select="./@id" />
  <site id="{./@id}">
    <othernodes></othernodes>
    <xsl:apply-templates select="/root/WorkSite[@Site = $sid]">
      <xsl:sort select="./Hours" data-type="number" order="descending"/>
    </xsl:apply-templates>
  </site>
</xsl:template>

<xsl:template match="/root/WorkSite">
  <xsl:if test="position() = 1">
    <person><xsl:value-of select="./@Person" /></person>
  </xsl:if>
</xsl:template>

关于xml - 查找所有子元素的最大值,并在xslt中获取其父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3463161/

相关文章:

javascript - 从 HTML 页面插入 XML

java - AndroidManifest 中的工具替换

variables - 在 XSLT2 中选择变量的子级

php - 如何获取 xPath nodeValue 美元金额

php - 根据初始上下文选择节点

java - Kotlin 1.2.21 + SimpleXml 2.3.0 - 消费列表错误(必须标记set get方法)

android - 购物车图标未与操作栏中的计数器一起显示

xml - XML 中的组元素

xml - xslt is/when is choose 比 if 更有效

php - 删除特定表 DOMXPath