有沒有辦法可以根據元素的屬性值更新元素的值?
我在 xslt 下面使用,但它正在替換屬性的值而不是元素的值。
輸入 XML:
<Elements>
<Element>
<Entry1>
<data attribute="Delete">value</data>
</Entry1>
<Entry2 attribute="Delete"/>
<Entry3>
<data attribute="Update">value</data>
</Entry3>
</Element>
<Element attribute="Update">
<Entry1>
<data2>
<data3 attribute="Delete">value</data3>
</data2>
</Entry1>
</Element>
<Element attribute="Update">
<Entry1>
<data attribute="Delete">value</data>
</Entry1>
</Element>
</Elements>
XSLT:
<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="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@attribute">
<xsl:attribute name="attribute">
<xsl:choose>
<xsl:when test=". = 'Delete'">REMOVED</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
XSLT 產生的輸出:
<Elements>
<Element>
<Entry1>
<data attribute="REMOVED">value</data>
</Entry1>
<Entry2 attribute="REMOVED"/>
<Entry3>
<data attribute="Update">value</data>
</Entry3>
</Element>
<Element attribute="Update">
<Entry1>
<data2>
<data3 attribute="REMOVED">value</data3>
</data2>
</Entry1>
</Element>
<Element attribute="Update">
<Entry1>
<data attribute="REMOVED">value</data>
</Entry1>
</Element>
</Elements>
期望的輸出:
<Elements>
<Element>
<Entry1>
<data attribute="Delete">REMOVED</data>
</Entry1>
<Entry2 attribute="Delete"/>
<Entry3>
<data attribute="Update">value</data>
</Entry3>
</Element>
<Element attribute="Update">
<Entry1>
<data2>
<data3 attribute="Delete">REMOVED</data3>
</data2>
</Entry1>
</Element>
<Element attribute="Update">
<Entry1>
<data attribute="Delete">REMOVED</data>
</Entry1>
</Element>
</Elements>
uj5u.com熱心網友回復:
將您的主模板更改為
<xsl:template match="*[@attribute='Delete' and normalize-space(.)]">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:text>REMOVED</xsl:text>
</xsl:copy>
</xsl:template>
這會將所有元素的值替換為attribute具有“已洗掉”值且其元素值不為空的屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485293.html
上一篇:如何處理廣播訊息?
下一篇:重新編譯給出錯誤不平衡括號
