2015-07-13 40 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「元素,也需要元素進行轉換,但您始終可以編寫更復雜的條件來覆蓋該元素。」 可以,但我有他們覆蓋。 –