XSLT:使用[not(object)]模板匹配返回值将剪切模板匹配



我正在创建一个XSLT,希望从XML元素中获取值。如果它们不存在,我希望确保插入一些默认值。我尝试使用XSL:template-match来实现这一点,它非常有效,然后如果元素不存在,则返回一个值xsl:template match="a/b/[not(c)]

这会添加默认值,但随后会删除所有其他值。

在此基础上,我尝试使用xsl:apply-templates来添加所有现有对象的值。这是有效的,但仅适用于单个[not(object)]子句。也就是说,如果有多个不存在的对象匹配,那么只有最后一个对象与所有匹配的对象一起显示。

我如何才能确保我得到所有";默认";值(或:不存在对象的值(,同时获取所有现有元素?

以下是我当前XSLT:的简化版本

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<items>
<xsl:apply-templates select="LinkedHashMap"/>
</items>
</xsl:template>

<xsl:template match="A/items/itemA">
<first>
<xsl:value-of select="."/>
</first>
</xsl:template>

<xsl:template match="A/items[not(itemA)]">
<first>
"Default value"
</first>
<xsl:apply-templates/>
</xsl:template>


<xsl:template match="A/items/itemB">
<second>
<xsl:value-of select="."/>
</second>
</xsl:template>
<xsl:template match="A/items[not(itemB)]">
<second>
"Default value"
</second>
<xsl:apply-templates/>
</xsl:template>


<xsl:template match="A/items/itemC">
<third>
<xsl:value-of select="."/>
</third>
</xsl:template>
<xsl:template match="A/items[not(itemC)]">
<third>
"Default value"
</third>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="A/items/itemD">
<third>
<xsl:value-of select="."/>
</third>
</xsl:template>
<xsl:template match="A/items[not(itemD)]">
<fourth>
"Default value"
</fourth>
<xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

输入:

<LinkedHashMap>
<A>
<items>
<itemA>First item</itemA>
<itemB>Second item</itemB>
</items>
</A>
</LinkedHashMap>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<items>

<fourth>
"Default value"
</fourth>
<first>First item</first>
<second>Second item</second>


</items>

我还希望在哪里找到

<third>"default value"</third>

如何确保在此处添加所有默认值?谢谢

<xsl:template match="node()">
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template match="items">
<xsl:copy>
<xsl:choose>
<xsl:when test="itemA">
<xsl:apply-templates select="itemA"/>
</xsl:when>
<xsl:otherwise>
<first>
"Default value"
</first>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="itemB">
<xsl:apply-templates select="itemB"/>
</xsl:when>
<xsl:otherwise>
<second>
"Default value"
</second>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="itemC">
<xsl:apply-templates select="itemC"/>
</xsl:when>
<xsl:otherwise>
<third>
"Default value"
</third>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="itemD">
<xsl:apply-templates select="itemD"/>
</xsl:when>
<xsl:otherwise>
<forth>
"Default value"
</forth>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
<xsl:template match="itemA">
<first>
<xsl:value-of select="."/>
</first>
</xsl:template>
<xsl:template match="itemB">
<second>
<xsl:value-of select="."/>
</second>
</xsl:template>
<xsl:template match="itemC">
<third>
<xsl:value-of select="."/>
</third>
</xsl:template>
<xsl:template match="itemD">
<third>
<xsl:value-of select="."/>
</third>
</xsl:template>

最新更新