简体   繁体   English

XSLT获取同一标签内多个元素的文本值

[英]XSLT get the text value of multiple elements within the same tag

I have this piece of XML 我有这段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. 我想使用for循环在节点“ output_name”和“ output_category”内获取文本值。

I'm using the following XSL 我正在使用以下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. 我只能获得“ my_output_name”变量的正确名称。 The second variable only get the first value and it does not change with respect to the "my_output_name" variable. 第二个变量仅获得第一个值,相对于“ my_output_name”变量而言,它不变。

I know that with text() I can get only the value of the current node. 我知道使用te​​xt()只能获取当前节点的值。

Could you please tell me how I can fix this code to get both associated variables? 您能否告诉我如何解决此代码以获取两个关联变量?

thanks in advance 提前致谢

I am guessing (since you did not post the expected result) that you want to do: 我猜(因为您没有发布预期的结果),您想这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM