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

Xslt 如何使用与字符串匹配的属性获取父节点的级别?,xslt,xslt-1.0,Xslt,Xslt 1.0,我有一个XML文件(这是一个我想转换为markdown的自由平面思维导图): 节点之间的深度可以从0到n 如何使用XSLT v1.0在头节点和当前节点之间获得正确的深度 ancestor::* [starts-with(@LOCALIZED_STYLE_REF, 'AutomaticLayout.level,')] [1] 我会用这个值定义一个变量$current header,然后深度的差异就变成了,正如您所期望的,一个 count(ancestor::*) - count($cur

我有一个XML文件(这是一个我想转换为markdown的自由平面思维导图):

节点之间的深度可以从
0
n


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

ancestor::*
  [starts-with(@LOCALIZED_STYLE_REF, 'AutomaticLayout.level,')]
  [1]
我会用这个值定义一个变量
$current header
,然后深度的差异就变成了,正如您所期望的,一个

count(ancestor::*) - count($current-header/ancestor::*)

谢谢你的回答。我实际上得到了一些东西,但是如果没有可用的祖先,
将失败。因此,我尝试在它前面放一个if语句,但它不起作用:
我会使用类似于
count($current header)>0的东西;其他人可能只喜欢
$current header
。你眼前的问题是你在引用变量时忽略了美元符号;第二个问题是,您似乎将存在与拥有除“”以外的字符串值混为一谈。对于您报告的失败,我有点惊讶;如果$current标头不存在,我希望count()返回零。您收到了什么错误消息以及您使用的是什么XSLT处理器?您好,我终于设法使用一个变量
lastHeaderCount
将其初始化为
count(祖先:*[以(@LOCALIZED\u STYLE\u REF,'AutomaticLayout.level,')][1]使其正常工作了。
。之后,如果它的值大于0,我可以使用您的代码,它就可以工作了。谢谢
<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>
ancestor::*
  [starts-with(@LOCALIZED_STYLE_REF, 'AutomaticLayout.level,')]
  [1]
count(ancestor::*) - count($current-header/ancestor::*)