0

以下の XSLT では、次のことを確認しています

  1. 要素が null の場合は、それを別の要素の値に置き換えます。
  2. その要素の属性が null の場合は、定数値に置き換えます。

1 は機能しますが、2 は機能しません。

2については、次の2つのことを試しました:

まず、xsl:if条件を使用してもうまくいきませんでした。属性に値を挿入する代わりに、同じノード名で新しいノードを追加しています。

次に、テンプレートを使用しようとしました。それもうまくいきませんでした。ノードを完全に削除し、値を持つ親に属性を追加します。

また、別の方法またはより良い方法で行うことは可能ですか。

XSLT

  <xsl:template match="//ns0:Cedent/ns0:Party/ns0:Id">
    <xsl:if test="//ns0:Cedent/ns0:Party/ns0:Id = ''">
      <xsl:copy>
        <xsl:value-of select="//ns0:Broker/ns0:Party/ns0:Id"/>
      </xsl:copy>
    </xsl:if>
    <!--<xsl:if test="//ns0:Cedent/ns0:Party/ns0:Id[@Agency = '']">
      <xsl:copy>
        <xsl:attribute name="Agency">Legacy</xsl:attribute>
        <xsl:value-of select="'Legacy'"/>
      </xsl:copy>
    </xsl:if>-->
  </xsl:template>

  <xsl:template match="//ns0:Cedent/ns0:Party/ns0:Id[@Agency = '']">
    <xsl:attribute name="Agency">Legacy</xsl:attribute>
  </xsl:template>

入力

<ns0:Testing>
  <ns0:Cedent>
    <ns0:Party>
      <ns0:Id Agency=""></ns0:Id>
      <ns0:Name>Canada</ns0:Name>
    </ns0:Party>
  </ns0:Cedent>
  <ns0:Broker>
    <ns0:Party>
      <ns0:Id Agency="Legacy">292320710</ns0:Id>
      <ns0:Name>Spain</ns0:Name>
    </ns0:Party>
  </ns0:Broker>
</ns0:Testing>

出力

<ns0:Testing>
    <ns0:Cedent>
      <ns0:Party>
        <ns0:Id Agency="Legacy">292320710</ns0:Id>
        <ns0:Name>Canada</ns0:Name>
      </ns0:Party>
    </ns0:Cedent>
</ns0:Testing>
4

1 に答える 1

1
<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ns0="some_namespace_uri"
>
  <xsl:output indent="yes" />

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

  <xsl:template match="ns0:Cedent/ns0:Party/ns0:Id[. = '']">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:apply-templates select="
        ../../following-sibling::ns0:Broker[1]/ns0:Party/ns0:Id/node()
      " />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ns0:Cedent/ns0:Party/ns0:Id/@Agency[. = '']">
    <xsl:attribute name="Agency">Legacy</xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

あなたにあげる

<Testing xmlns="some_namespace_uri">
  <Cedent>
    <Party>
      <Id Agency="Legacy">292320710</Id>
      <Name>Canada</Name>
    </Party>
  </Cedent>
  <Broker>
    <Party>
      <Id Agency="Legacy">292320710</Id>
      <Name>Spain</Name>
    </Party>
  </Broker>
</Testing>

ノート:

  • 出力に要素がまったく必要ない場合は<Broker>、空のテンプレートを追加します。

    <xsl:template match="ns0:Broker" />
    
  • テンプレート内の一致式は、ルート ノードから開始する必要はありません。

  • このようなわずかな変更を加えて入力をコピーするスタイルシートは、常に ID テンプレートから開始する必要があります。
于 2013-05-04T05:42:00.297 に答える