【发布时间】:2021-11-21 10:34:01
【问题描述】:
我正在尝试通过 XSLT 使用已经预定义的格式重新创建结构图表,即它们的外观单独定义。该结构是使用 XML 编写的,但后来在 xml2tex 的帮助下使用目录树样式的 forest 包转换为 LaTeX 代码,Xproc 库 (https://github.com/transpect/xml2tex)。由于它们不是讨论的主要部分,所以我不再详细说明它们。
我所拥有的是以下形式的示例结构:
<structure>
<structentry id="1" parent="0" caller="true">Root</structentry>
<structentry id="2" parent="1" caller="false">Child 1 of ID 1 (Root)</structentry>
<structentry id="3" parent="1" caller="false">Child 2 of ID 1 (Root)</structentry>
<structentry id="4" parent="1" caller="false">Child 3 of ID 1 (Root)</structentry>
<structentry id="5" parent="4" caller="false">Child 1 of ID 4</structentry>
<structentry id="6" parent="5" caller="false">Child 1 of ID 5</structentry>
</structure>
理想情况下,输出应采用以下形式:
[{Root},fill=white[{Child 1 of ID 1 (Root)}][{Child 2 of ID 1 (Root)}][{Child 3 of ID 1 (Root)}[{Child 1 of ID 4}[{Child 1 of ID 5}]]]]
或者为了便于阅读:
[{Root},fill=white
[{Child 1 of ID 1 (Root)}]
[{Child 2 of ID 1 (Root)}]
[{Child 3 of ID 1 (Root)}
[{Child 1 of ID 4}
[{Child 1 of ID 5}]
]
]
]
然后在视觉上看起来像这样:
因此,我试图通过匹配的 id 将节点置于其父节点之下。也就是说,任何具有parent='1' 的节点都应该是具有id='1' 的节点的子节点。
我有以下转换,它使用“dbk”(DocBook)命名空间来定义输入 XML 中的节点。由于我对 XML、XSLT 和 XPath 还很陌生,所以在 xsl:template match="dbk:structentry" 之后我不禁停留在这里。如果需要任何其他信息,我很乐意更新它们。
<template context="dbk:structure">
<text>\begin{center}
</text>
<xsl:apply-templates select="dbk:caption"/>
<text>\rule[5pt]{0.8\textwidth}{0.4pt}
</text>
<text>
\begin{forest}</text>
<xsl:apply-templates select="dbk:structentry"/>
<text>\end{forest}</text>
<text>
\end{center}</text>
</template>
<xsl:template match="dbk:caption">
\textbf{<xsl:value-of select="."/>}

</xsl:template>
<xsl:template match="dbk:structentry">
<xsl:choose>
<xsl:when test="@parent eq '0' and @caller eq 'true'">
[{<xsl:value-of select="."/>},fill=white<xsl:apply-templates select="@parent eq '1'">]
</xsl:when>
<xsl:otherwise>
[{<xsl:value-of select="."/>}]
</xsl:otherwise>
</xsl:choose>
</xsl:template>
更新
一个新问题是如何区分图表的一个根条目和另一个图表的根条目?以下是 2 个结构的示例:
结构1:
<structure>
<structentry id="1" parent="0" caller="true">Root 1</structentry>
<structentry id="2" parent="1" caller="false">Child 1 of ID 1 (Root 1)</structentry>
<structentry id="3" parent="1" caller="false">Child 2 of ID 1 (Root 1)</structentry>
<structentry id="4" parent="1" caller="false">Child 3 of ID 1 (Root 1)</structentry>
<structentry id="5" parent="4" caller="false">Child 1 of ID 4</structentry>
<structentry id="6" parent="5" caller="false">Child 1 of ID 5</structentry>
</structure>
结构2:
<structure>
<structentry id="7" parent="0" caller="true">Root 2</structentry>
<structentry id="8" parent="7" caller="false">Child 1 of ID 7 (Root 2)</structentry>
<structentry id="9" parent="7" caller="false">Child 2 of ID 7 (Root 2)</structentry>
<structentry id="10" parent="7" caller="false">Child 3 of ID 7 (Root 2)</structentry>
<structentry id="11" parent="10" caller="false">Child 1 of ID 10</structentry>
<structentry id="12" parent="11" caller="false">Child 1 of ID 11</structentry>
</structure>
【问题讨论】:
-
我已经发布了一个答案 - 但一般来说,应该关闭这样的问题。
-
@michael.hor257k:不能自救并不是一件坏事。恭喜获得 100K。
-
@kjhughes 谢谢。我们这边见。
标签: xml xslt xpath latex xproc