gpt4 book ai didi

xml - 如何使用 XSLT 生成以逗号分隔的列表

转载 作者:数据小太阳 更新时间:2023-10-29 03:00:40 26 4
gpt4 key购买 nike

我试图在 ms Access 中导入带有 XSLT 的 XML,以便它以逗号分隔的列表导入
我的 XML:

<entry>
<title>Adobe</title>
<description>Adobe Acrobat</description>
<title>Adobe1</title>
<description>Adobe Acrobat1</description>
<title>Adobe2</title>
<description>Adobe Acrobat2</description>
</entry>

我的尝试:

<xsl:for-each select="entry">
<xsl:if test="position() > 1">, </xsl:if>
<xsl:value-of select="title"/>
</xsl:for-each>

预期结果:Adobe、Adobe1、Adobe2

最佳答案

您的 XML 只有一个 entry ,因此 xsl:for-each 中的代码只会运行一次。做<xsl:value-of select="title" />只会选择第一个 title在里面entry (假设 XSLT 1.0,即)

改成这样...

<xsl:for-each select="entry/title">
<xsl:if test="position() > 1">, </xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>

请注意,在 XSTL 2.0 中,您可以将上面的代码片段完全替换为...

 <xsl:value-of select="entry/title" separator="," />

编辑:假设您的 XSLT 看起来像这样......

<iavmNotice xmlns="http://stuff.com" noticeId="138643">
<title>Cisco Vulnerability</title>
<techOverview>
<entry>
<title>2012-2490</title>
<description>Cisco ID 71.</description>
</entry>
<entry>
<title>2012-2525</title>
<description>Cisco ID 69.</description>
</entry>
</techOverview>
</iavmNotice>

然后,使用xsl:for-each在上下文中,您可以像这样添加一个模板:

<xsl:template match="stuff:techOverview">
<xsl:copy>
<xsl:for-each select="stuff:entry/stuff:title">
<xsl:if test="position() > 1">, </xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>

请注意以下事项:

  • 确保 stuff前缀根据输入 XML 绑定(bind)到正确的命名空间
  • 确保您没有其他模板匹配 stuff:techOverview

关于xml - 如何使用 XSLT 生成以逗号分隔的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42034187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com