In XSLT, many to many attributes comparing in for-

2019-08-14 19:31发布

问题:

Using XSLT 1.0

Is it possible to filter many to many attributes, i mean as below example: "../../../../fieldmap/field[@name" i.e. more then 1 elements as fieldmap containing "field/@name" attribute are exists and it is comparing with definition/@title and there too more then one definition element exists containing @title.

EXAMPLE:

<xsl:for-each select="../../../../fieldmaps/field[@name=../destination/@title]">

Can you please suggest me how it could possible to achieve -- if field containing @name existed in any of defination/@title then only those records should be process within for-each loop? (as now, it looks, it will just compare with first @title attribute and consider all fieldmaps/field/@name attributes)

Thanks

回答1:

You can achieve that using a variable:

<xsl:variable name="titles" select="../destination/@title"/>
<!--now "titles" contains a nodeset with all the titles -->
<xsl:for-each select="../../../../fieldmaps/field[@name=$titles]">
<!-- you process each field with a name contained inside the titles nodeset -->
</xsl:for-each>

Here you have a simplified example:

INPUT:

<parent>
    <fieldmaps>
        <field name="One"/>
        <field name="Two"/>
        <field name="Three"/>
    </fieldmaps>
    <destinations>
        <destination title="One"/>
        <destination title="Two"/>
    </destinations>
</parent>

TEMPLATE:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- ++++++++++++++++++++++++++++++++ -->
    <xsl:template match="parent">
        <Results>
            <xsl:variable name="titles" select="destinations/destination/@title"/>
            <xsl:for-each select="fieldmaps/field[@name=$titles]">
                <Result title="{@name}"/>
            </xsl:for-each>
        </Results>
    </xsl:template>
    <!-- ++++++++++++++++++++++++++++++++ -->
</xsl:stylesheet>

OUTPUT:

<Results>
    <Result title="One"/>
    <Result title="Two"/>
</Results>

I hope this helps!



标签: xslt xslt-1.0