使用xslt将数据从xml文件复制到另一个xml文件时出现问题
我正在尝试使用xsl转换将数据从文件2复制到文件1。我可以复制数据,但在生成的xml文件上我的xsd验证失败。请帮助我以正确的方式复制数据。这是我的密码: file1.xml:使用xslt将数据从xml文件复制到另一个xml文件时出现问题,xml,xslt,Xml,Xslt,我正在尝试使用xsl转换将数据从文件2复制到文件1。我可以复制数据,但在生成的xml文件上我的xsd验证失败。请帮助我以正确的方式复制数据。这是我的密码: file1.xml: <Org> <Security xmlns:saxon="http://saxon.sf.net" /> </Org> file2.xml: <Profile> <Policy>Policy1</Policy> <Pol
<Org>
<Security xmlns:saxon="http://saxon.sf.net" />
</Org>
file2.xml:
<Profile>
<Policy>Policy1</Policy>
<PolicyValue>Value1</PolicyValue>
</Profile>
政策1
价值1
result.xml:
<Org>
<Security xmlns:saxon="http://saxon.sf.net">
<Security>
<Profile>
<Policy>Policy1</Policy>
<PolicyValue>Value1</PolicyValue>
</Profile>
</Security>
</Security>
</Org>
政策1
价值1
期望输出:
<Org>
<Security xmlns:saxon="http://saxon.sf.net">
<Profile description="SecurityProfile">
<Policy description="SecurityProfile">Policy1</Policy>
<PolicyValue description="SecurityProfile">Value1</PolicyValue>
</Profile>
</Security>
</Org>
政策1
价值1
以下是我的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>
我的输出文件具有嵌套的安全元素,导致验证失败。有人能帮我解决这个问题吗。我还需要向复制的所有元素递归地添加属性值。我可以将变量设置为从查找文件中读取属性。我无法设置子节点的属性值
感谢您的帮助。考虑使用Org和Security模板沿着第一个xml的树走下去,在后一个模板中,在外部节点上运行多个
:
<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>
安全档案
安全档案
考虑使用组织和安全模板沿着第一个xml的树走下去,在后一个模板中,在外部节点上运行多个
:
<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>
安全档案
安全档案
感谢您的快速回复。“安全性”位于xml文件中的两个不同位置,需要使用一个具有子元素的位置进行转换。上述解决方案删除了第二次出现的子节点,并从file2复制了节点,我们如何保留安全性的子节点并从file2复制数据。当我们从file2复制元素时,它没有获得所需的属性,我们如何获得它们?此外,安全元素上的属性也将被删除。是否可以向安全元素添加一些属性。谢谢你的帮助。我现在不在我的机器旁,但我几乎可以肯定这个解决方案的结果符合你发布的期望输出。可能您正在使用不同的XML数据运行?为了安全起见,我从XML文件中获取了第一个节点,它有一个属性,没有子节点。我将其发布为所需的输出。当我将您提供的代码应用于xml文件时,我发现它从第二个安全节点中删除了子节点。对不起,给你添麻烦了。谢谢你的帮助。请检查你想要的输出。只有一个安全节点,我刚刚检查了我的运行,它完全按照您的要求呈现。感谢您的快速回复。“安全性”位于xml文件中的两个不同位置,需要使用一个具有子元素的位置进行转换。上述解决方案删除了第二次出现的子节点,并从file2复制了节点,我们如何保留安全性的子节点并从file2复制数据。当我们从file2复制元素时,它没有获得所需的属性,我们如何获得它们?此外,安全元素上的属性也将被删除。是否可以向安全元素添加一些属性。谢谢你的帮助。我现在不在我的机器旁,但我几乎可以肯定这个解决方案的结果符合你发布的期望输出。可能您正在使用不同的XML数据运行?为了安全起见,我从XML文件中获取了第一个节点,它有一个属性,没有子节点。我将其发布为所需的输出。当我将您提供的代码应用于xml文件时,我发现它从第二个安全节点中删除了子节点。对不起,给你添麻烦了。谢谢你的帮助。请检查你想要的输出。只有一个安全节点,我刚刚检查了我的运行,它完全按照您的要求呈现。