2017-04-05 12 views
0

XMLtoJSON変換のためにリスト要素を識別するのに役立つ処理命令を挿入して、さまざまなXML応答のXSL変換を行う必要があります。xpathsを含むXSLTテンプレートの一致パラメータ

例入力XML:

<?xml version="1.0" encoding="UTF-8"?> 
<recipe_collection> 
    <last_updated>20170405</last_updated> 
    <recipe> 
     <name>Split Pea Soup</name> 
     <ingredients_list> 
     <ingredient> 
      <name>Split Peas</name> 
      <amount>1 bag</amount> 
     </ingredient> 
     <ingredient> 
      <name>Vegetable Broth</name> 
      <amount>32 Ounces</amount> 
     </ingredient> 
     <ingredient> 
      <name>Vegetable Broth</name> 
      <amount>32 Ounces</amount> 
     </ingredient> 
     <ingredient> 
      <name>Ham</name> 
      <amount>Small</amount> 
     </ingredient> 
     </ingredients_list> 
     <preparation> 
     <step>Rinse Peas</step> 
     <step>Add ingredients to pressure cooker</step> 
     <step>Cook at full pressure for 12 minutes</step> 
     <step>Season with salt, pepper, garlic powder to taste</step> 
     </preparation> 
     <serve_with> 
     <name>Bread</name> 
     </serve_with> 
    </recipe> 
</recipe_collection> 

例のXSL:ターンのコピーに<?xml-muliple ...?>のPIと元のXMLを挿入

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

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

    <xsl:template match="/recipe_collection/recipe/name[1]|/recipe_collection/recipe/ingredients_list/ingredient[1]|/recipe_collection/recipe/ingredients_list/ingredient[1]|/recipe_collection/recipe/preparation/step[1]|/recipe_collection/recipe/serve_with/name[1]"> 
     <xsl:processing-instruction name="xml-multiple"> 
     <xsl:value-of select="local-name()" /> 
     </xsl:processing-instruction> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

<?xml version="1.0" encoding="UTF-8"?> 
<recipe_collection> 
    <last_updated>20170405</last_updated> 
    <recipe> 
     <?xml-multiple name?> 
     <name>Split Pea Soup</name> 
     <ingredients_list> 
     <?xml-multiple ingredient?> 
     <ingredient> 
      <name>Split Peas</name> 
      <amount>1 bag</amount> 
     </ingredient> 
     <ingredient> 
      <name>Vegetable Broth</name> 
      <amount>32 Ounces</amount> 
     </ingredient> 
     <ingredient> 
      <name>Vegetable Broth</name> 
      <amount>32 Ounces</amount> 
     </ingredient> 
     <ingredient> 
      <name>Ham</name> 
      <amount>Small</amount> 
     </ingredient> 
     </ingredients_list> 
     <preparation> 
     <?xml-multiple step?> 
     <step>Rinse Peas</step> 
     <step>Add ingredients to pressure cooker</step> 
     <step>Cook at full pressure for 12 minutes</step> 
     <step>Season with salt, pepper, garlic powder to taste</step> 
     </preparation> 
     <serve_with> 
     <?xml-multiple name?> 
     <name>Bread</name> 
     </serve_with> 
    </recipe> 
</recipe_collection> 

これまでのところは良いです。これで、このXSLをさまざまなXMLスキーマで動作させることができます。私は、マッチで使用されたパスを含むパラメータを渡したいと思います。変更されたスタイルシートで

、私はデフォルトのパスのリストをパラメータ「のXPath」を定義しています(注、このPARAMの値は、実際に実行時にXSLに渡されます):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:param name="xpaths">/recipe_collection/recipe/name[1]|/recipe_collection/recipe/ingredients_list/ingredient[1]|/recipe_collection/recipe/ingredients_list/ingredient[1]|/recipe_collection/recipe/preparation/step[1]|/recipe_collection/recipe/serve_with/name[1]</xsl:param> 

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

    <xsl:template match="$xpaths"> 
     <xsl:processing-instruction name="xml-multiple"> 
     <xsl:value-of select="local-name()" /> 
     </xsl:processing-instruction> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

注意すべきしかし新しいXSLでmatch="$xpaths"が無効である

他のもの:

私が使用しているXSLパーサーがXSL 2.0することができます。

XSLは、要素が一意の名前を持たないスキーマを含む任意のスキーマで機能する必要があるため、完全なxpathを使用してリストを指定する必要があります。

最後に私はXSL newbであることを謝罪します。私はまだ変容の概念のいくつかの周りに私の脳を得ることができません。

正しい方向(または解決策)のポインタをありがとう。

答えて

0

Saxon 9.7 EEやPEなどのXSLT 3.0プロセッサでは、静的変数とシャドウ属性を使用することができます。純粋なXSLT 2.0で

<xsl:param name="xpaths" static="yes" as="xs:string">/recipe_collection/recipe/name[1]|/recipe_collection/recipe/ingredients_list/ingredient[1]|/recipe_collection/recipe/ingredients_list/ingredient[1]|/recipe_collection/recipe/preparation/step[1]|/recipe_collection/recipe/serve_with/name[1]</xsl:param> 

    <xsl:template _match="{$xpaths}"> 
     <xsl:processing-instruction name="xml-multiple"> 
     <xsl:value-of select="local-name()" /> 
     </xsl:processing-instruction> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

あなたは、出力/挿入match属性値と所望の第二のスタイルシートを生成するための引数として、あなたの文字列のパス(複数可)を取って1つのスタイルシートを使用する必要があります。

+0

ありがとうございました。これはXML 3.0の評価者でうまくいきます。残念ながら、私が使用しているツールAxwayは、XSLT 2.0以下に制限されているだけでなく、生成されたスタイルシートを参照することもできません。 おそらく2.0でそれを行う複雑な方法があると思われます。私の他の選択肢は、AxwayのXSLTプロセッサを使用せず、あなたが言及したSaxonをフックすることです。 – user7822580

関連する問題