- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
XML::Twig
使用 set_text
方法 - 有一个警告:
set_text ($string) Set the text for the element: if the element is a PCDATA, just set its text, otherwise cut all the children of the element and create a single PCDATA child for it, which holds the text.
因此,如果我想做一些简单的事情,比如 - 比如说 - 更改我的 XML::Document 中所有文本的大小写:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $twig = XML::Twig->new(
'pretty_print' => 'indented_a',
'twig_handlers' => {
'_all_' => sub {
my $newtext = $_->text_only;
$newtext =~ tr/[a-z]/[A-Z]/;
$_->set_text($newtext);
}
}
);
$twig->parse( \*DATA );
$twig->print;
__DATA__
<root>
<some_content>fish
<a_subnode>morefish</a_subnode>
</some_content>
<some_more_content>cabbage</some_more_content>
</root>
这 - 因为 set_text
替换了 child - 被破坏成:
<root></root>
但如果我只关注一个(底层)节点(例如 a_subnode
),那么它工作正常。
是否有一种优雅的方式来替换/转换元素内的文本而不破坏其下方的数据结构?我的意思是,我可以对是否存在 child 或类似的东西进行测试,但是......似乎应该有更好的方法来做到这一点。 (也许是不同的库?)
(为了清楚起见 - 这是我音译文档中所有文本的示例,我的实际用例相当复杂,但仍然是“关于”就地文本转换)。
我正在考虑节点剪切/和/粘贴方法(剪切所有子节点、替换文本、粘贴所有子节点),但这似乎是一种低效的方法。
最佳答案
与其在 _all_
上使用处理程序,不如尝试仅在文本元素上使用它:#TEXT
,并将 text_only
更改为 文本
。它应该工作。
更新:或者在创建 Twig 时使用char_handler
选项:char_handler => sub { uc shift },
而不是处理程序.
关于XML::Twig - 没有破坏结构的 set_text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30802600/
Twig 使用 {{ }}、{% %}、{# #} 分隔符。 但是如何在 Twig 模板中显示 {{ }} 呢?我不是在谈论 HTML 转义。 我问这个问题是因为我想在我的 Twig 模板中包含一个
为了转换我的法语 html 口音,我尝试通过在 Twig 中执行此操作来更改编码: {{ ddf.description | convert_encoding('UTF-8', 'HTML-ENTIT
我有一个带有输出元素的 .twig 文件的循环。 我需要为每个元素增加一个值。我知道如何在 PHP 中执行此操作,但不清楚如何使用 twig 文档执行此操作。我真的不能在 Controller 中做到
我有一个 xxx.html.twig 文件,它显示一个页面,但是当我想用不同的数据刷新页面并用新数据更新它时,我有一个选择和一个提交按钮。问题是我不知道如何在 Controller 中调用一个 Act
我需要使用 Liquid 来创建模板,但可能只能访问 Twig 模板语言。这两种语言基本上是相同的东西,还是语言上存在差异,导致很难使用 Twig 来替代 Liquid? 最佳答案 Twig 和 Li
number rounding 的 Twig 文档谈论四舍五入小数,但我有一个案例,我想将 19,995 这样的数字四舍五入到 20,000。是否有一种巧妙的方法可以四舍五入到最接近的千位? 最佳答案
我在 2.7 和 3.1 版本中都有 Symfony 项目。 PhpStorm 启用了 Symfony 插件和 Twig 支持插件。 当我在 Symfony 2.7 版本的 PhpStorm 中工作时
我已升级到新版本的 twig/twig 3.x.x。从 twig 版本 3.0 开始,Twig_Extension 已被弃用。所以我接下来要讨论“whiteoctober/breadcrumbs-bu
我在 Twig 中经常有类似的样板代码: {% if (somelongcond) %} {% endif %} Some long content {% if (somelongcond)
我想在我自己的插件中覆盖来自插件的 Twig 模板的 block 。我过去曾覆盖过 native 商店软件模板,但我在使用插件模板时遇到了麻烦。 我正在尝试调整的插件是 FroshProductCom
我用带 Twig 的silex; 我创建自定义 form_div_layout 并将其放在 webroot 中(例如) 我这样注册 TwigService 提供者 $app->register
我不太明白 Twig 中的属性函数是如何工作的。有人可以帮我举个例子吗? 我在 SQL 中有一个名为动态的字段。我可以是例如“field27”,但我不知道号码,号码保存在 radio.id 中。我想做
我想知道是否有一种方法可以在 TWIG 中创建多语句 示例:两个单独的语句... {% set foo:bar %} {% set baz:qux %} 合并成一个语句 {% set foo:
我正在尝试使用 Twig i18n 扩展。 据我所知,我需要的文件在这里: https://github.com/fabpot/Twig-extensions/blob/master/lib/Twig
我正在使用 patternlab 的节点版本用 Twig 作为模板引擎。我正在使用 twig,因为我的代码库是用 twig 编写的 - 所以使用 mustache 不是一个选项。 我简单地尝试包含一个
我有以下代码: {% set pageDescription = item.description|length > 197 ? item.description|striptags|trim|sli
我有以下 Twig 代码: {% for likeditem in user.getItemLikes() %} //iterate over each liked items here {%
反正有没有像这样在 Twig 中重复一个块: {% block title %}{% endblock %} | MyBusiness 为了只声明两个块一次?像那样: {% block title
编辑:2016年12月3日 想学习如何向 Twig 添加自定义扩展(过滤器)吗?请参阅this answer的lxg 您是否只需要找到与ucword等效的嫩枝?请参阅this answer的Javie
我有一个简单的浮点数组。我需要将它显示为逗号分隔的字符串。 {{ arr|join(', ') }} 由于过度的微不足道的准确性,这是一个糟糕的解决方案。 {% for val in arr %}
我是一名优秀的程序员,十分优秀!