Alejandro DC
Alejandro DC

Reputation: 236

Match attribute name in xslt version 1.0

I have this XML file

<REFERENCES>
    <COMPLETE REFERENCE="ABC1" />
    <CODIGO_SIM REFERENCE="ABC1" />
    <ETAPA REFERENCE="C1" />
    <NPP_SERIE REFERENCE="NAOBI" />
    <SIM_MPLD REFERENCE="METALIC" />
    <SIM_COCI REFERENCE="NOTTE" />
    <SIM_ANCH REFERENCE="563" />
    <NP_TIPO REFERENCE="Placard" />
</REFERENCES>

I want to filer only those entries starting with SIM_, but I couldn't, so I've modified the file adding the name of the attribute to its value, like this:

<REFERENCES>
    <COMPLETE REFERENCE="ABC1" />
    <CODIGO_SIM REFERENCE="ABC1" />
    <ETAPA REFERENCE="C1" />
    <NPP_SERIE REFERENCE="NAOBI" />
    <SIM_MPLD REFERENCE="SIM_MPLD|METALIC" />
    <SIM_COCI REFERENCE="SIM_COCCI|NOTTE" />
    <SIM_ANCH REFERENCE="SIM_ANCH|563" />
    <NP_TIPO REFERENCE="Placard" />
</REFERENCES>

and so I can filter those entries with this:

<xsl:variable name="CodigoSim" select="REFERENCES/CODIGO_SIM/@REFERENCE" />
    <xsl:for-each select="REFERENCES//.//@REFERENCE" >
        <xsl:if test="starts-with(., 'SIM_')" >
            <xsl:value-of select="$CodigoSim"/>
            <xsl:text>|</xsl:text>
            <xsl:value-of select=" substring-after(., 'SIM_')" />
            <xsl:value-of select="substring-after(., '|')" />
            <xsl:call-template name="QuebrarLinha"/>
       </xsl:if>
     </xsl:for-each>

which does exactly what I want. Is there anyway to obtain the same behaviour without changing the XML file?

Upvotes: 0

Views: 60

Answers (1)

Rudolf Yurgenson
Rudolf Yurgenson

Reputation: 603

If you need to select elements using some rule related to element's name then you can use something like this:

<xsl:for-each select="REFERENCES/*[starts-with(local-name(), 'SIM_')]">
  <xsl:value-of select="$CodigoSim"/>
  ...
</xsl:for-each>

local-name() is the part element's name that comes after namespace (if any), i.e. SIM_MPLD and etc.

Upvotes: 1

Related Questions