簡體   English   中英

使用xslt復制包含特定子元素的父元素

[英]Copy parent element that contains specific child element using xslt

我有一個簡單的xml:

<custom-objects>
    <custom-object id="1">
        <object-attribute attribute-id="address1">Warndtstr. 33</object-attribute>
        <object-attribute attribute-id="branch">01</object-attribute>
        <object-attribute attribute-id="catalogid">7991</object-attribute>
        <object-attribute attribute-id="exportdate">2015-09-19</object-attribute>
    </custom-object>
    <custom-object>
...
    </custom-object>
</custom-objects>

我試圖簡單地復制每個包含子項的<custom-object>元素,其中@attribute-id"exportdate"並且具有特定的textvalue。

這是我的xslt:

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

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

    <xsl:template match="custom-object[object-attribute[@attribute-id='exportdate']='2015-09-19']"/>
</xsl:stylesheet>

使用xpath時匹配正在運行。 xslt返回一個空結果。

  • 為什么在這種情況下不起作用?
  • 我的錯誤在哪里?

“我正在嘗試簡單地復制包含子項的每個元素,其中@ attribute-id是”exportdate“並且具有特定的文本值。”

應該用於刪除元素的空模板。 所以目前,你的XSL應該刪除'exportdate'等於'2015-09-19'的custom-object ,並復制其他元素。 如果您想要相反,請嘗試使用具有相反含義的XPath,例如:

<xsl:template 
    match="custom-object[not(object-attribute[@attribute-id='exportdate']='2015-09-19')]"/>

暫無
暫無

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

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