XSLT从xpath变量返回多个值

时间:2014-07-10 21:14:28

标签: xml xslt xpath

定义了以下变量,它们传入正确的ID,一个用于区域类代码,另一个用于扇区类代码:

 <xsl:variable name="Bloomberg_Region_Class_Code_ClassID">150050</xsl:variable>   <xsl:variable name="Bloomberg_Sector_Class_Code_ClassID">150051</xsl:variable>

我创建了2个变量,它们从xpath输出值如下:

<xsl:variable name= "Region_Class" select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Region_Class_Code_ClassID]/entity_name" />

<xsl:variable name= "Sector_Class" select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Sector_Class_Code_ClassID]/entity_name" />

然后我按如下方式连接变量以获得所需的输出:

<Class>
      <xsl:value-of select="concat($Region_Class, $Sector_Class, 1)" />
</Class>

我得到的输出正确如下:

<Class>111</Class>

然而我遇到的问题是我只回到第一组值。如何循环并输出它为每个值找到的所有值并将它们连接起来。

我尝试过以下操作:

<xsl:apply-templates select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Region_Class_Code_ClassID]" />

<xsl:apply-templates select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Sector_Class_Code_ClassID]" />


<xsl:template match="ns_ss_network_schema_entity[entity_class = $Bloomberg_Region_Class_Code_ClassID]">
    <Class>
     <xsl:value-of select="entity" />
    </Class>
  </xsl:template>

   <xsl:template match="ns_ss_network_schema_entity[entity_class = $Bloomberg_Sector_Class_Code_ClassID]">
    <Class>
      <xsl:value-of select="entity" />
    </Class>
  </xsl:template>

以上代码返回多个值,但它们显示如下:

<Class>1</Class>
<Class>1</Class>
<Class>3</Class>
<Class>2</Class>

我希望将$Bloomberg_Region_Class_Code_ClassID的每个实例与$Bloomberg_Sector_Class_Code_ClassID]连接起来,并在结尾附加一个'1'。如下:

<Class>111</Class>
<Class>321</Class>

以下是XML示例:

<?xml version="1.0" encoding="UTF-8"?><controlfiledata><filenames ctrlfilename="00000156.ctl" docfilename="00000156.pdf"/><ns_network state="select"><abbreviation>B</abbreviation><use_revisions>0</use_revisions><network_name>Bloomberg</network_name><op_delete>1</op_delete><ftp_id>2</ftp_id><include_email>false</include_email><op_add>1</op_add><op_edit>1</op_edit><network_type_id>0</network_type_id><default_schema_id>4</default_schema_id><broker_code>BRO</broker_code><doc_filename_mask>########+.+{EXT}</doc_filename_mask><network_description>Bloomberg Network</network_description><network_id>124948</network_id></ns_network><ns_publication_file_numbering state="select"><revision_unique_id>1</revision_unique_id><filenumber_unique_id>156</filenumber_unique_id><publication_id>126875</publication_id><network_id>124948</network_id></ns_publication_file_numbering><ns_network_custom_class_coll><display_info/><ns_network_custom_class state="select"><class_id>7</class_id><class_name>Wire ID</class_name><network_id>124948</network_id></ns_network_custom_class></ns_network_custom_class_coll><ns_analyst_code_coll><display_info/></ns_analyst_code_coll><ns_ss_network_entity_coll><display_info/><ns_ss_network_entity state="select"><publication_id>126875</publication_id><entity_class>7</entity_class><entity_name>4062</entity_name><network_id>124948</network_id><entity_id>124949</entity_id></ns_ss_network_entity></ns_ss_network_entity_coll><ns_ss_network_schema_entity_coll><display_info/><ns_ss_network_schema_entity state="select"><previous_entity_id>10</previous_entity_id><previous_entity_class_id>2</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>SubRegion</previous_entity_class_name><entity_class>124950</entity_class><entity_name>CE</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>CEE</previous_entity_name><entity_id>124952</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>12</previous_entity_id><previous_entity_class_id>2</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>SubRegion</previous_entity_class_name><entity_class>124950</entity_class><entity_name>US</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>US</previous_entity_name><entity_id>124956</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>13</previous_entity_id><previous_entity_class_id>2</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>SubRegion</previous_entity_class_name><entity_class>124950</entity_class><entity_name>WE</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>Western Europe</previous_entity_name><entity_id>124958</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>118</previous_entity_id><previous_entity_class_id>3</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Country</previous_entity_class_name><entity_class>124960</entity_class><entity_name>CH</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>Switzerland</previous_entity_name><entity_id>124998</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>119</previous_entity_id><previous_entity_class_id>3</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Country</previous_entity_class_name><entity_class>124960</entity_class><entity_name>TR</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>Turkey</previous_entity_name><entity_id>125000</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>120</previous_entity_id><previous_entity_class_id>3</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Country</previous_entity_class_name><entity_class>124960</entity_class><entity_name>GB</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>United Kingdom</previous_entity_name><entity_id>125002</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>121</previous_entity_id><previous_entity_class_id>3</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Country</previous_entity_class_name><entity_class>124960</entity_class><entity_name>US</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>United States</previous_entity_name><entity_id>125004</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>3001</previous_entity_id><previous_entity_class_id>7</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>SubSector</previous_entity_class_name><entity_class>125006</entity_class><entity_name>BNK</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>European Retail Banks</previous_entity_name><entity_id>125008</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>3009</previous_entity_id><previous_entity_class_id>7</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>SubSector</previous_entity_class_name><entity_class>125006</entity_class><entity_name>INS</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>European Multi-Line Insurers</previous_entity_name><entity_id>125010</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><publication_id>126875</publication_id><entity_class>125026</entity_class><entity_name>55</entity_name><schema_id>4</schema_id><network_id>124948</network_id><entity_id>125028</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>1</previous_entity_id><previous_entity_class_id>1</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Region</previous_entity_class_name><entity_class>150050</entity_class><entity_name>1</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>Europe</previous_entity_name><entity_id>128000</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>2</previous_entity_id><previous_entity_class_id>1</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Region</previous_entity_class_name><entity_class>150050</entity_class><entity_name>2</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>US</previous_entity_name><entity_id>128001</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>2000</previous_entity_id><previous_entity_class_id>6</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Sector</previous_entity_class_name><entity_class>150051</entity_class><entity_name>1</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>Banks</previous_entity_name><entity_id>128003</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>2001</previous_entity_id><previous_entity_class_id>6</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Sector</previous_entity_class_name><entity_class>150051</entity_class><entity_name>3</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>Insurance</previous_entity_name><entity_id>128005</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>1</previous_entity_id><previous_entity_class_id>1</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Region</previous_entity_class_name><entity_class>150052</entity_class><entity_name>406210</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>Europe</previous_entity_name><entity_id>128008</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>2</previous_entity_id><previous_entity_class_id>1</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Region</previous_entity_class_name><entity_class>150052</entity_class><entity_name>406211</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>US</previous_entity_name><entity_id>128009</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>2000</previous_entity_id><previous_entity_class_id>6</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Sector</previous_entity_class_name><entity_class>150053</entity_class><entity_name>406213</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>Banks</previous_entity_name><entity_id>128011</entity_id></ns_ss_network_schema_entity><ns_ss_network_schema_entity state="select"><previous_entity_id>2001</previous_entity_id><previous_entity_class_id>6</previous_entity_class_id><publication_id>126875</publication_id><previous_entity_class_name>Sector</previous_entity_class_name><entity_class>150053</entity_class><entity_name>406215</entity_name><schema_id>4</schema_id><network_id>124948</network_id><previous_entity_name>Insurance</previous_entity_name><entity_id>128013</entity_id></ns_ss_network_schema_entity></ns_ss_network_schema_entity_coll><entity_link_union_coll><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>124953</entity_link_id><entity_name>CE</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Region Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>124952</entity_id></entity_link_union><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>124957</entity_link_id><entity_name>US</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Region Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>124956</entity_id></entity_link_union><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>124959</entity_link_id><entity_name>WE</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Region Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>124958</entity_id></entity_link_union><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>124999</entity_link_id><entity_name>CH</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Country Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>124998</entity_id></entity_link_union><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>125001</entity_link_id><entity_name>TR</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Country Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>125000</entity_id></entity_link_union><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>125003</entity_link_id><entity_name>GB</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Country Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>125002</entity_id></entity_link_union><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>125005</entity_link_id><entity_name>US</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Country Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>125004</entity_id></entity_link_union><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>125009</entity_link_id><entity_name>BNK</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Industry Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>125008</entity_id></entity_link_union><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>125011</entity_link_id><entity_name>INS</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Industry Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>125010</entity_id></entity_link_union><entity_link_union state="select"><relation_entity_id>-1</relation_entity_id><schema_name>Bloomberg Schema</schema_name><entity_link_id>125029</entity_link_id><entity_name>55</entity_name><relationship>parent</relationship><entity_class_name>Bloomberg Doc Class Code</entity_class_name><relation_class_name>ROOT</relation_class_name><schema_id>4</schema_id><relation_entity_name>ROOT</relation_entity_name><entity_id>125028</entity_id></entity_link_union></entity_link_union_coll></controlfiledata>

这是我正在使用的完整xslt:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" omit-xml-declaration="no" media-type="text/xml" indent="yes" />
  <xsl:variable name="Bloomberg_Wire_ID_customClassID">7</xsl:variable>
  <xsl:variable name="Bloomberg_Country_Code_classID">124960</xsl:variable>
  <xsl:variable name="Bloomberg_Industry_Code_classID">125006</xsl:variable>
  <xsl:variable name="Bloomberg_Subject_Code_classID">125016</xsl:variable>
  <xsl:variable name="Bloomberg_Doc_Class_Code_classID">125026</xsl:variable>
  <xsl:variable name="Ticker_ClassID">5</xsl:variable>
  <xsl:variable name="Bloomberg_Region_Class_Code_ClassID">150050</xsl:variable>
  <xsl:variable name="Bloomberg_Sector_Class_Code_ClassID">150051</xsl:variable>
  <xsl:variable name="Bloomberg_Sector_Industry_Code_ClassID">150053</xsl:variable>
  <xsl:variable name="Bloomberg_Region_Industry_Code_ClassID">150052</xsl:variable>
  <xsl:variable name= "Region_Class" select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Region_Class_Code_ClassID]/entity_name" />
  <xsl:variable name= "Sector_Class" select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Sector_Class_Code_ClassID]/entity_name" />
  <xsl:variable name= "Sector_Industry" select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Sector_Industry_Code_ClassID]/entity_name" />
  <xsl:variable name= "Region_Industry" select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Region_Industry_Code_ClassID]/entity_name" />
  <xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)" />
        <xsl:value-of select="$by" />
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="substring-after($text,$replace)" />
          <xsl:with-param name="replace" select="$replace" />
          <xsl:with-param name="by" select="$by" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="strip-tags">
    <xsl:param name="text" />
    <xsl:choose>
      <xsl:when test="contains($text, '&lt;')">
        <xsl:value-of select="substring-before($text, '&lt;')" />
        <xsl:call-template name="strip-tags">
          <xsl:with-param name="text" select="substring-after($text, '&gt;')" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="/">
    <BloombergFtpContribution>
      <Version>3.1</Version>
      <Contribution>
        <Encoding>1</Encoding>
        <Action>N</Action>
        <ExpirationDate>
          <xsl:variable name="day" select="/controlfiledata/display_date_coll/display_date[date_name = 'doc_date']/day_number" />
          <xsl:variable name="month" select="/controlfiledata/display_date_coll/display_date[date_name = 'doc_date']/month_number" />
          <xsl:value-of select="/controlfiledata/display_date_coll/display_date[date_name = 'doc_date']/year_number + 3" />
          <xsl:value-of select="format-number($month, &quot;00&quot;)" />
          <xsl:value-of select="format-number($day, &quot;00&quot;)" />
        </ExpirationDate>
        <ResearchId>
          <xsl:value-of select="format-number(/controlfiledata/ns_publication_file_numbering/filenumber_unique_id, &quot;00000000&quot;)" />
        </ResearchId>
        <Wire>
          <xsl:value-of select="/controlfiledata/ns_ss_network_entity_coll/ns_ss_network_entity[entity_class = $Bloomberg_Wire_ID_customClassID]/entity_name" />
        </Wire>

         <Industry>406218</Industry>

        <xsl:apply-templates select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Region_Class_Code_ClassID]" />
        <xsl:apply-templates select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Region_Industry_Code_ClassID]" />
        <xsl:apply-templates select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Sector_Industry_Code_ClassID]" />
        <xsl:apply-templates select="/controlfiledata/ns_ss_company_ticker_code_coll/ns_ss_company_ticker_code[symbol_set_id = $Ticker_ClassID]" />
        <xsl:apply-templates select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Country_Code_classID]" />
    <Attachment>        
      <AttachFile type="Research">
          <AttachFileName><xsl:value-of select="/controlfiledata/serializable/ss_filename" /></AttachFileName>
          <AttachFileSize>1</AttachFileSize>
          </AttachFile>
          <AttachTitle><xsl:value-of select="/controlfiledata/serializable/doc_title" /></AttachTitle>
        </Attachment>
    <Headline>
          <xsl:value-of select="/controlfiledata/serializable/doc_title" />
        </Headline>
        <NewsText>
          <xsl:variable name="myVar">
            <xsl:call-template name="strip-tags">
              <xsl:with-param name="text" select="/controlfiledata/serializable/doc_text" />
            </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="myVar1">
            <xsl:call-template name="string-replace-all">
              <xsl:with-param name="text" select="$myVar" />
              <xsl:with-param name="replace" select="'&amp;'" />
              <xsl:with-param name="by" select="''" />
            </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="myVar2">
            <xsl:call-template name="string-replace-all">
              <xsl:with-param name="text" select="$myVar1" />
              <xsl:with-param name="replace" select="'nbsp;'" />
              <xsl:with-param name="by" select="''" />
            </xsl:call-template>
          </xsl:variable>
          <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="$myVar2" />
            <xsl:with-param name="replace" select="'amp;'" />
            <xsl:with-param name="by" select="'&amp;'" />
          </xsl:call-template>
        </NewsText>
      </Contribution>
    </BloombergFtpContribution>
    <xsl:value-of select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = 36]/entity_name" />
  </xsl:template>
  <xsl:template match="ns_ss_company_ticker_code[symbol_set_id = $Ticker_ClassID]">
    <Ticker>
      <xsl:apply-templates select="code" />
    </Ticker>
  </xsl:template>
<xsl:template match="ns_ss_network_schema_entity[entity_class = $Bloomberg_Country_Code_classID]">
    <Country>
      <xsl:value-of select="entity_name" />
    </Country>
  </xsl:template>
        <xsl:template match="ns_ss_network_schema_entity[entity_class = $Bloomberg_Region_Class_Code_ClassID]">
    <xsl:variable name="i" select="position()" />
    <Classss>
        <xsl:value-of select="entity"/>
        <xsl:value-of select="/controlfiledata/ns_ss_network_schema_entity_coll/ns_ss_network_schema_entity[entity_class = $Bloomberg_Sector_Class_Code_ClassID][$i]/entity"/>
        <xsl:text>1</xsl:text>
    </Classss>
  </xsl:template>
 <xsl:template match="ns_ss_network_schema_entity[entity_class = $Bloomberg_Region_Industry_Code_ClassID]">
    <Industry>
      <xsl:value-of select="entity_name" />
    </Industry>
  </xsl:template>
   <xsl:template match="ns_ss_network_schema_entity[entity_class = $Bloomberg_Sector_Industry_Code_ClassID]">
    <Industry>
      <xsl:value-of select="entity_name" />
    </Industry>
  </xsl:template>
  <xsl:template match="code">
    <xsl:variable name="code" select="." />
    <xsl:variable name="tickerCode">
      <xsl:call-template name="replaceCharsInString">
        <xsl:with-param name="stringIn" select="string($code)" />
        <xsl:with-param name="charsIn" select="'.'" />
        <xsl:with-param name="charsOut" select="'@'" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$tickerCode" />
  </xsl:template>
  <xsl:template name="replaceCharsInString">
    <xsl:param name="stringIn" />
    <xsl:param name="charsIn" />
    <xsl:param name="charsOut" />
    <xsl:choose>
      <xsl:when test="contains($stringIn,$charsIn)">
        <xsl:value-of select="concat(substring-before($stringIn,$charsIn),$charsOut)" />
        <xsl:call-template name="replaceCharsInString">
          <xsl:with-param name="stringIn" select="substring-after($stringIn,$charsIn)" />
          <xsl:with-param name="charsIn" select="$charsIn" />
          <xsl:with-param name="charsOut" select="$charsOut" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$stringIn" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

这是我正在使用的完整XSLT:

我是xslt和xpath的新手,非常感谢任何反馈。

1 个答案:

答案 0 :(得分:0)

这是一个简单的样式表。我使用而不是谓词来选择ns_ss_network_schema_entity元素 - 我相信它既简单又高效。否则它和以前一样,除了我更正了:

<xsl:value-of select="entity"/>

(从你这里复制!)到:

<xsl:value-of select="entity_name"/>

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:variable name="Bloomberg_Region_Class_Code_ClassID">150050</xsl:variable>
<xsl:variable name="Bloomberg_Sector_Class_Code_ClassID">150051</xsl:variable>

<xsl:key name="schema-by-class" match="ns_ss_network_schema_entity" use="entity_class"/>

<xsl:template match="/">
    <output>
        <xsl:apply-templates select="key('schema-by-class', $Bloomberg_Region_Class_Code_ClassID)"/>
    </output>
</xsl:template>

<xsl:template match="ns_ss_network_schema_entity">
    <xsl:variable name="i" select="position()" />
    <Class>
        <xsl:value-of select="entity_name"/>
        <xsl:value-of select="key('schema-by-class', $Bloomberg_Sector_Class_Code_ClassID)[$i]/entity_name"/>
        <xsl:text>1</xsl:text>
    </Class>
</xsl:template>

</xsl:stylesheet>

应用于您的输入,结果是:

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <Class>111</Class>
  <Class>231</Class>
</output>