2017-04-12 44 views
0

我有这一块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_name中”并在文本值“output_category”使用换循环。

我用下面的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> 

我能为“my_output_name”变量只能得到正确的名称。第二个变量只获得第一个值,并且它不会相对于“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/xpath-get -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 –