[英]Problem with copying data from an xml file to another xml file using xslt
我正在尝试使用xsl转换将数据从file2复制到file1。 我能够复制数据,但是对生成的xml文件的xsd验证失败。 请帮助我以正确的方式复制数据。 这是我的代码: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>
所需的输出:
<Org>
<Security xmlns:saxon="http://saxon.sf.net">
<Profile description="SecurityProfile">
<Policy description="SecurityProfile">Policy1</Policy>
<PolicyValue description="SecurityProfile">Value1</PolicyValue>
</Profile>
</Security>
</Org>
这是我的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>
我的输出文件具有嵌套的Security元素,这导致验证失败。 有人可以帮我解决问题。 另外,我需要向所有复制的元素递归添加属性值。 我能够设置变量以从查找文件读取属性。 我无法将属性值设置为子节点。
谢谢您的帮助。
考虑使用用于组织和安全性的模板在第一个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.