0

I have this piece of XML

<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>

I would like to get the text value within the node "output_name" and "output_category" using a for-loop.

I'm using the following 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>

I'm able to get only the correct name for "my_output_name" variable. The second variable only get the first value and it does not change with respect to the "my_output_name" variable.

I know that with text() I can get only the value of the current node.

Could you please tell me how I can fix this code to get both associated variables?

thanks in advance

sylar_80
  • 251
  • 3
  • 18
  • Possible duplicate of [Find sibling node after specified node is found](http://stackoverflow.com/questions/10055269/find-sibling-node-after-specified-node-is-found) – ceving Apr 12 '17 at 10:58
  • Also: http://stackoverflow.com/questions/11657223/xpath-get-following-sibling – ceving Apr 12 '17 at 10:59
  • Or: http://stackoverflow.com/questions/3139402/how-to-select-following-sibling-xml-tag-using-xpath – ceving Apr 12 '17 at 11:00
  • @ceving thanks for the link. But is there any chance to get the same result without using xpath? – sylar_80 Apr 12 '17 at 12:52
  • You are using already XPath. The argument to `select` is XPath. – ceving Apr 12 '17 at 13:04
  • The stylesheet you show us is not well-formed. The only result you can expect is an error message. – michael.hor257k Apr 12 '17 at 14:09

1 Answers1

2

I am guessing (since you did not post the expected result) that you want to do:

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>

to get:

<?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>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51