【问题标题】:Select a node based on value of another node using an xsl predicate使用 xsl 谓词根据另一个节点的值选择一个节点
【发布时间】:2011-10-19 16:32:18
【问题描述】:

与此问题类似的问题: XPath: select a node based on another node?

对象是根据兄弟节点的值选择一个节点——在本例中是基于Pagetype节点的值的Pagetitle节点。

xpaths:

/dsQueryResponse/Rows/Row/@Title
/dsQueryResponse/Rows/Row/@Pagetype
/dsQueryResponse/Rows/Row/@Pagetitle

这个 xsl 没有返回任何东西:

<xsl:value-of select= "/dsQueryResponse/Rows/Row[Pagetype='Parent']/@Pagetitle" />  

示例 xml:

<dsQueryResponse>
       <Rows>
            <Row>
               <Title>1</Title>
               <Pagetype>Parent</Pagetype>
               <Pagetitle>title of page</Pagetitle>
            </Row>
        </Rows>
</dsQueryResponse>  

目标是返回 Pagetitle 的值,如果它们的 Pagetype 值为“Parent”。

【问题讨论】:

  • 请向我们展示输入 XML 的样本并说明您要选择哪些节点。您当前的谓词 [Pagetype='Parent'] 检查名称为 Pagetype 且值为 Parent 的子元素。
  • @Martin 添加了 xml 示例并试图阐明我想要选择的节点。

标签: xml sharepoint xslt xpath predicates


【解决方案1】:

@ 符号表示节点的属性。所以如果要返回Pagetype属性等于Parent的属性Pagetitle的值,应该是:

<xsl:value-of select= "/dsQueryResponse/Rows/Row[@Pagetype='Parent']/@Pagetitle" />

我用来测试 XPATH 的有用资源是 http://www.xmlme.com/XpathTool.aspx

【讨论】:

  • 感谢您的链接。那会派上用场的。我添加了一个 xml 的 sn-p 来澄清。 Pagetype 和 Pagetitle 都是元素/节点,没有属性。我想我对何时应该使用 @ 感到困惑。 xpath Sharepoint Designer 将其包含在 Pagetile 元素中。
  • @_Aaron:似乎 OP 在您的回答之后添加了源 XML,并且其中没有 Pagetype 属性。
【解决方案2】:

使用提供的 XML 文档

/*/*/*[Pagetype = 'Parent']/Pagetitle

基于 XSLT 的验证

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select="/*/*/*[Pagetype = 'Parent']/Pagetitle"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时

<dsQueryResponse>
       <Rows>
            <Row>
               <Title>1</Title>
               <Pagetype>Parent</Pagetype>
               <Pagetitle>title of page</Pagetitle>
            </Row>
        </Rows>
</dsQueryResponse>

计算 XPath 表达式并输出所有(在这种情况下只有一个)选定节点

<Pagetitle>title of page</Pagetitle>

【讨论】:

  • 我试过了,输出还是没有。即使是简单的 value-of 也不会返回任何内容。如果我使用 xsl:ifs 过滤并且不使用谓词,它就可以工作,所以我很确定 xpath 是正确的。也许这与 Sharepoint 读取 xsl 的方式有关。
【解决方案3】:

我是这样理解这个问题的:查找每个 Row 的子元素 Pagetype 为“Parent”,显示子元素 Pagetitle 的值。

我的解决方案:创建一个包含所有满足条件的行的节点集,然后选择子元素 Pagetitle 的值。

<xsl:for-each select="/dsQueryResponse/Rows/Row[Pagetype='Parent']">
    <xsl:value-of select="Pagetitle" />
</xsl:for-each>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2011-08-26
    • 2016-08-23
    相关资源
    最近更新 更多