xslt 输出包含一些无效的元素标签名称
我可以生成以下输出作为 xsl 文件的转换输出吗?
需要输出(无顶部标签且标签名称以数字开头)。由于它是无效标签,我尝试将输出方法设置为我使用的 xsl 的“文本”
<134>my</134>
<114>xml</114>
<567>notgood</567>
<789>me</789>
输入 xml
<Root>
<a>me</a>
</Root>
,但无法工作,因为不允许数字作为标签的开头。然而我们需要得到这个输出。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:template match="/">
<xsl:variable name="output">
<Root>
<134>my</134>
<114>xml</114>
<567>notgood</567>
<789>
<xsl:value-of select="/Root/a"/>
</789>
</Root>
</xsl:variable>
<xsl:copy-of select="$output/Root" />
</xsl:template>
</xsl:stylesheet>
Can I produce the below ouput as the transform output of a xsl file.
Output needed(no top tag and the tag names start with numbers). As it is an invalid tag, I tried setting the output method to "text"
<134>my</134>
<114>xml</114>
<567>notgood</567>
<789>me</789>
input xml
<Root>
<a>me</a>
</Root>
the xsl i used, not working as numbers are not allowed as starting of tags. However we would need to get this output.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:template match="/">
<xsl:variable name="output">
<Root>
<134>my</134>
<114>xml</114>
<567>notgood</567>
<789>
<xsl:value-of select="/Root/a"/>
</789>
</Root>
</xsl:variable>
<xsl:copy-of select="$output/Root" />
</xsl:template>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单修改(将
<
更改为<
):应用于提供的 XML 文档时:
转换产生想要的结果:
XSLT可以有效且相当方便地使用来生成非 XML 输出 - 无论是 HTML 还是纯文本。
A simple modification (change
<
to<
):When applied on the provided XML document:
the transformation produces the wanted result:
XSLT can be used effectively and quite conveniently to produce non-XML output -- be it HTML or just plain text.
您可以使用标记字符创建文本节点,例如
,但我不确定 XSLT 或任何其他 XML 工具是否是创建非 XML 内容的不错选择。
You can create text nodes with markup characters e.g.
but I am not sure XSLT or any other XML tool is a good choice to create something that is not XML.
由于您生成的输出格式与 XML 有点相似,因此另一种选择是生成有效的 XML 输出,然后对其进行后处理。例如,您可以输出
<_123>
代替<123>
,然后使用 sed 或类似工具删除下划线。Another option, since you are producing an output format that bears some resemblance to XML, would be to produce valid XML output and then post-process it. For example you could output
<_123>
in place of<123>
and then get rid of the underscores using sed or similar.