xslt - 这个 XSLT 片段可以更简洁吗?

标签 xslt xpath xslt-1.0

在下面的代码片段中,我有 3 个 for-each,但在我看来它们应该能够合并为一个。它按原样工作,但我想知道是否有人知道更优雅的编写方式?

    <xsl:for-each select="/essentials/webservice">
        <xsl:for-each select="document(@filename)/productSearchResponse/products/product">
            <xsl:sort select="producingRegion" order="ascending"/> 
            <xsl:for-each select="producingRegion[not(preceding::producingRegion=.)]">
                <xsl:value-of select="."/>
                <br/>
            </xsl:for-each> 
        </xsl:for-each>
    </xsl:for-each>

最佳答案

如果不知道输入 XML 的预期结果和结构,很难给出答案,但我相信这应该可行。

<xsl:for-each select="/essentials/webservice">
    <xsl:for-each select="document(@filename)/productSearchResponse/products/product/producingRegion[not(preceding::producingRegion=.)]">
            <xsl:value-of select="."/>
            <br/>
    </xsl:for-each>
</xsl:for-each>

编辑:我刚刚注意到 document(@filename) 是函数调用,而不是节点测试。在这种情况下,我认为您需要两个 for-eaches。

您可以通过在 XPath 表达式中使用双斜杠来使其更加简洁,但我倾向于认为这不是一个很好的做法:

<xsl:for-each select="/essentials/webservice">
    <xsl:for-each select="document(@filename)//producingRegion[not(preceding::producingRegion=.)]">
            <xsl:value-of select="."/>
            <br/>
    </xsl:for-each>
</xsl:for-each>

关于xslt - 这个 XSLT 片段可以更简洁吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162242/

相关文章:

html - 使用 XPath 选择两个节点之间的兄弟节点

xslt - XSLT : How to reorder entries according to the dependencies 的 XSD 转换

css - 按字母顺序水平和垂直对齐文本

xml - 如何在 Xpath "contains()"函数中使用变量节点集

html - 如何使用XSLT在XML元素中获取所有内容

performance - XSLT 性能

xml - 在 XSLT 中添加/减去时间

xml - 在xsl tokenize中,如何获取前面的文本值

xml - XPATH表达式,用于检索xml的根标签而不是该标签的值

python - 在 scrapy 中,我试图检索链接列表,然后在单个 scrapy 文件中从这些链接中抓取数据。我的 csv 文件返回 xpath 列表。