1

特定の属性(myId)を持つドキュメントがあり、その値がゼロになるたびに値を更新する必要があります。ドキュメントは次のようになります

<?xml version="1.0" encoding="UTF-8"?><Summary>
 <Section myId="0">
  <Section myId="0">
   <Para>...</Para>
  </Section>
  <Section myId="5">
   <Para>...</Para>
  </Section>
 </Section>
</Summary>

テンプレートを使用して属性myIdを照合し、呼び出し元のプログラムから渡された一意のIDに設定していますが、ドキュメント内の属性の1つのみを照合したいと考えています。値がゼロの追加の属性は、別のIDを渡すことで更新されます。私が使用しているテンプレートは次のようになります。

 <xsl:template        match  = '@myId[.="0"]'>
  <xsl:attribute name = "{name()}">
   <xsl:value-of select = "$addValue"/>
  </xsl:attribute>
 </xsl:template>

値addValueは、呼び出し側プログラムから渡されるグローバルパラメーターです。1日のかなりの時間、答えを探しましたが、このテンプレートを1回だけ適用することはできません。出力は、両方のmyId値をaddValueのコンテンツに置き換えます。'@myId[。"0"][1]'と一致させようとし、position()関数を使用して一致させようとしましたが、テンプレートは常にゼロであるすべてのmyId属性に適用されます。

一致するテンプレートを1回だけ適用することは可能ですか?

4

1 に答える 1

1

一致するテンプレートを1回だけ適用することは可能ですか?

はい

  1. テンプレートが適用されるかどうかは、テンプレートがxsl:apply-templates実行対象として選択される原因によって異なります。

  2. さらに、一致パターンは、テンプレートがドキュメント内の1つの特定のノードのみと一致することを保証する方法で指定できます。

これがあなたができることです

<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:param name="pNewIdValue" select="9999"/>


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

 <xsl:template match=
 "Section
   [@myId = 0
  and
    not((preceding::Section | ancestor::Section)
                 [@myId = 0]
       )
   ]/@myId">
  <xsl:attribute name="myId"><xsl:value-of select="$pNewIdValue"/></xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

この変換が提供されたXMLドキュメントに適用される場合

<Summary>
    <Section myId="0">
        <Section myId="0">
            <Para>...</Para>
        </Section>
        <Section myId="5">
            <Para>...</Para>
        </Section>
    </Section>
</Summary>

必要な正しい結果が生成されます。

<Summary>
   <Section myId="9999">
      <Section myId="0">
         <Para>...</Para>
      </Section>
      <Section myId="5">
         <Para>...</Para>
      </Section>
   </Section>
</Summary>
于 2012-05-05T02:40:56.310 に答える