xslt 输出包含一些无效的元素标签名称

发布于 2024-12-27 00:54:13 字数 987 浏览 5 评论 0原文

我可以生成以下输出作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不离久伴 2025-01-03 00:54:13

简单修改(将 < 更改为 <):

<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" />
    </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<Root>
    <a>me</a>
</Root>

转换产生想要的结果

            <134>my<134>
            <114>xml<114>
            <567>notgood<567>
            <789>me<789>

XSLT可以有效且相当方便地使用来生成非 XML 输出 - 无论是 HTML 还是纯文本

A simple modification (change < to <):

<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" />
    </xsl:template>
</xsl:stylesheet>

When applied on the provided XML document:

<Root>
    <a>me</a>
</Root>

the transformation produces the wanted result:

            <134>my<134>
            <114>xml<114>
            <567>notgood<567>
            <789>me<789>

XSLT can be used effectively and quite conveniently to produce non-XML output -- be it HTML or just plain text.

毅然前行 2025-01-03 00:54:13

您可以使用标记字符创建文本节点,例如

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:text><![CDATA[<789>]]></xsl:text>
  <xsl:value-of select="/Root/a"/>
  <xsl:text><![CDATA[</789>]]></xsl:text>
</xsl:template>

,但我不确定 XSLT 或任何其他 XML 工具是否是创建非 XML 内容的不错选择。

You can create text nodes with markup characters e.g.

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:text><![CDATA[<789>]]></xsl:text>
  <xsl:value-of select="/Root/a"/>
  <xsl:text><![CDATA[</789>]]></xsl:text>
</xsl:template>

but I am not sure XSLT or any other XML tool is a good choice to create something that is not XML.

旧人九事 2025-01-03 00:54:13

由于您生成的输出格式与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文