我有一个 HTML 文档,它使用无序列表来显示导航。当它作为 XML 输出时,它不再使用列表,而是为每个链接分配一个 <Description>
。描述的工作方式是为主要链接分配数字,例如 100、200、300、400 等。这些子导航链接的分配方式如下:200.100、200.200、200.300 等。
XML 如下所示:
<Items>
<PgIndexElementItem>
<Description>100</Description>
<Title>This is the Title</Title>
</PgIndexElementItem>
<PgIndexElementItem>
<Description>200</Description>
<Title>This is the Title</Title>
</PgIndexElementItem>
<PgIndexElementItem>
<Description>200.100</Description>
<Title>This is the Title</Title>
</PgIndexElementItem>
<PgIndexElementItem>
<Description>200.100.100</Description>
<Title>This is the Title</Title>
</PgIndexElementItem>
</Items>
如果查看最后一个 PgIndexElementItem,会发现有三组数字。
我正在尝试使用 XSLT 重新创建无序列表。这就是我到目前为止一直在做的事情:
<ul>
<xsl:for-each select="//PgIndexElementItem">
<xsl:if test="not(contains(Description, '.'))">
<li><a href="{ResolvedURL/ServerRelative}"><xsl:value-of select="Title"/></a>
<ul>
<xsl:for-each select="//PgIndexElementItem">
<xsl:if test="contains(Description, '.')">
<li><a href="{ResolvedURL/ServerRelative}"><xsl:value-of select="Title"/></a></li>
</xsl:if>
</xsl:for-each>
</ul>
</li>
</xsl:if>
</xsl:for-each>
</ul>
我的问题是,有没有办法指定 contains(Description
其目标是带有两个句点的描述,例如上面的最后一个 PgIndexElementItem?
我希望我对此解释得足够好。我不知道如何让它不那么困惑。提前致谢!
编辑
o Graduate Program
* How to Apply
* Masters Program
o M.A. Handbook
o FAQs
o Alumni
o Masters Theses
o Sample Thesis Proposals
* M.A. Handbook
* FAQs
* Alumni
* Masters Theses
* Sample Thesis Proposals
最佳答案
你可以使用
<xsl:if test="contains(substring-after(Description, '.'), '.')">
...
测试某个元素是否具有包含两个或多个句点的描述子元素。
但是,我会将您的 XSLT 更改为:
<!-- this part replaces your <ul> code above -->
<ul>
<xsl:apply-templates
select="//PgIndexElementItem[not(contains(Description, '.'))]" />
</ul>
更新了此模板:
<!-- add this new template -->
<xsl:template match="PgIndexElementItem">
<li>
<a href="{ResolvedURL/ServerRelative}"><xsl:value-of select="Title"/></a>
<xsl:variable name="prefix" select="concat(Description, '.')"/>
<xsl:variable name="childOptions"
select="../PgIndexElementItem[starts-with(Description, $prefix)
and not(contains(substring-after(Description, $prefix), '.'))]"/>
<xsl:if test="$childOptions">
<ul>
<xsl:apply-templates select="$childOptions" />
</ul>
</xsl:if>
</li>
</xsl:template>
这将避免不必要的代码重复和不需要的空 <ul>
元素。
我对如何解释描述子项做了一些假设...例如“200.100”应该出现在“200”下的子列表中,但不能出现在“100”下。
关于XSLT 包含两个句点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4464640/