[英]Checking if preceding has particular type/Value in XSLT
我有一個類似以下的XML
<ARTICLE type="newspaper" segment="entertainment">
<Price type="tax">$2</Price>
</ARTICLE>
<ARTICLE type="newspaper" segment="political">
<Price type="tax">$1</Price>
</ARTICLE>
現在,我要執行的操作是僅在緊鄰的頂部元素是ARTICLE且具有segment =“ entertainment”時才刪除價格(塊)
我嘗試了以下方法,但是它刪除了兩個塊
<xsl:template match="Price[@type='tax'] ">
<xsl:if test="preceding::ARTICLE[1]/attribute(segment)='entertainment'">
<xsl:apply-templates/>
</xsl:if>
</xsl:template>
在這方面需要幫助。 僅當細分是緊靠上條的“娛樂”時,我才必須刪除價格塊。
很難說為什么沒有更多上下文(如格式良好的示例XML)模板就無法工作。
這應該可以工作:
<xsl:template match="Price[@type='tax' and parent::ARTICLE[@segment='entertainment']]"/>
如果仍然需要Price
的文本,請添加xsl:apply-templates
:
<xsl:template match="Price[@type='tax' and parent::ARTICLE[@segment='entertainment']]">
<xsl:apply-templates/>
</xsl:template>
你可以嘗試一下:
<xsl:template match="Price[@type='tax'] ">
<xsl:if test="preceding-sibling::ARTICLE[1]/attribute(segment)='entertainment'">
<xsl:apply-templates/>
</xsl:if>
</xsl:template>
希望能幫助到你..
只需使用模板:
<xsl:template match="ARTICLE[@segment='entertainment']/Price" />
這與您要查找的price
元素完全匹配,並且在該位置不輸出任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.