Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does xml::xslt support an xpath method to select an xml node with an attribute that has spaces in the value? Here is a snippet of my xml file:
... <Form id="id24" name="160D999998-98/01" accessRefs="#id3" subType="Par +t Revision Master" subClass="Part Revision Master"> <Form id="id30" name="09A10-000019/A-AF Engineering Or" accessRefs="#i +d3" subType="AF Engineering Order E" subClass="AF Engineering Order E +"> <Form id="id33" name="09A10-000019/A-AF Design/Draftin" accessRefs="#i +d3" subType="AF Design/Drafting Request A" subClass="AF Design/Drafti +ng Request A"> <Form id="id36" name="09A10-000019/A" accessRefs="#id3" subType="EngCh +ange Revision Master" subClass="EngChange Revision Master"> ...
Of these three Form nodes I'd like to select the one with subType="Part Revision Master" Here are my failed approaches:
<xsl:value-of select= "/PLMXML/Form@subType='Part Revision Master']/UserData/UserValue[@tit +le='cage_code']/@value"/> <!-- also tried --> <xsl:strip-space elements="*"/> <xsl:value-of select= "/PLMXML/Form@subType='PartRevisionMaster']/UserData/UserValue[@title +='cage_code']/@value"/> <!-- also tried --> <xsl:value-of select= "/PLMXML/Form@subType='Part#x20Revision#x20Master']/UserData/UserValu +e[@title='cage_code']/@value"/> <!-- also tried --> <xsl:for-each select="PLMXML/Form"> <xsl:variable name="formtype"> <xsl:value-of select="substring(@subType,1,4)"/> </xsl:variable> <xsl:if test="$formtype ='Part')"> <xsl:value-of select= "/PLMXML/Form/UserData/UserValue[@title='cage_code']/@value"/> </xsl:if> </xsl:for-each> <!-- also tried --> <xsl:for-each select="PLMXML/Form"> <xsl:variable name="formtype"> <xsl:value-of select="substring"/> </xsl:variable> <xsl:value-of select="$formtype"/> <xsl:if test="contains($formtype,'Part')"> <xsl:value-of select= "/PLMXML/Form/UserData/UserValue[@title='cage_code']/@value"/> </xsl:if> </xsl:for-each>

Replies are listed 'Best First'.
Re: xml::xslt xpath question
by ikegami (Patriarch) on Jul 06, 2009 at 19:29 UTC

    <xsl:value-of select="/PLMXML/Form@subType='Part Revision Master']/UserData/UserValue[@title='cage_code']/@value"/>

    Missing [, the Form doesn't have a UserData child, and I can't verify from what you've shown that the Form has the right ancestry.

    <xsl:strip-space elements="*"/><xsl:value-of select="/PLMXML/Form@subType='PartRevisionMaster']/UserData/UserValue[@title='cage_code']/@value"/>

    Missing [, the Form doesn't have a UserData child, and I can't verify from what you've shown that the Form has the right ancestry.

    <xsl:value-of select="/PLMXML/Form@subType='Part#x20Revision#x20Master']/UserData/UserValue[@title='cage_code']/@value"/>

    Missing [, the Form doesn't have a UserData child, and I can't verify from what you've shown that the Form has the right ancestry.

    I can't comment the later snippets since I know XPath, not XSLT.

      I'll add the full xml below but my biggest challenge is in finding the xpath methods that are available in Perl to select a node by its attributute value - when the attribute value includes space.

        Simplified XML:

        <?xml version="1.0" encoding="utf-8"?> <PLMXML xmlns="http://www.plmxml.org/Schemas/PLMXMLSchema"> <Form subType="Part Revision Master"> <UserData> <UserValue title="cage_code" value="98747"/> </UserData> </Form> </PLMXML>

        You keep mentioning spaces, but I don't see why that would be a problem.

        Did you try fixing the the missing "[" I mentioned? If the first snippet doesn't work with the missing "[ fixed, I suspect a namespace problem. I don't know how XSLT deals with namespaces, so I can't help you there.

        Outside of XSLT, your XPath would be wrong since it references elements in the null namespace while they are in the http://www.plmxml.org/Schemas/PLMXMLSchema namespace.