2

私はxmlデータを持っています。そして、xslt 変換を使用して解析します。私が必要とするのは、特定の要素のすべての子要素が同じ入れ子要素の値を持っているかどうかを調べることです。見て:

に同じ値がある場合:

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>1</compare>
   </child>
</parent>

すべてのツリーをコピーし、フラグを「1」に設定する必要があります。

<parent>
   ...
   </child>
   <flag>1</flag>
</parent>

値が異なる場合:

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>2</compare>
   </child>
</parent>

すべてのツリーをコピーし、フラグを "" に設定する必要があります。

<parent>
   ...
   </child>
   <flag/>
</parent>
4

3 に答える 3

2

How about comparing if anything is different from the first?

<xsl:template match="/parent">
  <parent>
    <xsl:copy-of select="*"/>
    <flag>
      <xsl:if test="not(child[1]/compare != child/compare)">1</xsl:if>
    </flag>
  </parent>
</xsl:template>

this does mean if you only have one it will have a flag of 1

于 2012-06-13T15:59:29.177 に答える
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:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
       <flag><xsl:value-of select=
       "substring('1', 2 - not(child[compare != current()/child/compare]))"/></flag>
     </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

次の XML ドキュメントに適用した場合:

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>1</compare>
   </child>
</parent>

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

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>1</compare>
   </child>
   <flag>1</flag>
</parent>

この XML ドキュメントに同じ変換を適用すると、次のようになります。

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>2</compare>
   </child>
</parent>

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

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>2</compare>
   </child>
   <flag/>
</parent>

注意してください:

  1. アイデンティティ ルールの使用とオーバーライド。

  2. 明示的な条件付き命令 (または変数) はまったく使用されません。

于 2012-06-14T02:51:09.550 に答える
1
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes"/>

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

    <xsl:template match="parent">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
            <flag>
                <xsl:variable name="comp" select="child[1]/compare"/>
                <!-- add flag value only if child/compare are the same -->
                <xsl:value-of select="child[1]/compare[
                    count(current()/child)
                    = 
                    count(current()/child[ compare=$comp ]
                    )]"/>
            </flag>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
于 2012-06-13T21:03:33.923 に答える