简体   繁体   中英

How to apply multiple imports in XSLT for xml generation

My xml only picks the last import(IdentifierXSLT) and not the above ones; and i want to import all the others.Can i use apply Template in all xslts that are needed to be imported? How can i achieve that? Any help would be appreciated.

My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <xsl:output method="xml" indent="yes"/>
        <xsl:import href="DocumentHeaderXSLT.xsl"/>
            <xsl:import href="IdentifierXSLT.xsl"/>
        <xsl:template match="/">
            <xsl:element name="ApplicationBatch">
                <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>
                <xsl:namespace name="xsd" select="'http://www.w3.org/2001/XMLSchema'"/>
                <xsl:apply-imports/>
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>

My Xml:

<?xml version="1.0" encoding="UTF-8"?>
<ApplicationBatch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <DocumentHeader>
      <SchemaName xsi:type="xsd:string">AcmeCorp_MotorApplicationRequest</SchemaName>
      <SchemaVersion xsi:type="xsd:string">v1_0</SchemaVersion>
   </DocumentHeader>
   <Identifier Type="LenderAssigned" UniqueID="ACMECORP"/>
   <Submission>
      <Date>2017-03-07</Date>
      <Time>09: 07: 39.1373551+11: 00</Time>
   </Submission>
</ApplicationBatch>

DocumentHeaderXSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:element name="DocumentHeader">
            <xsl:element name="SchemaName" >
                <xsl:attribute name="xsi:type">xsd:string</xsl:attribute>
                <xsl:value-of select="/ApplicationBatch/DocumentHeader/SchemaName"/>
            </xsl:element>
            <xsl:element name="SchemaVersion">
                <xsl:attribute name="xsi:type">xsd:string</xsl:attribute>
                <xsl:value-of select="/ApplicationBatch/DocumentHeader/SchemaVersion"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

IdentifierXSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:element name="Identifier">
            <xsl:attribute name="Type">
                <xsl:value-of select="/ApplicationBatch/Identifier/@Type"/>
            </xsl:attribute>
            <xsl:attribute name="UniqueID">
                <xsl:value-of select="/ApplicationBatch/Identifier/@UniqueID"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Expected output:

<ApplicationBatch ProductionData="Yes"
                  xmlns:xsd="htt://www.w3.org/2001/XMLSchema"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <DocumentHeader>
        <SchemaName xsi:type="xsd:string">AcmeCorp_MotorApplicationRequest</SchemaName>
        <SchemaVersion xsi:type="xsd:string">v1_0</SchemaVersion>
    </DocumentHeader>
    <Identifier Type="BrokerAssigned"
                UniqueID="Ref24723"/>
</ApplicationBatch>

My output:

<ApplicationBatch ProductionData="Yes"
                  xmlns:xsd="htt://www.w3.org/2001/XMLSchema"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Identifier Type="BrokerAssigned"
                UniqueID="Ref24723"/>
</ApplicationBatch>

When you use xsl:apply-templates , all the matching template rules are considered, and the one with highest import precedence wins. If you want more than one template rule to be executed for the same node, there are various options: you can call apply-templates twice in different modes , or you could have one template rule invoke the other using xsl:apply-imports or xsl:next-match .

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