循环通过文本元素和替换字符串

时间:2011-04-19 21:00:19

标签: xml xslt recursion

我想在AgreementTexts中用变量(括号中)替换XML中相同命名元素(当然没有括号)的值。

有人可以帮我怎么做吗? 谢谢! 亚当

这是我的XML:

<MailBody>
  <MSG_BOComment_PDF type="string" /> 
  <HSH_Is_Migrated type="string">NO</HSH_Is_Migrated> 
  <PDFC_X_Cust type="string">7990122189</PDFC_X_Cust> 
  <PDFC_X_FOBO_Id type="string">68281 / 41080</PDFC_X_FOBO_Id> 
  <PDFC_RPDealTradTime type="dateTime">06/04/2010 11:32:00</PDFC_RPDealTradTime> 
  <MSG_ConfTypeDE type="string">DEUTSCH</MSG_ConfTypeDE> 
  <MSG_ConfTypeEN type="string">ENGLISH</MSG_ConfTypeEN> 
  <Msg_SpecAgreeTxt2>
  <LINE_ON>
  <AgreementText type="string">The Buyer agrees that [MSG_ConfTypeDE], for the purpose of paragraph 8(a) of the Agreement, bonds that, in the determination of the Buyer (acting reasonably), meet each of the following criteria, shall be eligible as New Purchased Securities:</AgreementText> 
  </LINE_ON>
  <LINE_ON>
  <AgreementText type="string">(1) EITHER: Test+</AgreementText> 
  </LINE_ON>
  <LINE_ON>
  <AgreementText type="string">(A) The bonds [PDFC_X_FOBO_Id] have the of a grandfathered guarantee (Gew?tr?rhaftung) from, or are issued by, the Federal Re-public of Germany or one of its constituent States (L?er) and are rated by at least two of Fitch Ratings Inc, Moody? Investors Service, Inc</AgreementText> 
  </LINE_ON>
  <LINE_ON>
  <AgreementText type="string">OR+</AgreementText> 
  </LINE_ON>
  <LINE_ON>
  <AgreementText type="string">(B) The bonds are issued by LFAF??rbank Bayern and are guaranteed by the State of Bavaria and are rated at least one Rating Agency and</AgreementText> 
  </LINE_ON>
  <LINE_ON>
  <AgreementText type="string">(2) The bonds are [PDFC_X_FOBO_Id] denominated in Euro.</AgreementText> 
  </LINE_ON>
  </Msg_SpecAgreeTxt2>
  <MSG_EntC_EmailTo type="string">mmfx.backoffice@hsh-nordbank.com</MSG_EntC_EmailTo> 
  <MSG_C_Address1 type="string" /> 
  <MSG_C_Address2 type="string">ML Financial Centre</MSG_C_Address2> 
  <MSG_C_Address3 type="string">2 King Edward Street</MSG_C_Address3> 
  <MSG_C_Address4 type="string">LONDON EC1A 1HQ|GROSSBRITANNIEN</MSG_C_Address4> 
  <MSG_C_ContactName type="string">Merrill Lynch International</MSG_C_ContactName> 
  <MSG_EntC_Address1 type="string">Transaction Services</MSG_EntC_Address1> 
  <MSG_EntC_FaxAddress type="string">+49 431 900 34082</MSG_EntC_FaxAddress> 
  <MSG_EntC_PhoneNumber type="string">+49 431 900 11428</MSG_EntC_PhoneNumber> 
  <MSG_EntC_Address2 type="string">OE 3651 Geld- und Devisenabwicklung</MSG_EntC_Address2> 
  <MSG_EntC_Address3 type="string">Martensdamm 6</MSG_EntC_Address3> 
  <MSG_EntC_Address4 type="string">24103 Kiel</MSG_EntC_Address4> 
  <MSG_EntC_ContactName type="string">HSH Nordbank AG</MSG_EntC_ContactName> 
  <EVT_EventDate type="date">06/04/2010</EVT_EventDate> 
  <MSG_ConfTypeEN type="string" /> 
  <MUR type="string">KTPP86425</MUR> 
  <MailHeader_Id type="int">86425</MailHeader_Id> 
</MailBody>

在实施Flynn'S解决方案后,我得到了这个输出。但这不完全是我想要的。 我从XML开始以相同的顺序获取元素的AgreementText值之前和之后...

  

NO 7990122189 68281/41080 06/04/2010   11:32:00 DEUTSCH ENGLISH买家   同意DEUTSCH,为此目的   “协定”第8(a)段,   债券,在决定   买方(合理行事),见面   应符合以下各项标准   有资格成为新购买者   证券:

     

(1)EITHER:Test +

     

(A)债券68281/41080具有   保证金的保证金   (Gew?tr?rhaftung)来自或发行   通过,联邦重新公布德国   或其中一个组成国   (L?er)并且被评为至少两个   惠誉评级公司,穆迪?投资者   Service,Inc

     

OR +

     

(B)债券由发行   LFAF ?? rbank拜仁并保证   由巴伐利亚州评定   至少有一个评级机构和

     

(2)债券为68281/41080   以欧元计价。

     

mmfx.backoffice@hsh-nordbank.com ML   金融中心2 King Edward Street   伦敦EC1A 1HQ | GROSSBRITANNIEN   美林国际   交易服务+49 431 900 34082   +49 431 900 11428 OE 3651 Geld- und Devisenabwicklung Martensdamm 6 24103   基尔HSH Nordbank AG 06/04/2010   KTPP86425 86425

这是我的预期输出:只有协议文本中的[Variables.in.brackets]应该用XML中相同名称元素的值替换。

  

买方同意DEUTSCH,为   第8(a)段的目的   协议,债券,在   确定买方(代理   合理地),满足每一个   以下标准,应符合资格   作为新购买的证券:

     

(1)EITHER:Test +

     

(A)债券68281/41080具有   保证金的保证金   (Gew?tr?rhaftung)来自或发行   通过,联邦重新公布德国   或其中一个组成国   (L?er)并且被评为至少两个   惠誉评级公司,穆迪?投资者   Service,Inc

     

OR +

     

(B)债券由发行   LFAF ?? rbank拜仁并保证   由巴伐利亚州评定   至少有一个评级机构和

     

(2)债券为68281/41080   以欧元计价。

这是我的xsl文件:

  <?xml version="1.0" encoding="ISO-8859-1" ?> 
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java" xmlns:date="http://exslt.org/dates-and-times">
  <xsl:key name="vars" match="*" use="name()" /> 
  <xsl:template match="/">
  <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
  <fo:simple-page-master master-name="single_page" page-height="297mm" page-width="210mm" margin-left="25mm" margin-bottom="10mm" margin-right="13mm" margin-top="7mm">
  <fo:region-body region-name="xsl-region-body" /> 
  <fo:region-after region-name="page-numbering" extent="15mm" /> 
  </fo:simple-page-master>
  <fo:page-sequence-master master-name="repeatable_master">
  <fo:repeatable-page-master-reference master-reference="single_page" maximum-repeats="10" /> 
  </fo:page-sequence-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="repeatable_master">
  <fo:flow flow-name="xsl-region-body">
  <xsl:call-template name="MailBody" /> 
  </fo:flow>
  </fo:page-sequence>
  </fo:root>
  </xsl:template>
  <xsl:template name="MailBody">
  <fo:table font-size="10.00pt" width="170.00mm">
  <fo:table-column column-width="170mm" /> 
  <fo:table-body>
  <fo:table-row height="1mm">
  <fo:table-cell>
  <fo:block>
  <xsl:apply-templates select="@* | node()" /> 
  </fo:block>
  </fo:table-cell>
  </fo:table-row>
  </fo:table-body>
  </fo:table>
  </xsl:template>
  <xsl:template match="Msg_SpecAgreeTxt2/LINE_ON/AgreementText/text()" name="replace">
  <xsl:param name="text" select="." /> 
  <xsl:choose>
  <xsl:when test="contains($text,'[')">
  <xsl:variable name="varname" select="substring-before(substring-after($text,'['),']')" /> 
  <xsl:value-of select="substring-before($text,'[')" /> 
  <xsl:value-of select="key('vars',$varname)" /> 
  <xsl:call-template name="replace">
  <xsl:with-param name="text" select="substring-after($text,']')" /> 
  </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
  <xsl:value-of select="$text" /> 
  <fo:block space-after="10px" /> 
  </xsl:otherwise>
  </xsl:choose>
  </xsl:template>
  </xsl:stylesheet>

很抱歉多次编辑,错误的使用,因为我在这里完全是新手;))

1 个答案:

答案 0 :(得分:0)

试试这个模板:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="vars" match="*" use="name()" />

  <xsl:template match="Msg_SpecAgreeTxt2/LINE_ON/AgreementText/text()" name="replace">
    <xsl:param name="text" select="." />
    <xsl:choose>
      <xsl:when test="contains($text,'[')">
        <xsl:variable name="varname" select="substring-before(substring-after($text,'['),']')" />
        <xsl:value-of select="substring-before($text,'[')" />
        <xsl:value-of select="key('vars',$varname)" />
        <xsl:call-template name="replace">
          <xsl:with-param name="text" select="substring-after($text,']')" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

虽然顶部的密钥效率不高,但是如果不知道它们的祖先,就没有好办法找到一种有效的方法来匹配包含要替换的值的节点。