locked
Using XSL to access and modify elements from a predefined array of element names RRS feed

  • Question

  • Pretty new to XML/XSL. I need to process an XML file, using XSL through Access VBA.
    In the .xsl I have defined the elements I want to target. Everything works ok to the point where I want to replace:

    <xsl:template match="element_1"> 

    with

    <xsl:template match="$variable">
    ... which is illegal in xsl 1.0. I want to use a variable array member as part of a comparision.
    Here is my code this far:

    The XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <test> 
    <element_1>111</element_1>
    <element_2>222</element_2>
    <element_3>333</element_3>
    </test>
    
    The XSL:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      version="1.0">
    
    <!-- Make a raw RTF (result tree fragment) -->
      <xsl:variable name="elements">
        <elem name="element_1" />
        <elem name="element_2"/>
        <elem name="element_3"/>
      </xsl:variable>
    
    <!-- Convert to usable node-set (array variable) -->  
      <xsl:variable name="theElements" select="msxsl:node-set($elements)/elem"/>
    
    <!-- Do the copy of the DOM -->
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
    <!-- Here I need to match each of the elements in the array theElements -->
    <!-- and perform operations on them, like adding an attribute -->
    <!-- <xsl:template match="$variable"> is illegal -->
    <!-- Code below selects the values in theElements in order, but how to use each selected value to match each element? -->
    	<xsl:for-each select="$theElements"> 
          <xsl:value-of select="@name"/> <!-- element_1, element_2 etc ... -->
      </xsl:for-each>
    </xsl:stylesheet>


    Best regards, George



    Tuesday, August 30, 2016 4:56 PM

All replies

  • Pretty new to XML/XSL. I need to process an XML file, using XSL through Access VBA. It's working up to a point. Keep reading. 
    In the .xsl I have defined the elements I want to target. Everything works ok to the point where I want to replace:

    <xsl:template match="element_1"> 

    with

    <xsl:template match="$variable">
    ... which is illegal in xsl 1.0. Here is my code this far:

    The XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <test> 
    <element_1>111</element_1>
    <element_2>222</element_2>
    <element_3>333</element_3>
    </test>
    
    The XSL:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      version="1.0">
    
    <!-- Make a raw RTF (result tree fragment) -->
      <xsl:variable name="elements">
        <elem name="element_1" />
        <elem name="element_2"/>
        <elem name="element_3"/>
      </xsl:variable>
    
    <!-- Convert to usable node-set (array variable) -->  
      <xsl:variable name="theElements" select="msxsl:node-set($elements)/elem"/>
    
    <!-- Do the copy of the DOM -->
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
    <!-- Here I need to match each of the elements in the array theElements -->
    <!-- and perform operations on them, like adding an attribute -->
    <!-- <xsl:template match="$variable"> is illegal -->
    <!-- Code below selects the values in theElements in order, but how to use each selected value to match each element? -->
    	<xsl:for-each select="$theElements"> 
          <xsl:value-of select="@name"/> <!-- element_1, element_2 etc ... -->
      </xsl:for-each>
    </xsl:stylesheet>


    Best regards, George




    Tuesday, August 30, 2016 5:36 PM
  • Hi George.B.Summers,

    According to your description, since this is the forum to discuss questions and feedback for Access for Developers, and I have found that you have post same thread on MSDN forum for XML, System.Xml, MSXML and XmlLite. I'll merge your question to below link:

    https://social.msdn.microsoft.com/Forums/office/en-US/b17cfef7-bcd2-4977-8399-6f51e2f542c5/using-xsl-to-access-and-modify-elements-from-a-predefined-array-of-element-names?forum=xmlandnetfx

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, 
    and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. 

    Thanks for your understanding.
    Wednesday, August 31, 2016 5:18 AM
  • Hi George.B.Summers,

    From the following thread, we know that With XSLT 1.0 (which is all what Microsoft supports with its XSLT processors) does not support using variable or parameter references (i.e. $var-name, $param-name) in XSLT match patterns.

    If you want to use an XSLT 1.0 processor then you will need to change your stylesheet to move the comparison with the parameter into the template

    https://social.msdn.microsoft.com/Forums/en-US/e432852b-6054-48bb-bd67-30fb49707969/variables-cannot-be-used-within-this-expression-xsltemplate-match-attribute?forum=xmlandnetfx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, August 31, 2016 5:41 AM
    Moderator
  • Thanks but that answer only suggested how to use a variable as output, not as part of a comparision

    Best regards, George

    Wednesday, August 31, 2016 8:09 AM