gpt4 book ai didi

xml - 使用 xslt 将节点从 XML 插入到另一个 XML 文件

转载 作者:行者123 更新时间:2023-12-03 17:04:26 28 4
gpt4 key购买 nike

所以我有一个这样的 xml 文件:

<?xml version="1.0"?>
<class>

<students>
<name origin="English" firstname="Jeff" lastname="Richards"/>
<name origin="French" firstname="Martel" lastname="Francois"/>
</students>

<teachers>
<name origin="Spanish" firstname="Enrique" lastname="Rosa"/>
</teachers>

</class>

还有另一个像这样的 xml 文件:

 <?xml version="1.0"?>
<name origin="English" firstname="Richard" lastname="Priestly"/>
<name origin="Russian" firstname="Alexey" lastname="Romanov"/>

如何使用 xslt 将第二个文件中的两个元素添加到第一个文件中的学生元素中?换句话说,我如何创建一个如下所示的文件:

<?xml version="1.0"?>
<class>

<students>
<name origin="English" firstname="Jeff" lastname="Richards"/>
<name origin="French" firstname="Martel" lastname="Francois"/>
<name origin="English" firstname="Richard" lastname="Priestly"/>
<name origin="Russian" firstname="Alexey" lastname="Romanov"/>
</students>

<teachers>
<name origin="Spanish" firstname="Enrique" lastname="Rosa"/>
</teachers>

</class>

如果无法使用 xslt,是否可以使用 XPath?

非常感谢!

最佳答案

这是一种方法,假设您通过添加“students”根节点并将其命名为“students.xml”使第二个文件格式正确:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output indent="yes" method="xml"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="students">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:for-each select="document('students.xml')/students/name">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

关于xml - 使用 xslt 将节点从 XML 插入到另一个 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19392750/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com