从 RESULTING xslt 转换中删除空节点
Removing empty nodes from the RESULTING xslt transformation
我有以下需求,如果是href属性,我需要排除<xlink:link>
,如果它的值以“.xml”结尾,我需要排除<loc>
| “.xslt” | “。特性”。这有时会给我留下以下内容。 <div ...></div>
有没有办法一次性去掉这些<div ...></div>
,还是需要连续处理?
我输入的片段:
<?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>
我已经搜索了一些类似的问题,但他们都在解决从原始文档中删除空节点的问题,而不是结果文档中的空节点。
感谢任何帮助。
如果你添加
<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'))]"/>
那么那些只有 loc
和 xhtml:link/@href
的 url
元素将不会被转换,也不会创建输出。因此,使用这样的方法,您可以一步完成,我不知道 url
中是否存在其他内容,例如 foo
元素,这些内容也需要转换元素,但是您总是可以编写更复杂的条件来涵盖它。
我有以下需求,如果是href属性,我需要排除<xlink:link>
,如果它的值以“.xml”结尾,我需要排除<loc>
| “.xslt” | “。特性”。这有时会给我留下以下内容。 <div ...></div>
有没有办法一次性去掉这些<div ...></div>
,还是需要连续处理?
我输入的片段:
<?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>
我已经搜索了一些类似的问题,但他们都在解决从原始文档中删除空节点的问题,而不是结果文档中的空节点。
感谢任何帮助。
如果你添加
<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'))]"/>
那么那些只有 loc
和 xhtml:link/@href
的 url
元素将不会被转换,也不会创建输出。因此,使用这样的方法,您可以一步完成,我不知道 url
中是否存在其他内容,例如 foo
元素,这些内容也需要转换元素,但是您总是可以编写更复杂的条件来涵盖它。