[英]XSLT:Injecting values from one XML element into another XML element
I am working with a problem and was wondering how than can be done using XSLT(1.0). 我正在处理一个问题,想知道如何使用XSLT(1.0)来完成。 I have combined the data from 2 XML files into a single one. 我已经将2个XML文件中的数据合并为一个文件。 I have to inject values from one XML node into the other. 我必须将值从一个XML节点注入到另一个。 Since my XML files are pretty big, I don't have to luxury of of creating elements in the XSLT. 由于我的XML文件很大,因此不必在XSLT中创建元素。 I would simply like to somehow map them from one data1 elements to data2 elements. 我只是想以某种方式将它们从一个data1元素映射到data2元素。
Input: 输入:
<combinedData>
<data1>
<element1>
<id>12</id>
<name>Tony Green</name>
<address>Home Address</address>
</element1>
</data1>
<data2>
<element1>
<element2>
<element3>
<IdOfPerson></IdOfPerson>
<NameOfPerson></NameOfPerson>
<addressOfPerson></addressOfPerson>
</element3>
</element2>
</element1>
</data2>
Desired Output: 所需输出:
<data2>
<element1>
<element2>
<element3>
<IdOfPerson>12</IdOfPerson>
<NameOfPerson>Tony Green</NameOfPerson>
<addressOfPerson>Home Address</addressOfPerson>
</element3>
</element2>
</element1>
Any help with this will be highly appreciated. 任何帮助,将不胜感激。
Use the identity transform to copy data2
: 使用身份转换来复制data2
:
<xsl:template match="/">
<xsl:apply-templates select="//data2"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
Use a specific template for the child nodes of element3
. 为element3
的子节点使用特定的模板。 I am assuming that you want to copy the data from the lower-case nodes of the same name of data1/element1
: 我假设您要从具有相同名称data1/element1
的小写节点复制数据:
<xsl:template match="element3/node()">
<xsl:variable name="name-without-of-person" select="substring-before(name(), 'OfPerson')" />
<xsl:variable name="lower-case" select="translate($name-without-of-person, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
<!-- copy the original node -->
<xsl:copy>
<!-- XSLT 1.0 does not support XPath queries that are dynamically generated,
therefore, .../element1/$lower-case does not work.
However, using a predicate with a name() query works -->
<xsl:value-of select="/combinedData/data1/element1/*[name()=$lower-case]"/>
</xsl:copy>
</xsl:template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.