0

私のソース XML では、すべてのインスタンス ID を番号 1 からそれ以降 (存在するまで) に再割り当てしています。Apple、Mango、Banana の InstanceId を Profile 要素の下の上記の値で更新しています。ここでは、Profile InstanceID レベルのオカレンスを無視します。私の要件は、特定の「プロファイル」セット内の少なくとも 1 つのインスタンス ID が 25 より大きい場合にのみ、これらの xslt コードをトリガーすることです。

<Root>
  <Properties>
    <Props></Props>
    <Input></Input>
    <Profile InstanceID="4" ObjectID="XYZ">
      <foo>Profile 1</foo><!-- Need to update these set as atleast one instanceID is greater than 25 -->
      <Apple InstanceID="26" ObjectID="ABC" Type="103"></Apple>
      <Mango InstanceID="1" ObjectID="DEF" Type="103"></Mango>
      <Mango InstanceID="27" ObjectID="GHI" Type="103"></Mango>
      <Banana InstanceID="29" ObjectID="GHI1" Type="103"></Banana>
    </Profile>
  </Properties>
  <Properties>
    <Props></Props>
    <Input></Input>
    <Profile InstanceID="4" ObjectID="XYZ">
      <foo>Profile 1</foo><!-- no need to update these set as no instanceID is greater than 25 -->
      <Apple InstanceID="21" ObjectID="MNO" Type="103"></Apple>
      <Mango InstanceID="21" ObjectID="PQR" Type="103"></Mango>
      <Mango InstanceID="23" ObjectID="EFG" Type="103"></Mango>
      <Mango InstanceID="24" ObjectID="EFG123" Type="103"></Mango>
      <Banana InstanceID="25" ObjectID="GHI1" Type="103"></Banana>
    </Profile>
  </Properties>
</Root>

以下は私が使用したXSLTです

# XSLT #
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        exclude-result-prefixes="msxsl">
  <xsl:output omit-xml-declaration="yes" indent="yes" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Profile/*/@InstanceID">
    <xsl:attribute name="InstanceID">
      <xsl:value-of select="count(../preceding-sibling::*[@InstanceID]) + 1" />
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

そして、これらは出力/変換された XML です

# Transformed XML #
<Root>
  <Properties>
    <Props></Props>
    <Input></Input>
    <Profile InstanceID="4" ObjectID="XYZ"><!-- no need to update these instanceID -->
      <foo>Profile 1</foo>
      <Apple InstanceID="1" ObjectID="ABC" Type="103"></Apple>
      <Mango InstanceID="2" ObjectID="DEF" Type="103"></Mango>
      <Mango InstanceID="3" ObjectID="GHI" Type="103"></Mango>
      <Banana InstanceID="4" ObjectID="GHI1" Type="103"></Banana>
    </Profile>
  </Properties>
  <Properties>
    <Props></Props>
    <Input></Input>
    <Profile InstanceID="4" ObjectID="XYZ"><!-- no need to update these instanceID -->
      <foo>Profile 1</foo>
      <Apple InstanceID="1" ObjectID="MNO" Type="103"></Apple>
      <Mango InstanceID="2" ObjectID="PQR" Type="103"></Mango>
      <Mango InstanceID="3" ObjectID="EFG" Type="103"></Mango>
      <Mango InstanceID="4" ObjectID="EFG123" Type="103"></Mango>
      <Banana InstanceID="5" ObjectID="GHI1" Type="103"></Banana>
    </Profile>
  </Properties>
</Root>
4

1 に答える 1