问题
I have this tree structured XML (toc.xml):
<?xml version="1.0" encoding="utf-8"?>
<toc>
<item name="top" key="4294967296" subkey="1">
<item name="child1" key="4294967611" subkey="">
<item name="child2-1" key="4294961611" subkey="">
<item name="child3-1" key="4294967613" subkey=""/>
<item name="child3-2" key="4294967612" subkey=""/>
</item>
<item name="child2-2" key="4294962611" subkey="">
<item name="d" key="4294974806" subkey=""/>
</item>
<item name="child2-3" key="4294963611" subkey="">
<item name="d" key="4294967661" subkey=""/>
<item name="PI" key="4294967659" subkey=""/>
<item name="q" key="4294967660" subkey=""/>
</item>
<item name="child2-4" key="4294964611" subkey=""/>
<item name="child2-5" key="4294965611" subkey="">
<item name="bb" key="4294967616" subkey=""/>
<item name="bb" key="4294967620" subkey=""/>
<item name="f" key="4294967615" subkey=""/>
</item>
</item>
</item>
</toc>
Each key will be unique in the document.
I have an XSLT that imports the toc XML and tries to output the navigation:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no" />
<xsl:variable name="id" select="/member/@id" />
<xsl:template match="/member">
<html>
<head>
<title><xsl:value-of select="/member/name"/></title>
</head>
<body>
<div class="navigation">
<xsl:apply-templates select="document('toc.xml')" />
</div>
<div class="content">
<xsl:apply-templates />
</div>
</body>
</html>
</xsl:template>
</xsl>
And would like to find a node and output the following HTML inside the HTML file:
...html...
<div class="navigation">
<ul>
<li><a href="#">top</a><ul>
<li><a href="#">child1</li><ul>
<li><a href="#">child2</li><ul>
<li><a href="#">child3-1</a></li>
<li><a href="#">child3-2</a></li>
</ul></li>
</ul></li>
</ul></li>
</ul>
</div>
...more html...
Basically I want to search for a node: item[@key='4294967611'] and output all of the parent nodes and the direct children.
I feel this should be really easy but I'm struggling to find information about how to do this. My XSLT knowledge isn't great.
回答1:
With the provided input (with no unique @key), this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="key" select="4294967611"/>
<xsl:template match="item">
<xsl:choose>
<xsl:when test="generate-id() =
generate-id(../item[@key=$key][1])
and
not(item[@key=$key])">
<xsl:call-template name="chain">
<xsl:with-param name="parents" select="ancestor-or-self::item"/>
<xsl:with-param name="childs" select="item"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="chain">
<xsl:param name="parents"/>
<xsl:param name="childs"/>
<xsl:if test="$parents">
<ul>
<li>
<a href="#">
<xsl:value-of select="$parents[1]/@name"/>
</a>
<xsl:call-template name="chain">
<xsl:with-param name="parents" select="$parents[position()!=1]"/>
<xsl:with-param name="childs" select="$childs"/>
</xsl:call-template>
<xsl:if test="count($parents)=1">
<ul>
<xsl:for-each select="$childs">
<li>
<a href="#">
<xsl:value-of select="@name"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</ul>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<ul>
<li>
<a href="#">top</a>
<ul>
<li>
<a href="#">child1</a>
<ul>
<li>
<a href="#">child2-1</a>
<ul>
<li>
<a href="#">child3-1</a>
</li>
<li>
<a href="#">child3-2</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
If @key were unique, this stylesheet should work:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="itemBykey" match="item" use="@key"/>
<xsl:param name="key" select="4294967611"/>
<xsl:template match="/">
<xsl:for-each select="key('itemBykey',$key)">
<xsl:call-template name="chain">
<xsl:with-param name="parents" select="ancestor-or-self::item"/>
<xsl:with-param name="childs" select="item"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="chain">
<xsl:param name="parents"/>
<xsl:param name="childs"/>
<xsl:if test="$parents">
<ul>
<li>
<a href="#">
<xsl:value-of select="$parents[1]/@name"/>
</a>
<xsl:call-template name="chain">
<xsl:with-param name="parents" select="$parents[position()!=1]"/>
<xsl:with-param name="childs" select="$childs"/>
</xsl:call-template>
<xsl:if test="count($parents)=1">
<ul>
<xsl:for-each select="$childs">
<li>
<a href="#">
<xsl:value-of select="@name"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</ul>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
回答2:
This is the correct implementation with the proper parent-child hierarchy for the ul and li elements. It will output only those nodes that are parents or direct children of value for key given in the xsl:variable keyVar.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" >
<xsl:output method="xml" indent="yes"/>
<xsl:variable name ="keyVar" select="'4294967666'" />
<xsl:template match="toc">
<ul>
<xsl:apply-templates select="item" />
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<a href="#" />
<xsl:value-of select="@name"/>
<xsl:if test ="count(descendant::item) > 0">
<ul>
<xsl:apply-templates select="item[.//@key=$keyVar or ../@key = $keyVar]" />
</ul>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
Output hierarchy:
<?xml version="1.0" encoding="utf-8"?>
<ul>
<li>top<ul>
<li>child1<ul>
<li>child2-1<ul>
<li>child3-1</li>
<li>child3-2</li>
</ul></li>
<li>child2-2<ul>
<li>d</li>
</ul></li>
<li>child2-3<ul>
<li>d</li>
<li>PI</li>
<li>q</li>
</ul></li>
<li>child2-4</li>
<li>child2-5<ul>
<li>bb</li>
<li>bb</li>
<li>f</li>
</ul></li>
</ul></li>
</ul></li>
</ul>
回答3:
To start with, the provided wanted output is not well-formed XML document or fragment.
Also, there is no <item name="child2" ...> element in the provided source XML document.
Also, there are six item elements with key='4294967611', so this also isn't a usable criterion for identifying the element.
I think, you want this:
<ul>
<li><a href="#">top</a>
<ul>
<li><a href="#">child1</a>
<ul>
<li><a href="#">child2-1</a>
<ul>
<li><a href="#">child3-1</a></li>
<li><a href="#">child3-2</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
If my guess is correct, here is one possible solution:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pEl" select="//item[@name='child2-1']"/>
<xsl:template match="toc">
<xsl:variable name="pChain" select="$pEl/ancestor-or-self::item"/>
<xsl:apply-templates select="$pChain[1]">
<xsl:with-param name="pChain" select="$pChain"/>
<xsl:with-param name="pEndElementId" select="generate-id($pEl)"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="item">
<xsl:param name="pChain" select="/.."/>
<xsl:param name="pcurPosition" select="1"/>
<xsl:param name="pEndElementId"/>
<ul>
<li><a href="#"><xsl:value-of select="@name"/></a>
<xsl:choose>
<xsl:when test="generate-id() = $pEndElementId">
<ul>
<xsl:apply-templates select="item" mode="leafChildren"/>
</ul>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$pChain[position()=$pcurPosition+1]">
<xsl:with-param name="pChain" select="$pChain"/>
<xsl:with-param name="pcurPosition" select="$pcurPosition +1"/>
<xsl:with-param name="pEndElementId" select="$pEndElementId"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</li>
</ul>
</xsl:template>
<xsl:template match="item" mode="leafChildren">
<li><a href="#"><xsl:value-of select="@name"/></a></li>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
<toc>
<item name="top" key="4294967296" subkey="1">
<item name="child1" key="4294967611" subkey="">
<item name="child2-1" key="4294967611" subkey="">
<item name="child3-1" key="4294967613" subkey=""/>
<item name="child3-2" key="4294967612" subkey=""/>
</item>
<item name="child2-2" key="4294967611" subkey="">
<item name="d" key="4294974806" subkey=""/>
</item>
<item name="child2-3" key="4294967611" subkey="">
<item name="d" key="4294967661" subkey=""/>
<item name="PI" key="4294967659" subkey=""/>
<item name="q" key="4294967660" subkey=""/>
</item>
<item name="child2-4" key="4294967611" subkey=""/>
<item name="child2-5" key="4294967611" subkey="">
<item name="bb" key="4294967616" subkey=""/>
<item name="bb" key="4294967620" subkey=""/>
<item name="f" key="4294967615" subkey=""/>
</item>
</item>
</item>
</toc>
the wanted result is produced:
<ul>
<li>
<a href="#">top</a>
<ul>
<li>
<a href="#">child1</a>
<ul>
<li>
<a href="#">child2-1</a>
<ul>
<li>
<a href="#">child3-1</a>
</li>
<li>
<a href="#">child3-2</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
In case my guess about the wanted result was not a hit, please, correct your question and provide a well-formed output.
回答4:
This generates your desired output:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="item">
<ul>
<li>
<a href="'#'">
<xsl:value-of select="@name"/>
</a>
<xsl:apply-templates select="*"/>
</li>
</ul>
</xsl:template>
</xsl:stylesheet>
Instead of '#' as href you should save the url in your XML file like the name attribute. To find a Node you need to pass you search as a parameter. How to do this depends on how you do transformation.
来源:https://stackoverflow.com/questions/3385792/tree-navigation-with-xml-and-xslt