【问题标题】:How can I connect the content using ID/IDref on xslt?如何在 xslt 上使用 ID/IDref 连接内容?
【发布时间】:2022-01-13 20:44:58
【问题描述】:

我有一个 xml 文档,我在其中使用了 ID 和 IDREF,希望它们可以使用 xpath 在 xlst 中连接,但我无法找到解决方法。 我的 xml 是这样的:

<root>
  <list>
    <item id="c1"> <description> Class 1 </description> </item>
    <item id="gc1"> <description> Gym-class 1 </description> </item>
    <item id="c2"> <description> Class 2 </description> </item>
  </list>

  <school name="abc">
     <schedule>
       <class idref="c1">
         <day>monday</day>
         <day>friday</day>
       </class>
     </schedule>
  </school>
  <school name="def">
     <schedule>
       <class idref="gc1">
         <day>tuesday</day>
         <day>thurday</day>
       </class>
     </schedule>
  </school>
</root>

我想以一种方式建立连接,当我引用 idref 中的值时,在输出(即 html)上,显示的将是列表中的描述,在开始时。这甚至可能吗?

【问题讨论】:

    标签: xml xpath idref


    【解决方案1】:

    是的。这很容易实现。但输出取决于您的 XPath 版本。
    以下 XPath 表达式

    /root/list/item[@id=/root/school/schedule/class/@idref]/description
    

    返回 XPath-1.0 中的第一项和 XPath-2.0 及更高版本中的一组节点。
    所以输出要么是(没有应用任何特殊的连接)

    1. Class 1 (1.0)
    2. Class 1 Gym-class 1 返回所有满足要求 (2.0+) 的项目。

    如果你想得到某个&lt;item&gt;的描述,你应该把上面表达式中的/root/school/schedule/class/@idref替换成你想从&lt;item&gt;列表中检索的idref值..

    【讨论】:

    • 所以我无法使描述只对应于自动引用的 id,我每次都必须指定 idref?无论如何,谢谢你,它已经帮助了很多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多