2017-04-12 10 views
0

私はノード「output_name」と内のテキスト値を取得したいと思いXMLXSLTは

<output_list> 
      <output_name>name_F</output_name> 
      <output_category>Ferrari</output_category> 
      <output_name>name_P</output_name> 
      <output_category>Porsche</output_category> 
      <output_name>name_L</output_name> 
      <output_category>Lamborghini</output_category> 
</output_list> 

のこの作品を持っている「output_category」FOR-を使用して、同じタグ内に複数の要素のテキスト値を取得しますループ。

私は「my_output_name」変数にのみ正しい名前を取得することができるよ、次のXSL

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xlink="http://www.w3.org/1999/xlink"> 
    <xsl:output method="xml" indent="yes" encoding="utf-8" /> 

<xsl:template match="/" > 
<xmlns:swe="http://www.opengis.net/swe/2.0" 
xmlns:sml="http://www.opengis.net/sensorml/2.0"> 
     <sml:OutputList>  
     <xsl:for-each select="//output_list/output_name"> 
     <xsl:variable name="my_output_name" select="text()"/> 
     <xsl:variable name="my_output_category" select="//output_list/output_category"/> 
     <sml:output name="{$my_output_name}"> 
     <swe:Category definition="{$my_output_category}"> 
     </swe:Category> 
     </sml:output> 
     </xsl:for-each> 
     </sml:OutputList> 

</xsl:stylesheet> 

を使用しています。 2番目の変数は最初の値のみを取得し、 "my_output_name"変数に関しては変化しません。

私はtext()で現在のノードの値だけを得ることができることを知っています。

このコードを修正して両方の関連変数を取得する方法を教えてください。事前に

おかげで(あなたが期待される結果を投稿していなかったので)私はあなたがやりたいこと推測してい

+0

[指定されたノードの後に​​兄弟ノードを検索する]が見つかりました](http://stackoverflow.com/questions/10055269/find-sibling-node-after-specified-node-is-found) – ceving

+0

また:http://stackoverflow.com/questions/11657223/xpathget -following-sibling – ceving

+0

またはhttp://stackoverflow.com/questions/3139402/how-to-select-following-sibling-xml-tag-using-xpath – ceving

答えて

2

XSLT 1.0

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

<xsl:template match="/output_list" > 
    <sml:OutputList xmlns:sml="http://www.opengis.net/sensorml/2.0" xmlns:swe="http://www.opengis.net/swe/2.0">  
     <xsl:for-each select="output_name"> 
      <sml:output name="{.}"> 
       <swe:Category definition="{following-sibling::output_category[1]}"/> 
      </sml:output> 
     </xsl:for-each> 
    </sml:OutputList> 
</xsl:template> 

</xsl:stylesheet> 

を取得する:

<?xml version="1.0" encoding="UTF-8"?> 
<sml:OutputList xmlns:sml="http://www.opengis.net/sensorml/2.0" xmlns:swe="http://www.opengis.net/swe/2.0"> 
    <sml:output name="name_F"> 
    <swe:Category definition="Ferrari"/> 
    </sml:output> 
    <sml:output name="name_P"> 
    <swe:Category definition="Porsche"/> 
    </sml:output> 
    <sml:output name="name_L"> 
    <swe:Category definition="Lamborghini"/> 
    </sml:output> 
</sml:OutputList> 
+0

はい、ありがとう@ michael.hor257k –