jjzjj

xml - XSL : Replacing certain characters with xml tags

coder 2024-06-25 原文

这个有点棘手,我已经坚持了一段时间。我想做的是将标签放在方括号“[”(例如按钮、链接等)和“]”的位置

<section>
    <title>Buttons</title>
    <orderedlist>
        <listitem>
            <para>Clicking on [Save] will attempt to save changes, then it navigates to <xref linkend="saved" xrefstyle="select: title"/>.</para>
        </listitem>
        <listitem>
            <para>Clicking on [Cancel] navigates to <xref linkend="noSave" xrefstyle="select: title"/>.</para>
        </listitem>
    </orderedlist>
</section>

收件人:

<section>
    <title>Buttons</title>
    <orderedlist>
        <listitem>
            <para>Clicking on <uicontrol>Save</uicontrol> will attempt to save changes, then it navigates to <xref linkend="saved" xrefstyle="select: title"/>.</para>
        </listitem>
        <listitem>
            <para>Clicking on <uicontrol>Cancel</uicontrol> navigates to <xref linkend="noSave" xrefstyle="select: title"/>.</para>
        </listitem>
    </orderedlist>
</section>

而且 '[' ']' 不一定总是在 section.listitem.para 中

编辑:我只需要 [] 替换括号中的某些词。

最佳答案

对于没有嵌套的uicontrol对于嵌套括号(需要解析平衡括号与无平衡括号)。

这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
 <xsl:template match="text()" name="replace" priority="1">
  <xsl:param name="pString" select="."/>
  <xsl:variable name="vMask" select="translate($pString,
                                                     translate($pString,
                                                               '[]',
                                                               ''),
                                                     '')"/>
  <xsl:choose>
   <xsl:when test="contains($vMask,'[]')">
    <xsl:call-template name="makeControl">
     <xsl:with-param name="pString" select="$pString"/>
     <xsl:with-param name="pMask"
                     select="substring-before($vMask,'[]')"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$pString"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 <xsl:template name="makeControl">
  <xsl:param name="pString"/>
  <xsl:param name="pMask"/>
  <xsl:choose>
   <xsl:when test="$pMask">
    <xsl:variable name="vMask" select="substring($pMask,1,1)"/>
    <xsl:value-of select="concat(
                             substring-before(
                                $pString,
                                $vMask),
                             $vMask)"/>
    <xsl:call-template name="makeControl">
     <xsl:with-param name="pString"
                     select="substring-after($pString,$vMask)"/>
     <xsl:with-param name="pMask" select="substring($pMask,2)"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="substring-before($pString,'[')"/>
    <uicontrol>
     <xsl:value-of select="substring-before(
                                             substring-after(
                                                $pString,
                                                '['),
                                             ']')"/>
    </uicontrol>
    <xsl:call-template name="replace">
     <xsl:with-param name="pString"
                                    select="substring-after($pString,']')"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

输出:

<section>
 <title>Buttons</title>
 <orderedlist>
  <listitem>
   <para>Clicking on <uicontrol>Save</uicontrol> will attempt to save changes, then it navigates to <xref linkend="saved" xrefstyle="select: title"></xref>.</para>
  </listitem>
  <listitem>
   <para>Clicking on <uicontrol>Cancel</uicontrol> navigates to <xref linkend="noSave" xrefstyle="select: title"></xref>.</para>
  </listitem>
 </orderedlist>
</section>

有了这个输入:

<text>
This is an opening bracket [ ? [Yes] [No]
This is a closing bracket ] ? [Yes] [No]
</text>

输出:

<text>
This is an opening bracket [ ? <uicontrol>Yes</uicontrol> <uicontrol>No</uicontrol>
This is a closing bracket ] ? <uicontrol>Yes</uicontrol> <uicontrol>No</uicontrol>
</text>

注意:任何匹配 \[[^\[\]]*\] 的文本将被包装成 uicontrol元素。

关于xml - XSL : Replacing certain characters with xml tags,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3972059/

有关xml - XSL : Replacing certain characters with xml tags的更多相关文章

  1. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  2. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  3. ruby-on-rails - 如何在 Rails 3 中禁用 XML 解析 - 2

    我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::

  4. ruby - 如何使用 Nokogiri::XML::Builder 生成动态标签? - 2

    我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi

  5. ruby - 如何让 Nokogiri 解析并返回 XML 文档? - 2

    这是一些奇怪的例子:#!/usr/bin/rubyrequire'rubygems'require'open-uri'require'nokogiri'print"withoutread:",Nokogiri(open('http://weblog.rubyonrails.org/')).class,"\n"print"withread:",Nokogiri(open('http://weblog.rubyonrails.org/').read).class,"\n"运行此返回:withoutread:Nokogiri::XML::Documentwithread:Nokogiri::

  6. ruby - 模式加载时出现 Nokogiri::XML::Schema SyntaxError - 2

    我正在尝试加载SAML协议(protocol)架构(具体来说:https://www.oasis-open.org/committees/download.php/3407/oasis-sstc-saml-schema-protocol-1.1.xsd),但在执行此操作之后:schema=Nokogiri::XML::Schema(File.read('saml11_schema.xsd'))我得到这个输出:Nokogiri::XML::SyntaxErrorException:Element'{http://www.w3.org/2001/XMLSchema}element',att

  7. ruby-on-rails - 带有选项的 link_to image_tag (rel, title) - 2

    在HTML中我会写:但我必须针对ruby​​onrails对其进行调整,而且我对它还很陌生...所以我尝试了:rel="lightbox"title="mycaption">...但它不起作用,因为“rel="lightbox"title="mycaption">”部分未应用但出现在html部分上+我看到了2个图像,而我应该只看到“imagethumb”。我也试过:"lightbox",:title=>"mycaption")%>我也看到了这两张图...我应该怎么做才能获得与我编写的HTML代码等效的内容? 最佳答案 试试这个"")

  8. ruby-on-rails - 如果参数为空,则 Text_field_tag 默认值? - 2

    如果params[:date]的参数为空,我希望我的text_field_tag将当前日期作为默认值,这是我目前的代码:我想要类似:谢谢 最佳答案 您可以简单地使用“或”运算符。如果params[:end]为空,它将使用Time.now。 关于ruby-on-rails-如果参数为空,则Text_field_tag默认值?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/35520

  9. ruby-on-rails - 如何在 RoR 中使用 content_tag 嵌入标签? - 2

    我有这个可以为我生成一个超链接:我希望它显示在td标签中,所以我想使用这个content_tag来帮助我:"example")%>我想要我的td中的超链接,所以我有这样的东西:,:class=>"example")%>但是我收到语法错误,我该怎么办? 最佳答案 内联:'example')%>或block形式:'example')do%> 关于ruby-on-rails-如何在RoR中使用content_tag嵌入标签?,我们在StackOverflow上找到一个类似的问题:

  10. ruby-on-rails - stylesheet_link_tag(:all) generates reference to `all.css` on Heroku - 2

    我的布局中有stylesheet_link_tag(:all)。即使在生产环境中运行它(railss-eproduction),它在本地机器上的表现也如预期。我所说的预期是指它发出所有指向现有样式表的链接,而不将它们连接到all.css中,并且它不发出指向all的链接。CSS.但是当我将它部署到Heroku时,结果是一样的,并且在开头添加了一个指向all.css的链接。这是我不想要也不期望的,尤其是当本地机器上的生产环境不发出它时。所以问题是如何在不手动指定所有文件的情况下摆脱Heroku上的all.css链接?谢谢。 最佳答案 H

随机推荐