简体   繁体   中英

XSLT - wrapping xsl:template output up in a tag

I need to take in 1 .XML file, and output N files that are a sub-set of the input file. This sub-set is based on a predicate check of 2 nodes. My plan is to run the input file over a XSLT template N times to output each file.

My input looks like 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 like 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 is that my output does not contain the EmployeeData or Employees root/parent nodes.

If I change my xslt to put in the first xsl:template-match, then those tags are repeated multiple times/

If I change my match predicate to /employee_data/employees[employee/agency_code='22' or employee/offices_administered/office_administered/@agency_code='22'] then I get all employees.

It's almost like I want some magic to wrap my whole output of the match up in my two parent tags.

From what I understand from this question, you want something like 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 this transformation is applied on the 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 is 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 it is possible to create all N documents with a single run of the 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 we run this transformation on the same provided (above) XML document, two files are created:

Saxon 9.1.0.5J from Saxonica
Java version 1.6.0_31
Stylesheet compilation time: 610 milliseconds
Processing file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xml
Building tree for 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 to file:/C:/Program%20Files/Java/jre6/bin/'Agency'22.xml
Writing to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM