简体   繁体   中英

Problem with copying data from an xml file to another xml file using xslt

I am trying to copy data from file2 to file1 using xsl transformation. I am able to copy the data, but my xsd validation fails on the resulting xml file. Please help me have the data copied the right way. Here is my code: file1.xml:

<Org>
   <Security xmlns:saxon="http://saxon.sf.net" />
</Org>

file2.xml:

<Profile>
   <Policy>Policy1</Policy>
   <PolicyValue>Value1</PolicyValue> 
</Profile>

result.xml:

    <Org>
       <Security xmlns:saxon="http://saxon.sf.net">
         <Security>
            <Profile>
               <Policy>Policy1</Policy>
               <PolicyValue>Value1</PolicyValue> 
            </Profile>      
        </Security>
    </Security> 
  </Org>

Desired output:

    <Org>
    <Security xmlns:saxon="http://saxon.sf.net">
    <Profile description="SecurityProfile">
        <Policy description="SecurityProfile">Policy1</Policy>
        <PolicyValue description="SecurityProfile">Value1</PolicyValue> 
    </Profile>      
    </Security> 
   </Org>

Here is the code from my xsl file:

  <xsl:template match="*[local-name()='Org']/*[local-name()='Security']]">
  <xsl:variable name="description" select="document($lookup)/Entity/@description" />
        <xsl:copy>
            <xsl:apply-templates select="@*" />
                <xsl:copy>
                    <xsl:copy-of select="document($lookup)/Profile" />
                </xsl:copy>
            <xsl:apply-templates select="node()" />
        </xsl:copy>     
  </xsl:template>

My output file has nested Security element which is causing the validation failure. Can somebody please help me in fixing the issue. Also I need to add an attribute value recursively to all the elements that are copied. I was able to set the variable to read the attribute from lookup file. I was not able to set the attribute values to the child nodes.

Thanks for the help.

Consider walking down the tree of first xml with templates for Org and Security and in latter template, run multiple <xsl:for-each> on external nodes:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/Org">
      <xsl:copy>
         <xsl:apply-templates select="Security"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="Security">
      <xsl:copy>
         <xsl:for-each select="document('SecurityProfile2.xml')/Profile">
             <xsl:copy>
               <xsl:attribute name="description">SecurityProfile</xsl:attribute>
               <xsl:for-each select="document('file2.xml')/Profile/*">
                  <xsl:element name="{local-name()}">
                      <xsl:attribute name="description">SecurityProfile</xsl:attribute>
                      <xsl:value-of select="."/>
                  </xsl:element>                    
               </xsl:for-each>
             </xsl:copy>
         </xsl:for-each>
      </xsl:copy>
    </xsl:template> 

</xsl:stylesheet>

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