簡體   English   中英

XSLT獲取同一標簽內多個元素的文本值

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

我有這段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>

我想使用for循環在節點“ 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”變量而言,它不變。

我知道使用te​​xt()只能獲取當前節點的值。

您能否告訴我如何解決此代碼以獲取兩個關聯變量?

提前致謝

我猜(因為您沒有發布預期的結果),您想這樣做:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM