我有这个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 > 0">
<!-- Add 1 number signs (tab) to output. -->
<xsl:text>	</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>
节点之间的深度可能会有0
到n
如何使用XSLT v1.0在标头节点和当前节点之间获得正确的深度?
答案 0 :(得分:1)
最新的标题是
ancestor::*
[starts-with(@LOCALIZED_STYLE_REF, 'AutomaticLayout.level,')]
[1]
我定义了一个带有该值的变量$current-header
,然后根据您的预期,深度差异成为
count(ancestor::*) - count($current-header/ancestor::*)