1

私は以下のような入力XMLを持っています

<testing>
<subject ref="yes">
 <firstname>
    tom
 </firstname>
</subject>
<subject ref="no">
 <firstname>
    sam
</firstname>
</subject>
</testing>

私の出力はそうあるべきだと期待しています。

件名に「はい」の参照がある場合。名前の値を取得します。それ以外の場合、参照(いいえ)私は要素を取得しません

<testing>
<firstname>
   tom
</firstname>
</testing>

ここで案内してください。

4

3 に答える 3

2

これは、ID変換の上に構築することで実現できます。まず、 @refが「no」のサブジェクト要素を無視するためのテンプレートが必要になります

<xsl:template match="subject[@ref='no']" />

また、@ refが「yes」のサブジェクト要素の場合、その子だけを出力する別のテンプレートがあります

<xsl:template match="subject[@ref='yes']">
   <xsl:apply-templates select="node()"/>
</xsl:template>

実際、@refが「yes」または「no」にしかできない場合は、このテンプレートの一致を単純化し<xsl:template match="subject">て、「no」の@refを持たないすべての要素に一致させることができます。

これが完全なXSLTです

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

   <xsl:template match="subject[@ref='no']" />

   <xsl:template match="subject">
      <xsl:apply-templates select="node()"/>
   </xsl:template>

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

サンプルXMLに適用すると、次のように出力されます。

<testing>
<firstname> tom </firstname>
</testing>
于 2012-08-08T11:46:12.217 に答える
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="/*">
  <testing><xsl:apply-templates/></testing>
 </xsl:template>

 <xsl:template match="subject[@ref='yes']">
  <xsl:copy-of select="node()"/>
 </xsl:template>
 <xsl:template match="subject"/>
</xsl:stylesheet>

提供されたXMLドキュメントに適用した場合

<testing>
    <subject ref="yes">
        <firstname>
         tom
     </firstname>
    </subject>
    <subject ref="no">
        <firstname>
         sam
     </firstname>
    </subject>
</testing>

必要な正しい結果を生成します

<testing>
   <firstname>
         tom
     </firstname>
</testing>
于 2012-08-08T12:07:17.533 に答える
0

これを試して:

<testing>
  <xsl:if test="testing/subject/@ref = 'yes'">
    <firstname>
      <xsl:value-of select="testing/subject/firstname" />
    </firstname>
  </xsl:if>
</testing>

これがxsltで機能することを願っています

于 2012-08-08T11:21:40.203 に答える