1

I did this xslt, it grabs a value from a XML, I have parameters that I can send to the for each to get what I need, it works with no problem, my for each do a comparison and if the value matches my parameter then it's done.

Does anybody knows if that is a good way to do it ? I'm telling that because I'm new to XSLT and that's the way I figured out that might work with no problem. I wonder if a function would be a better solution, in terms of coding and speed.

Cheers!

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="TransactionDataOfRequest">
  <xsl:output method="text" indent="no"/>
  <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:param name="param1" select="'ADM_1'"/>
    <xsl:param name="param2" select="'ADM_2'"/>
    <xsl:for-each select="//x:form/x:add">
      <xsl:if test="@name=$param1">
        Question1=<xsl:value-of select="."/>
      </xsl:if>
    </xsl:for-each>

    <xsl:for-each select="//x:form/x:add">
      <xsl:if test="@name=$param2">
        <xsl:variable name="test">
          <xsl:value-of select="."/>
        </xsl:variable>
        Question2=<xsl:copy-of select="$test" />
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="iso-8859-1"?>
<transaction xmlns="TransactionDataOfRequest" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <success>true</success>
  <code>0</code>
  <value>
    <form>
      <add name="ADM_1" title="Question 1" type="String" isList="false">No</add>
      <add name="ADM_2" title="Question 2" type="String" isList="false">Yes</add>
    </form>
  </value>
</transaction>

1 Answer 1

1

Assuming you want to print out all the questions and answers, you could do this generically with a simple apply template:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="TransactionDataOfRequest">
    <xsl:output method="text" indent="no"/>
    <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:apply-templates select="//x:form/x:add"></xsl:apply-templates>
    </xsl:template>

    <xsl:template match="x:add">
        <xsl:value-of select="@title"/>=<xsl:value-of select="."/>
        <xsl:text>&#xa;</xsl:text>
    </xsl:template>

</xsl:stylesheet> 

Edit

You can do this imperatively and and still DRY this up I guess:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="TransactionDataOfRequest">
    <xsl:output method="text" indent="no"/>
    <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:call-template name="processQuestion">
            <xsl:with-param name="name" select="'ADM_1'"></xsl:with-param>
        </xsl:call-template>
        <xsl:call-template name="processQuestion">
            <xsl:with-param name="name" select="'ADM_2'"></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="processQuestion">
        <xsl:param name="name"></xsl:param>
        <xsl:apply-templates select="//x:form/x:add[@name=$name]" mode="process" />
    </xsl:template>

    <xsl:template match="x:add" mode="process">
        <xsl:value-of select="@title"/>=<xsl:value-of select="."/>
        <xsl:text>&#xa;</xsl:text>
    </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi StuartLC, thanks for your reply, I need to manipulate the elements later, so I need to get them separately. That's why I'm using parameters.
I've updated with explicit calls to a call template (function). You can still avoid the for-each, the manual loop matching and the local variable.
Thanks Stuart, that's the solution I've been looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.