xml - XSLT - wrapping xsl:template output up in a tag -
i need take in 1 .xml file, , output n files sub-set of input file. sub-set based on predicate check of 2 nodes. plan run input file on xslt template n times output each file.
my input looks this:
<employee_data> <employees> <employee id="1"> <first_name>2sk8d</first_name> <agency_code>38</agency_code> <offices_administered> <office_administered office_identifier="allpois" agency_code="hl" /> </offices_administered> </employee> <employee id="2"> <first_name>2sk8d</first_name> <agency_code>24</agency_code> <offices_administered> <office_administered office_identifier="allpois" agency_code="22" /> </offices_administered> </employee> <employee id="3"> <first_name>2sk8d</first_name> <agency_code>22</agency_code> <offices_administered> <office_administered office_identifier="allpois" agency_code="hl" /> </offices_administered> </employee> </employees>
my xslt looks this
<?xml version="1.0" encoding="utf-8"?>
<xsl:output method="xml" indent="yes" encoding="utf-16"/> <xsl:template match="/employee_data/employees/employee[agency_code='22' or offices_administered/office_administered/@agency_code='22']"> <xsl:copy> <xsl:copy-of select="."/> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="@* | node()"> <xsl:message terminate="no"> catch 1 <xsl:value-of select="name()"/> </xsl:message> </xsl:template>
my problem output not contain employeedata or employees root/parent nodes.
if change xslt put in first xsl:template-match, tags repeated multiple times/
if change match predicate /employee_data/employees[employee/agency_code='22' or employee/offices_administered/office_administered/@agency_code='22'] employees.
it's want magic wrap whole output of match in 2 parent tags.
from understand question, want this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <employee_data> <employees> <xsl:apply-templates select= "/*/employees/employee [agency_code='22' or offices_administered/office_administered/@agency_code='22']"/> </employees> </employee_data> </xsl:template> <xsl:template match="employee"> <xsl:copy-of select="."/> </xsl:template> <xsl:template match="@* | node()"> <xsl:message terminate="no"> catch 1 <xsl:value-of select="name()"/> </xsl:message> </xsl:template> </xsl:stylesheet>
when transformation applied on provided xml document:
<employee_data> <employees> <employee id="1"> <first_name>2sk8d</first_name> <agency_code>38</agency_code> <offices_administered> <office_administered office_identifier="allpois" agency_code="hl" /> </offices_administered> </employee> <employee id="2"> <first_name>2sk8d</first_name> <agency_code>24</agency_code> <offices_administered> <office_administered office_identifier="allpois" agency_code="22" /> </offices_administered> </employee> <employee id="3"> <first_name>2sk8d</first_name> <agency_code>22</agency_code> <offices_administered> <office_administered office_identifier="allpois" agency_code="hl" /> </offices_administered> </employee> </employees> </employee_data>
the wanted, correct result produced:
<employee_data> <employees> <employee id="2"> <first_name>2sk8d</first_name> <agency_code>24</agency_code> <offices_administered> <office_administered office_identifier="allpois" agency_code="22"/> </offices_administered> </employee> <employee id="3"> <first_name>2sk8d</first_name> <agency_code>22</agency_code> <offices_administered> <office_administered office_identifier="allpois" agency_code="hl"/> </offices_administered> </employee> </employees> </employee_data>
ii. xslt 2.0 solution
using xslt 2.0 possible create n documents single run of transformation:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:param name="pcodes" as="xs:string+" select="'22', '38'"/> <xsl:variable name="vdoc" select="/"/> <xsl:template match="/"> <xsl:for-each select="$pcodes"> <xsl:variable name="vcode" select="."/> <xsl:result-document href="'agency'{.}.xml"> <employee_data> <employees> <xsl:apply-templates select= "$vdoc/*/employees/employee [agency_code=$vcode or offices_administered/office_administered /@agency_code=$vcode]"/> </employees> </employee_data> </xsl:result-document> </xsl:for-each> </xsl:template> <xsl:template match="employee"> <xsl:copy-of select="."/> </xsl:template> <xsl:template match="@* | node()"> <xsl:message terminate="no"> catch 1 <xsl:value-of select="name()"/> </xsl:message> </xsl:template> </xsl:stylesheet>
when run transformation on same provided (above) xml document, 2 files created:
saxon 9.1.0.5j saxonica java version 1.6.0_31 stylesheet compilation time: 610 milliseconds processing file:/c:/program%20files/java/jre6/bin/marrowtr.xml building tree file:/c:/program%20files/java/jre6/bin/marrowtr.xml using class net.sf.saxon.tinytree.tinybuilder tree built in 0 milliseconds tree size: 25 nodes, 21 characters, 9 attributes loading net.sf.saxon.event.messageemitter writing file:/c:/program%20files/java/jre6/bin/'agency'22.xml writing file:/c:/program%20files/java/jre6/bin/'agency'38.xml execution time: 94 milliseconds memory used: 11464160 namepool contents: 26 entries in 26 chains. 6 prefixes, 7 uris
Comments
Post a Comment