【问题标题】:How to change a element/tag value by its attribute value?如何通过其属性值更改元素/标签值?
【发布时间】:2014-06-20 21:27:07
【问题描述】:

我有一个带有一长串标签的 XML 代码,我想在每个元素“文本”中用它们各自的“表单”属性值替换其中的单词和标签。

例如,以下是我的 XML 文件中的两句话:

<messages>
    <text>
        <spelling form="Hello">Helo</spelling> I'll see you next <abrev form="week">wk</abrev> alright.
    </text>
    <text>
        <abrev form="Come on">cmon</abrev> get ready <spelling form="dude">dood</spelling>!
    </text>
</messages>

这是我正在寻找的输出:

Hello I'll see you next week alright.
Come on get ready dude!

有人知道怎么做吗?


这是我目前在 XSL 文件中的内容:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:for-each select="messages/text">

        <xsl:call-template name="parse">

            <!-- this selects all the tags inside "text" (I think...) -->
            <xsl:with-param name="a" select="./*"/>

        </xsl:call-template>

    </xsl:for-each>
</xsl:template>

然后,我的函数“解析”:

<xsl:template name="parse">

    <!-- "a" is the text to parse -->
    <xsl:param name="a"/>

    <!-- return the value of "form" -->
    <xsl:value-of select="$a/@form"/>

</xsl:template>

现在,我的“解析”函数还没有完成。我不知道如何用“form”值替换拼写错误的单词。

感谢您的帮助!

【问题讨论】:

  • 你有&lt;spelling&gt;&lt;abrev&gt; 还是只有一个?它们在您的源中没有正确关闭。

标签: xml xslt treeview xslt-1.0


【解决方案1】:

假设您的文本中可能同时包含 &lt;abrev&gt;&lt;spelling&gt; 元素(实际上是任何具有 form 属性的元素),并且您的真实 XML 格式正确,此样式表可用于替换标记的具有form 属性值的文本:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="text"/>

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

    <xsl:template match="text/*[@form]">
        <xsl:value-of select="@form"/>
    </xsl:template>
</xsl:stylesheet>

如果将其应用于此输入:

<messages>
    <text>
        <spelling form="Hello">Helo</spelling> I'll see you next <abrev form="week">wk</abrev> alright.
    </text>
    <text>
        <spelling form="Come on">cmon</spelling> get ready <abrev form="dude">dood</abrev>!
    </text>
</messages>

你会得到这个作为输出:

    Hello I'll see you next week alright.

    Come on get ready dude!

【讨论】:

    【解决方案2】:

    您可以从标识模板开始,然后从那里为每个元素创建模板,以便您可以控制它们的特定输出。例如,让text 元素输出其文本,同时运行spellingabrev 元素的模板,输出其@form 属性。

    所以看起来像下面这样。

      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="messages">
        <xsl:apply-templates/>
      </xsl:template>
    
      <xsl:template match="text">
        <xsl:apply-templates select="*|text()"/>
      </xsl:template>
    
      <xsl:template match="spelling">
        <xsl:value-of select="@form"/>
      </xsl:template>
    
      <xsl:template match="abrev">
        <xsl:value-of select="@form"/>
      </xsl:template>
    

    【讨论】:

    • 您也可以使用&lt;xsl:template match="spelling | abrev"&gt; 删除重复项。
    • @helderdarocha 是的,我完全错过了,直到我看到你的回答。在我们的两个答案之间,他们应该能够弄清楚他们想用它做什么。
    猜你喜欢
    • 1970-01-01
    • 2019-02-08
    • 2012-10-04
    • 2012-04-05
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多