簡體   English   中英

當子項中的文本與 XSLT 1.0 匹配時,如何向父項添加屬性

[英]how to add attribute to a parent when the text in the child match with XSLT 1.0

我試圖將屬性添加到父節點中,當子節點中的文本匹配時,我有這個輸入:

<CS>
    <CN name="PICTURE 1">
        <TN name="L_1">
            <color>red</color>
            <ptCN>IN4</ptCN>
            <ID>10</ID>
        </TN>
    </CN>
    <CN name="PICTURE 2">
        <TN name="L_2">
            <color>blue</color>
            <ptCN>IN3</ptCN>
            <ID>20</ID>
        </TN>
    </CN>
<CS>

當屬性 color = red 時,我需要將 ready="yes" 添加到 TN 中,所以我會有這樣的東西:

<CS>
    <CN name="PICTURE 1" >
        <TN name="L_1" ready="yes">
            <color>red</color>
            <ptCN>IN4</ptCN>
            <ID>10</ID>
        </TN>
    </CN>
    <CN name="PICTURE 2">
        <TN name="L_2">
            <color>blue</color>
            <ptCN>IN3</ptCN>
            <ID>20</ID>
        </TN>
    </CN>
<CS>

我正在嘗試這個 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='//TN/color[text()="red"]'>
    <xsl:copy>
       <xsl:apply-templates select="@*"/>
        <xsl:attribute name="ready">yes</xsl:attribute>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

將從TNcolor的步驟放在條件中(在方括號中):

<xsl:template match='//TN[color/text()="red"]'>
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:attribute name="ready">yes</xsl:attribute>
    <xsl:apply-templates select="node()"/>
  </xsl:copy>
</xsl:template>

暫無
暫無

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

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