XSLT でのパラメーターの設定
XSLT スタイルシートにパラメーターを使用するには、スタイルシート自体にグローバル・パラメーターとしてのパラメーターを宣言します。
手順
- as 属性を使用して、パラメーターの型を宣言します。 以下のスタイルシートは 2 つのパラメーターを宣言したものです。これらのパラメーターの内の 1 つには、型が明示的に指定されています。
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:param name="targetDate" as="xs:date"/> <xsl:param name="window" select="3"/> <xsl:template match="/"> <table> <tr><td>Task</td><td>Due</td><td>Status</td></tr> <xsl:apply-templates select="todo-list/task[xs:date(@due) le $targetDate + xs:dayTimeDuration(concat('P', $window, 'D'))]"> <xsl:sort select="@due"/> </xsl:apply-templates> </table> </xsl:template> <xsl:template match="task"> <tr> <td><xsl:value-of select="."/></td> <td><xsl:value-of select="format-date(xs:date(@due), '[MNn] [D1o]')"/></td> <td> <xsl:choose> <xsl:when test="xs:date(@due) lt $targetDate">OVERDUE by <xsl:value-of select="days-from-duration($targetDate - xs:date(@due))"/> day(s)</xsl:when> <xsl:otherwise>Due in <xsl:value-of select="days-from-duration(xs:date(@due) - $targetDate)"/> day(s)</xsl:otherwise> </xsl:choose> </td> </tr> </xsl:template> </xsl:stylesheet>
- 静的コンテキストには何も宣言する必要がないため、他のスタイルシートとまったく同様に、グローバル・パラメーターを指定した XSLT スタイルシートを作成します。 このスタイル・シートが xsltSource ソース・オブジェクトを使用して使用可能であると仮定すると、以下のコードでスタイル・シートが作成されます。
// Create the factory XFactory factory = XFactory.newInstance(); // Prepare the XSLT stylesheet XSLTExecutable xslt = factory.prepareXSLT(xsltSource);
- パラメーターを使用した XSLT スタイルシートを実行するには、XDynamicContext インスタンスを使用して
各パラメーターごとに値を指定 (またはバインド) します。
必須パラメーターには値を指定してください。 そうしないとエラーが表示されます。それ以外のパラメーターについては、値を指定しない場合は デフォルト値 (スタイルシート内に提供されている値か、またはゼロ長ストリング) が使用されます。
XDynamicContext には多くの bind、bindItem、および bindSequence メソッドがあります。各メソッドには 2 つのパラメーターがあります。1 番目の パラメーターはそのパラメーターの名前に対応する QName オブジェクトであり、2 番目は値です。表 1. XDynamicContext の bind、bindItem、および bindSequence メソッド. 以下の表に、 XDynamicContext の bind、bindItem、および bindSequence メソッドの各書式を使用する時機を示します。
メソッド 目的 バインド (bind) 単一のアトミック値をバインドするときに使用します 組み込み型から Java™ 型への標準マッピングに使用される Java 型のそれぞれに、 このメソッドの書式が 1 つずつあります。 このメソッドには、2 つの追加書式があります。1 つはノード・オブジェクトを取り、もう 1 つはソース・オブジェクトを取ります。 これらの書式は、それぞれ、DOM ツリーからノードをバインドするため、および新規ソースを構文解析して文書ノードを生成するために使用されます。
bindItem 単一の項目を XItemView オブジェクトとしてバインドするときに使用します XItemView オブジェクトは、別の式の実行結果から取得するか、 あるいは XItemFactory インスタンスを使用して構成することができます。
bindSequence 1 項目未満または 1 項目超のシーケンスをバインドするときに使用します 組み込み型から Java 型への標準マッピングに使用される Java 型のそれぞれに、 このメソッドの書式が 1 つずつあり、それぞれが所定の型の値の配列を受け入れます。
このメソッドには、XSequenceCursor を取る追加の書式があります。XSequenceCursor は、 別の式の実行結果にするか、あるいは XItemFactory インスタンスを使用して構成することができます。
以下の例は、まず最初に、各パラメーターごとに使用される値を バインドしてから、1 番目の例で作成された XSLT スタイルシートを実行したものです。 xsltResult を呼び出した結果オブジェクトが既に作成されていると仮定します。// Create an xs:date value for the "targetDate" parameter with date "April 10, 2009" XMLGregorianCalendar date = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2009, 4, 10, DatatypeConstants.FIELD_UNDEFINED); // Create a new dynamic context from the factory XDynamicContext dynamicContext = factory.newDynamicContext(); // Bind an atomic value for the "targetDate" parameter dynamicContext.bind(new QName("targetDate"), date); // Bind an atomic value for the "window" parameter dynamicContext.bind(new QName("window"), 7); // Create an XML input document String xml = "<todo-list>" + "<task due='2009-03-31' completed=''>File Quarterly Report</task>" + "<task due='2009-05-04' completed='2009-04-22'>Review candidate resumes</task> + "<task due='2009-04-16' completed=''>Order stock</task>" + "<task due='2009-05-01' completed=''>Buy concert tickets</task>" + "</todo-list>"; StreamSource source = new StreamSource(new StringReader(xml)); // Execute the stylesheet xslt.execute(source, dynamicContext, xsltResult);


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_params_xslt
ファイル名:txml_params_xslt.html