xsl:value-of 用作 xsl:element 名称 [英] xsl:value-of used as xsl:element name
问题描述
我有一个带有列表结构的传入 XML 文件:
I have an incoming XML file with a list structure:
<list>
<listItem>
<name>elementOne</name>
<value>elementOneValue</name>
</listItem>
<listItem>
<name>elementTwo</name>
<value>elementTwoValue</name>
</listItem>
</list>
我正在尝试转换为这种结构:
Which I am trying to convert to this structure:
<elementOne>elementOneValue</elementOne>
<elementTwo>elementTwoValue</elementTwo>
这是使用 XSL 实现的简单逻辑,但我遇到了复杂情况.
This is easy logic to implement with XSL, but I'm running into complications.
<xsl:for-each select="/list/listItem">
<xsl:element name="<xsl:value-of select="name"/>">
<xsl:value-of select="value"/>
</xsl:element>
</xsl:for-each>
不起作用,因为我认为连续双引号破坏了 <xsl:element>
标签
Doesn't work because I assume the sequential double quotes are breaking the <xsl:element>
tag
<xsl:for-each select="/list/listItem">
<<xsl:value-of select="name"/>>
<xsl:value-of select="value"/>
</<xsl:value-of select="name"/>>
</xsl:for-each>
不起作用,因为我不能使用 <<
或 >>
和
Doesn't work because I can't use <<
or >>
and
<xsl:for-each select="/list/listItem">
<<xsl:value-of select="name"/>>
<xsl:value-of select="value"/>
</<xsl:value-of select="name"/>>
</xsl:for-each>
不起作用,因为我最终得到了 >并且<在我的代码中,而不是 XML 可解析的 <
或 >
.我希望这是一个非常简单的解决方案,但我在互联网上找不到任何记录.我忽略的简单修复是什么?
Doesn't work because I end up with > and < in my code instead of XML parseable <
or >
. I expected this to be a very easy solution but I can't find any records for it on the internet. What is the simple fix I'm overlooking?
推荐答案
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="listItem">
<xsl:element name="{name}">
<xsl:value-of select="value"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时(更正为格式正确):
<list>
<listItem>
<name>elementOne</name>
<value>elementOneValue</value>
</listItem>
<listItem>
<name>elementTwo</name>
<value>elementTwoValue</value>
</listItem>
</list>
产生想要的结果:
<elementOne>elementOneValue</elementOne>
<elementTwo>elementTwoValue</elementTwo>
请注意:
使用
指令来创建一个静态名称未知的元素.
The use of the
<xsl:element>
instruction to create an element with statically unknown name.
AVT 的使用(Attribute-Value-Template) 以紧凑且更易读的方式指定 name
属性.
如果elementOne
和elementTwo
的字符串值不符合词法/元素名称 (QName) 的句法规则.
It is possible for an error to be raised if the string values of elementOne
and elementTwo
do not obey the lexical/syntactic rules for an element name (QName).
这篇关于xsl:value-of 用作 xsl:element 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!