XSLT:返回带有 [not(object)] 模板匹配的值会削减模板匹配
XSLT: Returning a value with a [not(object)] template match cuts template matches
我正在创建一个 XSLT,我想从中获取 XML 元素的值。如果它们不存在,我想确保插入一些默认值。我尝试使用 XSL:template 匹配来完成这项工作,效果很好,然后 return 如果元素不存在则为一个值
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>
我正在创建一个 XSLT,我想从中获取 XML 元素的值。如果它们不存在,我想确保插入一些默认值。我尝试使用 XSL:template 匹配来完成这项工作,效果很好,然后 return 如果元素不存在则为一个值
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>