[英]Once apply XSLT template
我有一個XML
<root>
<foo>Hello</foo>
<bar>World</bar>
</root>
和模板
<xsl:template match="foo">
<div>
<span><xsl:value-of select="." /></span>
<xsl:apply-templates select="//bar" mode="inner" />
</div>
</xsl:template>
<xsl:template match="bar">
<span><xsl:value-of select="." /></span>
</xsl:template>
<xsl:template match="bar" mode="inner">
<span><xsl:value-of select="." /></span>
</xsl:template>
期望
<div>
<span>Hello</span>
<span>World</span>
</div>
實際
<div>
<span>Hello</span>
<span>World</span>
</div><span>World</span>
我沒有可能阻止block bar的輸出。 該模板在其他地方使用
<xsl:template match="bar" />
-編輯-
我無法覆蓋根模板(這是不同級別的抽象)
<xsl:template match="/root">
<xsl:apply-templates />
</xsl:template>
我不能根據需要阻止使用模板欄 。
<root>
<bar>World</bar>
</root>
期望
<span>World</span>
-真實的例子-
模板
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output
method="xml"
version="1.0"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
omit-xml-declaration="yes"
media-type="text/html"
encoding="utf-8"
indent="yes"
/>
<xsl:template match="/root">
<html>
<head></head>
<body>
<xsl:apply-templates select="block[@place='main']"/>
</body>
</html>
</xsl:template>
<xsl:template match="block/page">
<div class="page-second">
<xsl:apply-templates select="//block/news" mode="inner" />
<div class="info">
<h1><xsl:value-of select="title" /></h1>
<xsl:value-of select="text" />
</div>
</div>
</xsl:template>
<xsl:template match="block/news">
<div class="clear" />
<div class="news-block">
<h2><xsl:value-of select="title" /></h2>
<ul>
<xsl:apply-templates select="article" />
</ul>
</div>
</xsl:template>
<xsl:template match="block/news" mode="inner">
<div class="news-block">
<h2><xsl:value-of select="title" /></h2>
<ul>
<li><a href="{article/link}"><xsl:value-of select="article/title" /></a></li>
</ul>
</div>
</xsl:template>
<xsl:template match="block/news/article">
<li><a href="{link}"><xsl:value-of select="title" /></a></li>
</xsl:template>
</xsl:stylesheet>
xml 1
<root>
<block place="main">
<page>
<title>Page title</title>
<text>Page text</text>
</page>
</block>
<block place="main">
<news>
<title>News title</title>
<article>
<title>Article 1 title</title>
<link>/article-1/</link>
</article>
<article>
<title>Article 2 title</title>
<link>/article-2/</link>
</article>
</news>
</block>
</root>
預期結果1
<html>
<head></head>
<body>
<div class="page-second">
<div class="news-block">
<h2>News title</h2>
<ul>
<li><a href="/article-1/">Article 1 title</a></li>
</ul>
</div>
<div class="info">
<h1>Page title</h1>
Page text
</div>
</div>
</body>
</html>
XML 2
<root>
<block place="main">
<news>
<title>News title</title>
<article>
<title>Article 1 title</title>
<link>/article-1/</link>
</article>
<article>
<title>Article 2 title</title>
<link>/article-2/</link>
</article>
</news>
</block>
</root>
結果2
<html>
<head></head>
<body>
<div class="clear" />
<div class="news-block">
<h2>News title</h2>
<ul>
<li><a href="/article-1/">Article 1 title</a></li>
<li><a href="/article-2/">Article 2 title</a></li>
</ul>
</div>
</body>
</html>
您應該執行以下操作:
<xsl:template match="/">
<xsl:apply-templates select="//foo"/>
</xsl:template>
<xsl:template match="foo">
<div>
<span><xsl:value-of select="." /></span>
<xsl:apply-templates select="//bar" mode="inner" />
</div>
</xsl:template>
<xsl:template match="bar">
<span><xsl:value-of select="." /></span>
</xsl:template>
<xsl:template match="bar" mode="inner">
<span><xsl:value-of select="." /></span>
</xsl:template>
第一個模板(匹配項/)將在根級別執行,如果不需要子元素,則不會對其進行解析。 您的解決方案將解析所有內容,並將正確的模板與其關聯。
看一下您簡單的“ foo / bar”示例,您說的是,當存在“ foo”時,您想要對后續的“ bar”信封進行一些不同的處理,而不希望“ bar”的其他模板能夠在選擇root的子元素時適用。
在這種情況下,您需要做的就是將模板添加到XSLT中,如果存在“ foo”元素則忽略“ bar”元素(因為您知道在這種情況下,匹配“ foo”的模板正在使用另一個模板進行處理) 。
<xsl:template match="bar[../foo]" />
現在,在您的實際示例中,“ foo”元素是帶有“ page”元素作為子元素的“ block”元素,而您的“ bar”元素是帶有“ news”元素作為子元素的“ block”元素。 這意味着您只需要將此模板添加到現有XSLT中即可。
<xsl:template match="block[news][../block/page]" />
或者,如果“地點”屬性很重要
<xsl:template match="block[@place='main'][news][../block[@place='main']/page]" />
因此,它同時具有“頁面”和“新聞”項,這將停止“根”模板中的<xsl:apply-templates />
正常處理“新聞”項。
您所有其他XSLT都可以保持原樣,只需將上面的模板添加到其中,它就可以解決問題。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="foo|bar">
<span>
<xsl:apply-templates/>
</span>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.