简体   繁体   English

使用xslt将数据从xml文件复制到另一个xml文件的问题

[英]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. 我正在尝试使用xsl转换将数据从file2复制到file1。 I am able to copy the data, but my xsd validation fails on the resulting xml file. 我能够复制数据,但是对生成的xml文件的xsd验证失败。 Please help me have the data copied the right way. 请帮助我以正确的方式复制数据。 Here is my code: file1.xml: 这是我的代码:file1.xml:

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

file2.xml: file2.xml:

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

result.xml: 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文件中的代码:

  <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. 我的输出文件具有嵌套的Security元素,这导致验证失败。 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: 考虑使用用于组织安全性的模板在第一个xml树中走下,在后面的模板中,在外部节点上运行多个<xsl:for-each>

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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