1
<xsl:template match="foobar">
    <xsl:if test="a[name = 'foo']">
        <xsl:apply-templates select="x/y[1]|x/y[2]" />
    </xsl:if>
    <xsl:if test="a[name = 'bar']">
        <xsl:apply-templates select="x/y[3]|x/y[4]|x/y[5]" />
    </xsl:if>
</xsl:template>

ロケーション パス式 "x/y[1]|x/y[2]" および x/y[3]|x/y[4]|x/y[5] をパラメーターとして渡したいのですが、この値はは将来変更される可能性があり、テンプレートは編集せずにパラメーター定義のみを編集したいと考えています。上記のテンプレートを次のように使用したいと思います

<xsl:template match="foobar">
    <xsl:if test="a[name = 'foo']">
        <xsl:apply-templates select="$param1" />
    </xsl:if>
    <xsl:if test="a[name = 'bar']">
        <xsl:apply-templates select="$param2" />
    </xsl:if>
</xsl:template>

私の知る限り、これは不可能です。ロケーションパス式を外部化する最良の方法は何ですか?

よろしくお願いします

4

2 に答える 2

1
<xsl:template match="foobar">
    <xsl:param name="param1" />
    <xsl:param name="param2" />
    <xsl:if test="a[name = 'foo']">
        <xsl:apply-templates select="$param1" />
    </xsl:if>
    <xsl:if test="a[name = 'bar']">
        <xsl:apply-templates select="$param2" />
    </xsl:if>
</xsl:template>

次に、次のようにテンプレートを呼び出します。

<xsl:call-template name="foobar">
    <xsl:with-param name="param1" select="actualXPath1"/>
    <xsl:with-param name="param2" select="actualXPath2"/>
</xsl:call-template>

または、XSLT ファイルの先頭でグローバル パラメータを使用できます。

<xsl:param name="param1" select="actualXPath1"/>
<xsl:param name="param2" select="actualXPath2"/>
<!-- continue with template definitions -->
...

この記事が役立つかもしれません。

于 2012-09-29T18:38:24.730 に答える
0

独自の関数や EXSLT を使用してもかまわない場合は、文字列として使用できる XPath 式を動的に評価できます。

  • EXSLT のdyn:evaluate(string)
  • Saxon 6.5.5 および 9.x には、独自の evaluate(string) 関数もあります。

次に、これらの XPath 文字列をグローバル パラメーターとして XSLT に渡したり、たとえば、外部ファイルに保存したりできます。

于 2012-10-01T08:18:38.080 に答える