我有两个与将XML转换为XML有关的问题。
<author>的值是Mike,我想要创建一个名为Mike的新标记)。<grade>的值为5,我希望生成一个名为Mike的新标记,其属性名为"5")。以下是我的例子:现有XML:
<art>
<images>
<image>
<title>Cat</title>
<author>Mike</author>
<grade>5<grade>
</image>
<image>
<title>Snake</title>
<author>John</author>
<grade>4<grade>
</image>
</images>
</art>标签艺术中的新元素:
<authors>
<Mike grade="5">
<field_of_art>photography</field_of_art>
</Mike>
</authors>发布于 2016-01-18 19:54:52
因此,您可以使用<xsl:element>和<xsl:attribute>标记来完成这一任务:
<!-- assumes the current context is an image tag -->
<xsl:element name="{author}">
<xsl:attribute name="grade">
<xsl:value-of select="grade" />
</xsl:attribute>
<field_of_art>photography</field_of_art>
</xsl:element>但是要注意--如果author标记包含空格或无效字符,它将无法工作。例如,如果您有一个<author>Mike 2</author>,样式表将失败,因为标记名不能有空格,或者如果有一个<author>Mike&Jane</author>,它也会失败。
https://stackoverflow.com/questions/34862865
复制相似问题