2015-07-13 38 views
0

我有以下要求,如果它的href属性需要排除<xlink:link>,如果值为“.xml”,则需要<loc> | <loc> | “.xslt”| “的.properties”。这有时会给我带来以下问题。 <div ...></div>有没有办法一次去除这些<div ...></div>,还是需要连续处理?从RESULTING xslt转换中删除空节点

片段我输入:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:xhtml="http://www.w3.org/1999/xhtml"> 
    <url> 
     <loc id="843">en-us/system/xml/navigation.xml</loc> 
     <xhtml:link href="/fr-fr/system/xml/navigation.xml" hreflang="fr-fr" rel="alternate"/> 
     <xhtml:link href="/en-uk/system/xml/navigation.xml" hreflang="en-uk" rel="alternate"/> 
    </url> 
    <url> 
     <loc id="3159">/en-us/index.html</loc> 
     <xhtml:link href="/fr-lu/index.html" hreflang="fr-lu" rel="alternate"/> 
    </url> 
</urlset> 

我的XSLT:

<xsl:stylesheet version="2.0" xpath-default-namespace="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="video image xsl xhtml"> 
<xsl:output method="xhtml" indent="yes" omit-xml-declaration="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy copy-namespaces="no"> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
</xsl:template> 

<!-- transform urlset --> 
<xsl:template match="urlset"> 
    <div class="urlset"><xsl:apply-templates select="@*|node()" /></div> 
</xsl:template> 

<!-- transform url --> 
<xsl:template match="url"> 
    <div class="url"><xsl:apply-templates select="@*|node()" /></div> 
</xsl:template> 

<!-- transform the 'link' when valid --> 
<xsl:template match="xhtml:link"> 
    <a><xsl:apply-templates select="@*|node()" /></a> 
</xsl:template> 

<!-- transform the 'loc' when valid --> 
<xsl:template match="loc"> 
    <a> 
     <xsl:attribute name="href"> 
      <xsl:value-of select="." /> 
     </xsl:attribute> 
     <xsl:apply-templates select="@*" /> 
    </a> 
</xsl:template> 


<!-- transform the 'link' when not valid --> 
<xsl:template match="xhtml:link[@href[ends-with(.,'.xml') or ends-with(.,'.xslt') or ends-with(.,'.properties')]]" /> 

<!-- transform the 'loc' when not valid --> 
<xsl:template match="loc[ends-with(.,'.xml') or ends-with(.,'.xslt') or ends-with(.,'.properties')]" /> 

</xsl:stylesheet> 

结果:

<div class="urlset"> 
    <div class="url"></div> <=== *** I need to get rid of the likes of this *** 
    <div class="url"> 
     <a href="/en-us/index.html" id="3159"></a> 
     <a href="/fr-lu/index.html" hreflang="fr-lu" rel="alternate"></a> 
    </div> 

我已经做了一些搜索类似的问题,但他们都是地址从原始文档中删除空节点,而不是最终的节点。

任何帮助表示赞赏。

回答

0

如果添加

<xsl:template match="url[every $node in (loc, xhtml:link/@href) satisfies (ends-with($node,'.xml') or ends-with($node,'.xslt') or ends-with($node,'.properties'))]"/> 

然后只与您要删除将不会被改变了结局locxhtml:link/@href并不会产生输出那些url元素。因此,采用这种方法,您可以一步完成,但我不知道是否可以在url中显示其他内容,例如foo元素,这些内容也需要转换元素,但您始终可以编写更复杂的条件来覆盖那。

+0

我无法编辑你的评论,但在'properties')]“/>'中有一个'''',它应该是'properties'))]”/>'。 工程就像一个魅力。谢谢。 “我不知道是否可以存在其他内容,例如url中存在的”foo“元素,也需要元素进行转换,但您始终可以编写更复杂的条件来覆盖该元素。” 可以,但我有他们覆盖。 –