简体   繁体   中英

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?

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), ie SIM_MPLD and etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM