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 文件中,有许多记录可以具有相同的描述。我还需要对描述进行完全匹配:不应匹配短语“深入了解使用 XML 创建应用程序,作者...”。在我的例子中,我也有一个描述,其中的区别只是这种情况,例如“深入了解使用 XML 创建应用程序”。不应该也匹配。因为在我的代码中我使用小写,这也可能是问题,但不确定......每当有匹配项时,必须将沿着搜索词指定的位置添加到 location 元素中,该元素目前不存在于任何记录在xml中。

4

1 回答 1

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 中。

于 2018-02-07T17:30:05.780 回答