XSL1.0:在重复中选择特定元素
Posted
技术标签:
【中文标题】XSL1.0:在重复中选择特定元素【英文标题】:XSL1.0 : Selecting specific elements in repeating 【发布时间】:2021-12-12 00:14:39 【问题描述】:拥有这样的 XML:
<categories>
<request type="this request is B and O">
<list name="B"/>
<list name="O"/>
</request>
<request type="this request is N only">
<list name="N"/>
</request>
<request type="this request is O and E">
<list name="O"/>
<list name="E"/>
</request>
<request type="this request is O and G">
<list name="O"/>
<list name="G"/>
</request>
<request type="this request is N and E">
<list name="N"/>
<list name="E"/>
</request>
<request type="this request is E only">
<list name="E"/>
</request>
<request type="this request is B only">
<list name="B"/>
</request>
<request type="this request is B and N">
<list name="N"/>
<list name="B"/>
</request>
</categories>
我尝试如下:
<xsl:template name="simplified">
<xsl:for-each select="categories/request[child::list/@name = 'B'] | categories/request[child::list/@name = 'O'] | categories/request[child::list/@name = 'N']">
<fo:block>
<xsl:value-of select="@type"/>
</fo:block>
</xsl:for-each>
</xsl:template>
规则是:
在所有请求后重复,其 类型的子级将仅具有名称 B、O 或 N; 如果一个现在我收到以下结果:
this request is B and O
this request is N only
this request is O and E
this request is O and G
this request is N and E
this request is B only
this request is B and N
我需要收到以下正确结果:
this request is B and O
this request is N only
this request is B only
this request is B and N
如何更新 xsl1.0 的重复模板?
【问题讨论】:
【参考方案1】:我很难理解您的描述。看来你想做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/categories">
<xsl:for-each select="request[not(list[contains('GE', @name)])]">
<xsl:value-of select="@type"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
或者 - 如果您更喜欢白名单而不是黑名单:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/categories">
<xsl:for-each select="request[not(list[not(contains('BON', @name))])]">
<xsl:value-of select="@type"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
【讨论】:
以上是关于XSL1.0:在重复中选择特定元素的主要内容,如果未能解决你的问题,请参考以下文章