仅处理选定的节点

发布于 2024-07-08 14:11:12 字数 1537 浏览 6 评论 0原文

XSLT 新手问题:我需要替换 XML 文件中的文本值。 所有其他节点必须保持不变。 这是我的输入文件 (in.xml):

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <level1 attr1="val1">
        <level2>in</level2>
    </level1>
</root>

这是我的 XSLT 转换 (subst.xsl):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="//node()">
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/root/level1/level2/text()">out</xsl:template>
</xsl:stylesheet>

我使用以下 Ant 脚本 (build.xml) 运行它:

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="test" basedir=".">
    <target name="test">
        <xslt style="subst.xsl" in="in.xml" out="out.xml" />
    </target>
</project>

这是我得到的 (out.xml):

<?xml version="1.0" encoding="UTF-8"?><root>
    <level1>
        <level2>out</level2>
    </level1>
</root>

属性“attr1”缺少“level1”的“。

如果有人

  • 告诉我subst.xsl 有什么问题

或者

  • 给我一个想法如何强制 xslt-processor仅复制不匹配的节点到输出文件并手动完成(在我的例子中很容易出错)。

An XSLT-newbie problem: I need to substitute a text value in XML-file. All other nodes must be left unchanged. Here's my input file (in.xml):

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <level1 attr1="val1">
        <level2>in</level2>
    </level1>
</root>

Here's my XSLT-transformation (subst.xsl):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="//node()">
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/root/level1/level2/text()">out</xsl:template>
</xsl:stylesheet>

I run it with the following Ant-script (build.xml):

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="test" basedir=".">
    <target name="test">
        <xslt style="subst.xsl" in="in.xml" out="out.xml" />
    </target>
</project>

And here's what I get (out.xml):

<?xml version="1.0" encoding="UTF-8"?><root>
    <level1>
        <level2>out</level2>
    </level1>
</root>

Attribute "attr1" of "level1" is missing.

I would be very grateful if anyone

  • tell me what's wrong with subst.xsl

OR

  • give me an idea how to force xslt-processor just copy non-matched nodes to output file and to do it by hand (which is in my case error-prone).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

深海夜未眠 2024-07-15 14:11:12

您的身份转换缺少属性(显然)。 使用这个代替:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

然后,只需添加您的最后一个模板。

Your identity transform is missing attributes (obviously). Use this instead:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Then, just add your last template.

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