在另一个元素之后立即添加 XML 元素。

huangapple go评论82阅读模式
英文:

Add xml element immediately after another element

问题

我有这个XML

<request_scope>
   <valuation_currency>GBP</valuation_currency>
   <fund_code_type_required>FUND1</fund_code_type_required>
   <intermediary></intermediary>
   <valuation_request type="Current"/>
   <valuation_request type="Surrender"/>
   <fund_breakdown_request>yes</fund_breakdown_request>
</request_scope>
英文:

I have this xml

  <request_scope>
     <valuation_currency>GBP</valuation_currency>
     <fund_code_type_required>FUND1</fund_code_type_required>
     <valuation_request type="Current"/>
     <valuation_request type="Surrender"/>
     <fund_breakdown_request>yes</fund_breakdown_request>
  </request_scope>

and I want an xslt that adds element intermediary immediately after fund_code_type_required
only if intermediary does not exist. So to be

   <request_scope>
      <valuation_currency>GBP</valuation_currency>
      <fund_code_type_required>FUND1</fund_code_type_required>
      <intermediary></intermediary>
      <valuation_request type="Current"/>
      <valuation_request type="Surrender"/>
      <fund_breakdown_request>yes</fund_breakdown_request>
   </request_scope>

答案1

得分: 1

只匹配fund_code_type_required,当它是request_scope的子元素且没有兄弟元素intermediary时。输出fund_code_type_required的副本,并在之后添加新元素...

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" expand-text="yes">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:mode on-no-match="shallow-copy"/>
    
    <xsl:template match="request_scope[not(intermediary)]/fund_code_type_required">
        <xsl:copy-of select="."/>
        <intermediary/>
    </xsl:template>
    
</xsl:stylesheet>

Fiddle: http://xsltfiddle.liberty-development.net/94humqD

英文:

Just match fund_code_type_required when it's a child of request_scope and doesn't have a sibling intermediary. Output a copy of fund_code_type_required and add the new element after...

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" expand-text="yes">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:mode on-no-match="shallow-copy"/>
    
    <xsl:template match="request_scope[not(intermediary)]/fund_code_type_required">
        <xsl:copy-of select="."/>
        <intermediary/>
    </xsl:template>
    
</xsl:stylesheet>

Fiddle: http://xsltfiddle.liberty-development.net/94humqD

huangapple
  • 本文由 发表于 2023年6月12日 23:31:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定