【问题标题】:XPath 2 expressions interfering with each otherXPath 2 表达式相互干扰
【发布时间】:2019-12-22 13:31:53
【问题描述】:

我有一个这样的 XML:

  <Values>
    <Value AttributeID="asset.extension">pdf</Value>
    <Value AttributeID="asset.size">10326</Value>
    <Value AttributeID="ATTR_AssetPush_Webshop">1</Value>
    <Value AttributeID="asset.format">PDF (Portable Document Format application)</Value>
    <Value AttributeID="asset.mime-type">application/pdf</Value>
    <Value AttributeID="asset.filename">filename.pdf</Value>
    <Value AttributeID="asset.uploaded">2018-01-10 17:05:39</Value>
    <Value AttributeID="ATTR_Verwendungsort" Derived="true">WebShop,</Value>
  </Values>

我有 2 个(或更多)这样的 XPath 表达式:

<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values/Value[not(@AttributeID='asset.mime-type')]" />
<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values/Value[not(@AttributeID='asset.size')]" />

但由于某种原因,如果我将其中两个放在一起,则所有信息都将被剥离。如果我只使用 1 个 expressoin,我会得到我想要的输出。我不能像这样使用2个表达式吗?

我也尝试像这样组合它们:

<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values/Value[not(@AttributeID='asset.mime-type') and (@AttributeID='asset.size')]" />

但这也没有做到。

想要的输出应该是这样的:

  <Values>
    <Value AttributeID="asset.size">10326</Value>
    <Value AttributeID="asset.mime-type">application/pdf</Value>
  </Values>

【问题讨论】:

  • 那么想要的输出是什么?
  • 您可能希望将条件更改为[not(@AttributeID='asset.mime-type') and not(@AttributeID='asset.size')][not(@AttributeID='asset.mime-type' or @AttributeID='asset.size')]
  • @MartinHonnen 添加了所需的输出
  • @TimC 就像一个魅力。随意添加答案。

标签: logic set-theory


【解决方案1】:
<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values
                                               /Value[not(@AttributeID='asset.mime-type')]" />
<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values
                                               /Value[not(@AttributeID='asset.size')]" />

这是一个逻辑错误——与 XPath 无关

就像说

  1. 从一周中的所有日子开始,我只在星期一工作
  2. 从上面选择的日子开始,我只会在星期二工作

上面的第一个语句只选择星期一。第二条语句从这些星期一中选择所有星期二——即空集。

正确的说法

从一周中的所有日子开始,我只会在星期一或星期二

工作

【讨论】:

    【解决方案2】:

    我认为在 XSLT 2/3 中您可以将其表达为

    <xsl:template match="Values/Value[not(@AttributeID = ('asset.mime-type', 'asset.size'))]"/>
    

    在 XSLT/XPath 1.0 中,您需要 Values/Value[not(@AttributeID = 'asset.mime-type' or @AttributeID = 'asset.size')]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 2013-04-27
      • 2023-01-31
      • 2019-03-23
      • 2013-05-09
      • 2011-07-14
      • 2021-04-08
      相关资源
      最近更新 更多