英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论