Logic for passing variable number of parameters to XSLT

那年仲夏 提交于 2021-01-28 19:53:09

问题


I need to develop an application that will periodically check data from a XML feed , process it and take appropriate actions like notifying the users etc but mostly used to generate and view a report. This xml feed basically checks the uptime, downtime of applications.

Here are the steps that i am following:

  1. I am downloading a xml file from a remote location to my own server.
  2. Check which applications to monitor from a properties file .
  3. Transform it to html using a XSLT and mail the html page.

Now this XML feed also contains applications that need not be monitored and is checked using the properties file in java. The application that is monitored can be added later on.

So is there any way to have this included in the xsl file as well? (I can pass parameters from java to xslt as well , but the number of params will be variable. I am grouping the app names and passing it using two params , but stuck because these values cannot be compared using the xsl:for-each iterator of the XML feed file.)

EDIT:

<begin last-update="12/16/2011 06:18:31 am">
    <application id="1" name="xyz" last-update="12/16/2011 6:16:03 AM">
        <node2 code="normal">
            <childnodes></childnodes>
            <childnodes></childnodes>
        </node2>

    </application>
    <application id="2" name="abc" last-update="12/14/2011 6:16:03 AM">
        ... Same as before
</application>
</begin>

This is the xml feed . i am iterating through this xml ..i need to check the name attribute in the application node .. below is the xsl code

<xsl:for-each select="begin/application">
<!-- Need this to be fetched dynamically using xsl params -->
<xsl:if test="@name='abc' or @name=xyz' >
<tr width="100%" style="border:solid 1px black;">
<td  style="text-align:center; vertical-align:center;">DATA</td>
<td  style="text-align:center; vertical-align:center;">DATA</td>
<td  style="text-align:center; vertical-align:center;">DATA</td>
</tr>
</xsl:if>

Currently name attributes are hardcoded .. i need to use these from the parameters list passed from the java code- the parameter string being app1,app2 and parameters in node set as

<root>
<application @name="ABC"></application>
<application @name="XYZ"></application>
</root>

The question is how do i iterate through the params , as i would also need the iteration for the xml file.


回答1:


Why don't you use JAXB to provide you with a Java representation of your XML data. Then you can playaround with the data in your Java code using your properties file (or whatever may replace your properties file with in the future, like a database table), and then just create your HTML output with a JSP/Velocity/<insert favourite view tech here>

This will probably be a more flexible design than trying to shoe horn everything into XSL




回答2:


The way to pass "multiple parameters" to a transformation is to pass a single parameter, that is an XML element (tree), whose children elements will be processed and treated as the actual (logical) parameters by the transformation.

Pass something like this:

<params>
 <param name="p1"> some subtree/<param>

.  .  .  .  

 <param name="pk"> some subtree/<param>
<params>



回答3:


Ok, so I was able to do this. Instead of passing parameters, I created an XML file and using the document function read that file in a template. This template was called inside from the for-each of the main document. Here, I passed parameters to the template, iterated through the XML file and did whatever I was supposed to do.

Uhm , in case someone is stuck in a stupid situation like me, where they did not think through before investing a significant amount of time. JAXB is the best way to go through. I did implement the JAXB solution and its quite simple. But, since the xslt is currently satisfying my requirement I am going with XSLT.

XSLT Code:

<xsl:for-each select="begin/application">
<xsl:call-template name="doSome">
    <xsl:with-param name="appname" select="@name" />
</xsl:call-template>
<xsl:template name="doSome">
    <xsl:param name="appname" />
    <xsl:for-each select="document('file:///E:/LOG/list.xml')/root/application">
        <xsl:if test="@name=$appname">                  
            ... do whatever you want to do
        </xsl:if>
     </xsl:for-each>
</xsl:template>


来源:https://stackoverflow.com/questions/8535409/logic-for-passing-variable-number-of-parameters-to-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!