我正在創建一個 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>
如何確保在此處添加所有默認值?謝謝!
uj5u.com熱心網友回復:
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/442394.html
