[英]XSLT for Variable Element Name for multiple same elements
I have a piece of XML data with information for multiple persons represented as - 我有一段XML数据,其中有多个人的信息,表示为-
<phoneContact>
<firstName>XXXXX</firstName>
<middleName>Y</middleName>
<lastName>ZZZZZ</lastName>
<phone>1234567890</phone>
</phoneContact>
<phoneContact>
<firstName>AAAA</firstName>
<middleName>B</middleName>
<lastName>CCCCC</lastName>
<phone>9876543210</phone>
</phoneContact>
There may be any number of persons available. 可能有许多人可用。 I want to transform this into - 我想将其转换为-
<phoneContact1>
<firstName>XXXXX</firstName>
<middleName>Y</middleName>
<lastName>ZZZZZ</lastName>
<phone>1234567890</phone>
</phoneContact1>
<phoneContact2>
<firstName>AAAA</firstName>
<middleName>B</middleName>
<lastName>CCCCC</lastName>
<phone>9876543210</phone>
</phoneContact2>
.. and so forth. ..依此类推。 How do I construct an XSL for-each code that creates multiple such different element names ? 如何构造一个XSL for-each代码,该代码创建多个这样的不同元素名称?
Thank you for any help with this. 感谢您对此的任何帮助。
Inside the loop over the phoneContact
elements you can use <xsl:element>
and the the position function to create your numbered phoneContacts
: 在phoneContact
元素的循环内,您可以使用<xsl:element>
和position函数来创建编号的phoneContacts
:
<xsl:for-each select="phoneContact">
<xsl:element name="phoneContact{position()}">
...
</xsl:element>
</xsl:for-each>
we can implement using identity function as below 我们可以使用以下身份函数来实现
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/a/phoneContact">
<xsl:element name="phoneContact{position()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.