簡體   English   中英

XSLT:如何更新上一級節點以進行匹配

[英]XSLT: how to update the node of previous level for match

這是示例Xml,我想檢查是否有將Input輸入為“ Else”的RangeItem(否則只有1個RangeItem),然后我想將該RangeItem的Output值設置為Default的值,即上一級。

 <ColorConverter> <Property ID="Default">#76D5F9</Property> <RangeItem> <Property ID="Name">RangeItem1</Property> <Property ID="Input">Else</Property> <Property ID="Output">#4A3737</Property> </RangeItem> <RangeItem> <Property ID="Name">RangeItem2</Property> <Property ID="Input">Equal</Property> <Property ID="Output">#FFFFFF</Property> </RangeItem> </ColorConverter> 

預期產量:

 <ColorConverter> <Property ID="Default">#4A3737</Property> <RangeItem> <Property ID="Name">RangeItem1</Property> <Property ID="Input">Else</Property> <Property ID="Output">#4A3737</Property> </RangeItem> <RangeItem> <Property ID="Name">RangeItem2</Property> <Property ID="Input">Equal</Property> <Property ID="Output">#FFFFFF</Property> </RangeItem> </ColorConverter> 

請幫我。

如果我理解正確,您想執行以下操作:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="ColorConverter">
    <xsl:variable name="default-item" select="RangeItem[Property[@ID='Input']='Else']" />
    <xsl:copy>
        <Property ID="Default">
            <xsl:choose>
                <xsl:when test="$default-item">
                    <xsl:value-of select="$default-item/Property[@ID='Output']"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="Property[@ID='Default']"/>
                </xsl:otherwise>
            </xsl:choose>
        </Property>
        <xsl:apply-templates select="RangeItem"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM