如何使用与字符串匹配的属性获取父节点的级别?

时间:2018-01-10 23:01:42

标签: xslt xslt-1.0

我有这个XML文件(我想要转换为markdown的Freeplane MindMap):

            <node TEXT="About exceptions" LOCALIZED_STYLE_REF="AutomaticLayout.level,2" ID="ID_1944432951" CREATED="1515603952224" MODIFIED="1515603952225">
                <node TEXT="- runtime exceptions (*runtime exceptions*) and its subclasses `:" ID="ID_1822312851" CREATED="1515603952233" MODIFIED="1515603952233">
                    <node TEXT="  - ArrayIndexOutofBounds" ID="ID_1897172344" CREATED="1515603952233" MODIFIED="1515603952233"/>

...

            <node TEXT="Collections" LOCALIZED_STYLE_REF="AutomaticLayout.level,2" ID="ID_1932847832" CREATED="1515603952454" MODIFIED="1515603952460">
                <node TEXT="Classes" LOCALIZED_STYLE_REF="AutomaticLayout.level,3" ID="ID_1207794388" CREATED="1515603952464" MODIFIED="1515603952464">
                    <node TEXT="`Map` is specific..." ID="ID_817824121" CREATED="1515603952464" MODIFIED="1515603952464"/>

我需要根据当前节点与属性选择的祖先(标头节点)之间存在多少祖先,为输出添加缩进。 / p>

标题节点(或深度参考节点

匹配的祖先属性为LOCALIZED_STYLE_REF,且必须以AutomaticLayout.level,开头(后跟任意数字:AutomaticLayout.level,1,例如。)

所以我需要根据以下内容之间的区别添加制表符:

    匹配父节点上的
  • count(ancestor::*)
  • 当前节点上的
  • count(ancestor::*)

我尝试了以下代码,但没有成功(我不知道如何获得lastHeaderLevel值):

<xsl:call-template name="addIndentation">
    <xsl:with-param name="currentNodeLevel" select="count(ancestor::*)" />
</xsl:call-template>

<!-- Template to calculate difference between current node's depth and last header's depth  -->
<xsl:template name="calculateIndentation">
    <xsl:param name="currentNodeLevel">0</xsl:param>
    <xsl:call-template name="numberSign">
        <xsl:with-param name="howMany" select="$currentNodeLevel - $lastHeaderLevel - 1"/>
    </xsl:call-template>
</xsl:template>

<!-- Template to change text indentation according to level from last header  -->
<xsl:template name="appendIndentationToOutput">
    <xsl:param name="howMany">0</xsl:param>
    <xsl:if test="$howMany &gt; 0">
        <!-- Add 1 number signs (tab) to output. -->
        <xsl:text>&#x9;</xsl:text>
        <!-- Print remaining ($howMany - 1) number signs. -->
        <xsl:call-template name="addIndentationToOutput">
            <xsl:with-param name="howMany" select="$howMany - 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

节点之间的深度可能会有0n

如何使用XSLT v1.0在标头节点和当前节点之间获得正确的深度

1 个答案:

答案 0 :(得分:1)

最新的标题是

ancestor::*
  [starts-with(@LOCALIZED_STYLE_REF, 'AutomaticLayout.level,')]
  [1]

我定义了一个带有该值的变量$current-header,然后根据您的预期,深度差异成为

的问题
count(ancestor::*) - count($current-header/ancestor::*)