gpt4 book ai didi

xml - 以下 sibling 包含2个子元素时的XSL XPath帮助

转载 作者:行者123 更新时间:2023-12-03 17:03:17 24 4
gpt4 key购买 nike

我在提出XPath语句时遇到问题,该语句将帮助我从Excel XML创建目录。

这是我的基本Excel XML结构:

<Table>
<Row>
<Cell>
<Data ss:Type="String">Title 1</Data>
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">Subtitle1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Filename1</Data>
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">Subtitle2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Filename2</Data>
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">Subtitle3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Filename3</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Title 2</Data>
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">Subtitle1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Filename1</Data>
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">Subtitle2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Filename2</Data>
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">Subtitle3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Filename3</Data>
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">Subtitle4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Filename4</Data>
</Cell>
</Row>




如上所示,我有两个标题,每个标题都有一个Cell元素,我需要能够抓取包含两个Cell元素的以下同级元素。我需要输出看起来像:

<ul class="nav">
<li class="sub">
<a href="javascript:void(0);">Title 1</a>
<ul>
<li>
<a href="javascript:void(0);" data-src="Filename1">Subtitle1</a>
</li>
<li>
<a href="javascript:void(0);" data-src="Filename2">Subtitle2</a>
</li>
<li>
<a href="javascript:void(0);" data-src="Filename3">Subtitle3</a>
</li>
</ul>
</li>
<li class="sub">
<a href="javascript:void(0);">Title 2</a>
<ul>
<li>
<a href="javascript:void(0);" data-src="Filename1">Subtitle1</a>
</li>
<li>
<a href="javascript:void(0);" data-src="Filename2">Subtitle2</a>
</li>
<li>
<a href="javascript:void(0);" data-src="Filename3">Subtitle3</a>
</li>
<li>
<a href="javascript:void(0);" data-src="Filename4">Subtitle4</a>
</li>
</ul>
</li>
</ul>


我会提供一些代码,但恐怕我还没有走太远。对于XSLT,我是一个菜鸟。

最佳答案

XSLT 1.0解决方案

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>

<xsl:template match="Table">
<ul class="nav">
<xsl:apply-templates select="Row[count(Cell)=1]"/>
</ul>
</xsl:template>

<xsl:template match="Row[count(Cell)=1]">
<li class="sub">
<a href="javascript:void(0);"><xsl:value-of select="Cell/Data"/></a>
<ul>
<xsl:variable name="heading" select="."/>
<xsl:apply-templates
select="following-sibling::Row[Cell[2]]
[generate-id(preceding-sibling::Row[count(Cell)=1][1]) =
generate-id($heading)]"/>
</ul>
</li>
</xsl:template>

<xsl:template match="Row[Cell[2]]">
<li>
<a href="javascript:void(0);" data-src="{ Cell[2]/Data }">
<xsl:value-of select="Cell[1]/Data"/>
</a>
</li>
</xsl:template>
</xsl:stylesheet>

关于xml - 以下 sibling 包含2个子元素时的XSL XPath帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29154432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com