搜索和替换元素,使用xslt 3,替换短语是相同的

时间:2018-02-07 15:32:50

标签: xslt-3.0

虽然我输入了一个xml文件,如:

 <?xml version="1.0"?>
    <catalog>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications 
          with XML.</description>
       </book>
       <book id="bk102">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former architect battles corporate zombies, 
          an evil sorceress, and her own childhood to become queen 
          of the world.</description>
       </book>
       <book id="bk103">
          <author>Corets, Eva</author>
          <title>Maeve Ascendant</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-11-17</publish_date>
          <description>After the collapse of a nanotechnology 
          society in England, the young survivors lay the 
          foundation for a new society.</description>
       </book>
    </catalog>

我尝试找到在文件或xsl本身中获得以下信息的最佳方法:

value to search for: 
An in-depth look at creating applications with XML.
add location:
on the self
value to search for:
A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.
add location:
on the self

所以如果我用逗号分隔输入文件,它看起来像:

"An in-depth look at creating applications with XML.","on the self"
"A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.","on the self" 

我已尝试使用xslt 2,但我不断收到错误,因为不允许使用多个项目的序列作为变量$ search_phrase的值......

期望的输出:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>to be checked</description>
      <location>on the self</location>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>to be checked</description>
      <location>on the self</location>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
</catalog>

有人能给我一个xslt-3.0的例子吗?我可以替换上面的短语,并且只要有匹配就添加所需的元素?

我需要做什么:

在完整的xml文件中,有许多记录可以具有相同的描述。我还需要在描述上做一个完全匹配:这句话 &#34;深入了解使用XML创建应用程序,由...创建&#34; 不应该匹配。在我的例子中,我还有一个描述,其中差异只是例如,&#34;深入研究用XML创建应用程序。&#34;不应该也匹配。因为在我的代码中我使用小写,这也可能是问题,但不确定......每当有匹配时,必须将搜索项中指定的位置添加到location元素中,该元素当前不存在于任何位置元素中记录在xml中。

1 个答案:

答案 0 :(得分:0)

以下是关于如何将description元素与作为参数传入的字符串序列进行比较的建议(但您可以从文件中读取它):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    expand-text="yes"
    version="3.0">

  <xsl:param name="new" as="xs:string" select='"on the self"'/>

  <xsl:param name="replace" as="xs:string" select="'to be checked'"/>

  <xsl:param name="search" as="xs:string*"
    select='"An in-depth look at creating applications with XML.",
"A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world."'/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="description[. = $search]">
      <xsl:copy>{$replace}</xsl:copy>
      <location>{$new}</location>
  </xsl:template>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/eiQZDbk正常工作,但仅在编辑样本后才能将所有描述数据放在一行上。

如果不是这样,那么将模板更改为

  <xsl:template match="description[normalize-space() = $search]">
      <xsl:copy>{$replace}</xsl:copy>
      <location>{$new}</location>
  </xsl:template>

应该有所帮助:http://xsltfiddle.liberty-development.net/eiQZDbk/1

如果你有几个术语相互关联,那么某些XML格式似乎更适合于构造数据,所以

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    expand-text="yes"
    version="3.0">

  <xsl:param name="data-url" as="xs:string" select="'data.xml'"/>

  <!-- if you want to load from a file use xsl:param name="replacement-doc" select="doc($data-url)" -->
  <xsl:param name="replacement-doc">
    <root>
        <search>
            <term>An in-depth look at creating applications with XML.</term>
            <replacement>to be checked</replacement>
            <new>on the self</new>
        </search>
        <search>
            <term>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</term>
            <replacement>whatelse</replacement>
            <new>something</new>
        </search>
    </root>
  </xsl:param>

  <xsl:key name="search" match="search" use="term"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="description[key('search', normalize-space(), $replacement-doc)]">
      <xsl:variable name="search" select="key('search', normalize-space(), $replacement-doc)"/>
      <xsl:copy>{$search/replacement}</xsl:copy>
      <location>{$search/new}</location>
  </xsl:template>

</xsl:stylesheet>

我已经提出了一些建议,并修改了模板。在线样本位于http://xsltfiddle.liberty-development.net/eiQZDbk/2。如注释中所示,您可以调整该方法以从单独的文件加载数据,而不是将其保持在XSLT中。