0

In the database, the user has created a number of properties depending on his needs. An XML generated on the server-side includes all the properties from the database and the data they hold. The report generated, by Microsoft's XSLT processor, includes a sub-set of these properties based on the user's selections (via a list of checkboxes in the UI).

<xml ...>
    <property id="01" title="First Property" value="Beauty of XSLT"/>
    <property id="02" title="Second Property" value="Please Help"/>
    <property id="XX" title="Variable Number" value="Use Global Variables"/>
</xml>

Previously, I had a fixed number of properties which had pre-defined names. I used global variables in the XSL file and passed their values (on or off) to the XSLT processor. Now, both of these, the name of the properties and their number, are user defined.

Somehow, I need to pass the list of properties to show to the XSLT processor. I am not sure how. Any suggestion or guidance is appreciated.

EDIT -- adding clarification:

The XML, generated on the server-side, includes all properties regardless of the user selection. The transformation is done on the client-side based on the user selection of what properties to display. The user may change this selection however we do NOT re-generate the XML. We simply pass different values to the XSLT processor, as global variables. When these properties are fixed, the global variables look like this: g_property_id-01 and it's set to true, display, or false, do not display. Now, I don't know the number/names of the user-created properties; hence my problem.

EDIT -- adding XSLT as requested:

<?xml version="1.0"?>

<xsl:stylesheet ...>
...
<!-- This XSL Template is used to transform report results into HTML. -->

  <xsl:param name="g_bShow_Property01" select="1"/>
  <xsl:param name="g_bShowUser" select="1"/>
    ...

  <xsl:template match="Result">
    <xsl:variable name="g_bShowHeader" select="$g_bShowUser=1 or $g_bShow_Property01=1"/>
    ...
    <!-- Should we show the Property with 01 id? -->
      <xsl:if test="$g_bShow_Property01 = 1 and ./Document/Property02Value">
        <xsl:variable name="FixedUpProperty">
          <xsl:call-template name="fixup-text">
            <xsl:with-param name="fixup-string" select="./Document/Property02Value"/>
          </xsl:call-template>
        </xsl:variable>
    </xsl:if>

</xsl:stylesheet>
Sqandr
  • 75
  • 1
  • 7

2 Answers2

0

You probably want to refactor your XSLT to use a for-each loop, like so:

<xsl:for-each select="/xml/property">
  ... process the property ...
</xsl:for-each>
Dan Field
  • 20,885
  • 5
  • 55
  • 71
0

Assuming MSXML:

I see two possible approaches, pass an XML document as the parameter with all names, that should be possible as long as you run the processor using its API and some programming language (and not from the command line). So with MSXML and JScript you would do:

var propDoc = new ActiveXObject('Msxml2.DOMDocument.3.0');
propDoc.async = false;
if (propDoc.load('propfile.xml')) {
  // now pass the doc as a parameter to your stylesheet 
  ...
  xsltProc.addParameter('propDoc', propDoc);
}
else {
  // handle parse error here
}

The addParameter method is documented at https://msdn.microsoft.com/en-us/library/ms762312%28v=vs.85%29.aspx.

In your stylesheet you would then use e.g.

<xsl:param name="propDoc" select="/.."/>

and then you can process $propDoc//property.

A different approach is to write two stylesheets, one that first processes your property list and then creates a second with all parameters as needed. You then run the created stylesheet is a second, separate step. I will not spell out this approach, other than saying that XSLT can create XSLT code as shown in http://www.w3.org/TR/xslt#literal-result-element.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you @martin-honnen. First, based on [List of MS XML parser versions](http://support.microsoft.com/kb/269238), I believe I'm using version **3.0**, and it's through a COM API. Second, I'm new to XML processing so I wouldn't be offended if you provide more details regarding the two approaches you suggest. – Sqandr Mar 04 '15 at 16:54
  • The XML, generated on the server-side, includes all properties regardless of the user selection. The transformation is done on the client-side based on the user selection of what properties to display. The user may change this selection however we do NOT re-generate the XML. We simply pass different values to the XSLT processor, as global variables. When these properties are fixed, the global variables look like this: `g_property_id-01` and it's set to `true`, display, or `false` do not display. Now, I don't know the number/names of the user-created properties; hence my problem. Thanks @martin – Sqandr Mar 04 '15 at 17:59
  • Can you edit your post and show us the XSLT that needs to use a property like ``? – Martin Honnen Mar 04 '15 at 18:40