2012-07-03 24 views
0

我想写xslt模板以匹配标记,后跟img标记。要匹配的模板<a>标记后跟<img>标记

样品有效来源:

1.<a title="google site" href="http://google.com"><img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/></a> 
2.<a title="Internal link" href="tcm:202-9720"><img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/></a> 

无效:

1.<a title="google site" href="http://google.com"></a> 
    <img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/> 

2.<img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/> 
<a title="google site" href="http://google.com"></a> 

3.<a title="google site" href="http://google.com"><img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/></a> 
    <a title="Internal link" href="tcm:202-9720"><img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/></a> 

4.<a><img/></a> 

规则:

1.Only one Image should be allowed and it should be hyperlink. 
2.There should be one <img> tag wrapped by one <a> tag. 
3. Multiple images are not allowed. 
4.Attributes for Image and a Tag. 
5.No text is allowed for both the tags. 

可以在任何帮助,如何编写模板来匹配这个条件。

+0

要与该标签做什么? –

+0

@ infantprogrammer'Aravind'。如果遇到这样的情况,我想检查一下情况。 – Patan

回答

0

使用此XPath在match属性为您的模板:

//a[count(@*)>0 and img[count(@*)>0] and count(.//*)=1 and normalize-space(.)=''] 

这将选择其中具有非显著文本内容的所有a元素,属性与属性的单一img元素。

1

请记住,XSLT模板不匹配标签,它们与元素匹配。 a元素的开始标签紧接着是img元素的开始标签,但是从XSLT的角度来看,您正在寻找的结构是一个只有img元素作为其唯一子元素的元素。

第四条规则“图像和标签的属性”。是不完整的:你没有说明两个元素的属性必须满足什么条件。

您还没有明确说明您是否希望模板规则与img元素或a元素相匹配。

这里有一个是img元素匹配规则提供的含有元素具有href属性:

match="a[@href]/img[not(preceding-sibling::node() or following-sibling::node())]"