首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >循环遍历文本元素并替换字符串

循环遍历文本元素并替换字符串
EN

Stack Overflow用户
提问于 2011-04-19 21:00:18
回答 1查看 125关注 0票数 0

我希望将AgreementTexts中的变量(括号中)替换为XML中相同命名元素的值(当然没有括号)。

有人能帮我做这件事吗?谢谢!亚当

这是我的XML:

代码语言:javascript
运行
复制
<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的解决方案之后,我得到了这个输出。但这不是我想要的。我得到前后元素的AgreementText值以相同的顺序从XML.

NO . 7990122189 68281 / 41080 06/04/201011:32:00 DEUTSCH ENGLISH买方同意,为本协议第8(a)段的目的,在确定买方(合理行事)时符合下列各项标准的债券应符合新购买证券的资格:

(1)任何一种: Test+

(A) 68281 / 41080的债券由德国联邦再公开公司或其一个成员国(L?呃)提供或由至少两家惠誉评级公司穆迪评级(Gew?tr?rhaftung)担保?投资者服务公司

OR+

(B)债券由拜仁皇家银行发行,由巴伐利亚州担保,评级至少为一个评级机构;

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

+49 431 900 34082 +49 431 900 34082+49 431 900 11428 OE 3651 Geld- und Devisenabwicklung 6 24103 Kiel HSH Nordbank AG 06/04/2010 KTPP86425 86425

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

买方同意,为本协议第8(a)段的目的,在买方确定(合理行事)时符合下列各项标准的债券应符合新购买证券的资格:

(1)任何一种: Test+

(A) 68281 / 41080的债券由德国联邦再公开公司或其一个成员国(L?呃)提供或由至少两家惠誉评级公司穆迪评级(Gew?tr?rhaftung)担保?投资者服务公司

OR+

(B)债券由拜仁皇家银行发行,由巴伐利亚州担保,评级至少为一个评级机构;

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

这是我的xsl文件:

代码语言:javascript
运行
复制
  <?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>

对不起,我的编辑多了,用法不对,因为我是这里的新手;)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-27 15:45:35

尝试以下模板:

代码语言:javascript
运行
复制
<?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>

但是,顶部的键并不是很有效,但是如果不知道它们的祖先,就没有一种很好的方法来导出匹配包含要替换的值的节点的有效方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5722630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档