[英]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.