XSL根据其他节点+多级排序的值查找节点

时间:2012-03-07 13:32:34

标签: xml xslt

基本上我需要弄清楚两件事:

  1. 多级排序
  2. 在第三级,我把???:如何选择另一个<term>,其<title>等于当前<narrowerterm>的{​​{1}}。< / LI>

    我希望这很清楚。我有一段时间没能找到我需要的东西。

    我的XML(摘录):

    <term>

    我的XSL:

    <thesaurus>
        <term>
            <title>defense</title>
             <narrowerterm>defense skills</narrowerterm>
             <narrowerterm>defense actions</narrowerterm>
        </term>
        <term>
            <title>defense skills</title>
            <broaderterm>defense</broaderterm>
            <narrowerterm>skill cd</narrowerterm>
            <narrowerterm>skill xy</narrowerterm>
            <narrowerterm>skill ab</narrowerterm>
        </term>
        <term>
            <title>defense actions</title>
            <broaderterm>defense</broaderterm>
            <narrowerterm>actions against xy</narrowerterm>
            <narrowerterm>actions against ab</narrowerterm>
        </term>
    </thesaurus>
    

    我想要的是:

    <xsl:template match="thesaurus">
    <html>
    <body> 
    <xsl:for-each-group select="term" group-by="title">
        <xsl:sort select="title"/>
        <xsl:choose>
            <xsl:when test="broaderterm"> 
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="use"> 
                    </xsl:when>
                    <xsl:otherwise>
                        <p>
                            <xsl:value-of select="current-grouping-key()"/><br/>
                            <xsl:for-each select="narrowerterm">
                                .<xsl:value-of select="."/><br/>
                                <xsl:for-each select="???"/>
                                    ..<xsl:value-of select="narrowerterm"/><br/>
                                </xsl:for-each>
                            </xsl:for-each>
                        </p>
                    </xsl:otherwise> 
                </xsl:choose>
            </xsl:otherwise> 
        </xsl:choose>
    </xsl:for-each-group> 
    </body>
    </html>
    </xsl:template>
    

    我在哪里:

    defense
    .defense actions
    ..actions against ab
    ..actions against xy
    .defense skills
    ..skill ab
    ..skill cd
    ..skill xy
    

1 个答案:

答案 0 :(得分:3)

我不认为这是一个分组任务,或者它需要XSLT 2.0。

此转化:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:apply-templates select="term[not(broaderterm)]">
    <xsl:with-param name="pIndent" select="''"/>
    <xsl:sort select="title"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="term">
  <xsl:param name="pIndent"/>
  <xsl:value-of select="concat($pIndent, title, '&#xA;')"/>

  <xsl:variable name="vSubterms" select=
      "../term[broaderterm = current()/title]"/>

  <xsl:variable name="vLeafSubterms" select=
  "narrowerterm[not(. = /*/term[broaderterm = current()/title]/title)]"/>

  <xsl:apply-templates select="$vSubterms | $vLeafSubterms">
   <xsl:with-param name="pIndent" select="concat($pIndent, '.')"/>
   <xsl:sort select="concat(self::term/title, self::narrowerterm/text())"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="narrowerterm">
  <xsl:param name="pIndent"/>
  <xsl:value-of select="concat($pIndent, ., '&#xA;')"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<thesaurus>
    <term>
        <title>defense</title>
        <narrowerterm>defense skills</narrowerterm>
        <narrowerterm>defense actions</narrowerterm>
    </term>
    <term>
        <title>defense skills</title>
        <broaderterm>defense</broaderterm>
        <narrowerterm>skill cd</narrowerterm>
        <narrowerterm>skill xy</narrowerterm>
        <narrowerterm>skill ab</narrowerterm>
    </term>
    <term>
        <title>defense actions</title>
        <broaderterm>defense</broaderterm>
        <narrowerterm>actions against xy</narrowerterm>
        <narrowerterm>actions against ab</narrowerterm>
    </term>
</thesaurus>

生成想要的正确结果

defense
.defense actions
..actions against ab
..actions against xy
.defense skills
..skill ab
..skill cd
..skill xy