xml - If "match" else -
in xslt match nodes using following command
<xsl:template match="*[local-name() = 'proposal']/*[local-name() = 'applicationdata']">
which nodes from
<?xml version="1.0" encoding="utf-8"?><?xfa generator="xfa2_4" apiversion="2.8.9029.0"?> <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timestamp="2013-03-01t09:48:58z" uuid="3e3468da-104d-4532-8077-0dc001ca166b"> <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"> <xfa:data> <proposal xmlns="http://www.govtalk.gov.uk/planning/oneappproposal-2006" version=""> <oneapp:applicationdata xmlns:oneapp="http://www.govtalk.gov.uk/planning/oneappproposal-2006"> <oneapp:treeshedgeswales/> <oneapp:otherlowcarbonenergy/> </oneapp:applicationdata> </proposal> ...
matching
<oneapp:applicationdata xmlns:oneapp="http://www.govtalk.gov.uk/planning/oneappproposal-2006"> <oneapp:treeshedgeswales/> <oneapp:otherlowcarbonenergy/> </oneapp:applicationdata>
how determine whether applicationdata
existed, , if not, insert it?
you need add logic template matched 'proposal' element
<xsl:template match="*[local-name() = 'proposal']">
then, write xsl:if
statement, so:
<xsl:if test="not(*[local-name() = 'applicationdata'])"> <oneapp:applicationdata xmlns:oneapp="http://www.govtalk.gov.uk/planning/oneappproposal-2006"> <oneapp:treeshedgeswales/> <oneapp:otherlowcarbonenergy/> </oneapp:applicationdata> </xsl:if>
you need wrap in xsl:copy
if wanted retain proposal
element.
if didn't have existing template matching proposal
in xslt, add test template match itself
<xsl:template match="*[local-name() = 'proposal'][not(*[local-name() = 'applicationdata'])]"> <xsl:copy> <oneapp:applicationdata xmlns:oneapp="http://www.govtalk.gov.uk/planning/oneappproposal-2006"> <oneapp:treeshedgeswales/> <oneapp:otherlowcarbonenergy/> </oneapp:applicationdata> </xsl:copy> </xsl:template>
as michael.hor257k mentioned in comments, cleaner if declared namespaces in xslt, , used namespace prefixes in matching....
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0" xmlns:oneapp="http://www.govtalk.gov.uk/planning/oneappproposal-2006"> <xsl:template match="oneapp:proposal[not(oneapp:applicationdata)]"> <xsl:copy> <oneapp:applicationdata> <oneapp:treeshedgeswales/> <oneapp:otherlowcarbonenergy/> </oneapp:applicationdata> </xsl:copy> </xsl:template>
Comments
Post a Comment