1

複数のチェック条件があり、特定の条件に基づいて出力要素がコピーされるxslt1.0で、以下のような冗長なコーディングを回避する方法があるかどうかを確認したかっただけです。条件が真でない場合、要素自体は出力に含まれません。私が尋ねている理由は、xslファイルに多くの要素が存在するからです。

私のxslt

<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="/">
    <Root>
    <xsl:if test="Root/a/text() = '1'">
      <first>present</first>   
    </xsl:if>
    <xsl:if test="Root/b/text() = '1'">
      <second>present</second>
    </xsl:if>
    <xsl:if test="Root/c/text() = '1'">
      <third>present</third>
    </xsl:if>
    <xsl:if test="Root/d/text() = '1'">
      <fourth>present</fourth>
    </xsl:if>
    </Root>
  </xsl:template>
</xsl:stylesheet>

私の入力xml

<Root>
  <a>1</a>
  <b>1</b>
  <c>0</c>
  <d>1</d>  
</Root>

私の出力

<Root>
  <first>present</first>
  <second>present</second>
  <fourth>present</fourth>
</Root>
4

2 に答える 2

2
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:ord>
   <first>first</first>
   <second>second</second>
   <third>third</third>
   <fourth>fourth</fourth>
 </my:ord>

 <xsl:variable name="vOrds" select="document('')/*/my:ord/*"/>

 <xsl:template match="Root/*[. = 1]">
  <xsl:variable name="vPos" select="position()"/>

  <xsl:element name="{$vOrds[position()=$vPos]}">present</xsl:element>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

この変換が提供された XML ドキュメントに適用されると、次のようになります。

<Root>
  <a>1</a>
  <b>1</b>
  <c>0</c>
  <d>1</d>  
</Root>

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

<Root>
  <first>present</first>
  <second>present</second>
  <fourth>present</fourth>
</Root>
于 2012-06-19T14:37:45.890 に答える
1

これを行う 1 つの方法は、output-template.xml で出力用のテンプレートを作成することです。

<Root>
  <first>present</first>
  <second>present</second>
  <third>present</third>
  <fourth>present</fourth>
</Root>

そして、これを処理します:

<xsl:variable name="input" select="/"/>

<xsl:template match="Root/*">
  <xsl:variable name="p" select="position()"/>
  <xsl:if test="$input/Root/*[$p] = '1'">
    <xsl:copy-of select="."/>
  </xsl:if>
</xsl:template>

<xsl:template match="/">
  <Root>
    <xsl:apply-templates select="document('output-template.xml')/Root/*"/>
  </Root>
</xsl:template>
于 2012-06-19T14:37:45.997 に答える