0

I've been trying to get a value from a XML and create this output:

OUTPUT1=Question1=No&Question2=Yes

My current output is:

    Question1=No
    Question2=Yes

I don't know the best approach to do this, I'm new to XSLT and I was thinking to store these values into a variable and use them later like: OUTPUT1=Variable1&Variable2

But even that it is a challenge to me, can someone help me with directions ?

XSLT

<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>

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>

Variable

    <xsl:variable name="test">
      <xsl:value-of select="."/>
    </xsl:variable>

I've also tried this:

<?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>

That was my first attempt, in fact I was able to save the value into a variable, but that variable only exist inside that template right?

2 Answers 2

1

How about doing it the simple way?

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tx="TransactionDataOfRequest">

<xsl:output method="text" encoding="utf-8"/>

<xsl:template match="/">
    <xsl:text>OUTPUT1=</xsl:text>
    <xsl:for-each select="tx:transaction/tx:value/tx:form/tx:add[@name='ADM_1' or @name='ADM_2']">
        <xsl:value-of select="@title"/>
        <xsl:text>=</xsl:text>
        <xsl:value-of select="."/>
        <xsl:if test="position()!=last()">
            <xsl:text>&amp;</xsl:text>
        </xsl:if>
    </xsl:for-each>

</xsl:template>

</xsl:stylesheet>

Applied to the following example input:

<?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="XCL_3" title="Question 3" type="String" isList="false">Yes</add>
        <add name="ADM_2" title="Question 2" type="String" isList="false">Yes</add>
    </form>
    </value>
</transaction>

produces the result::

OUTPUT1=Question 1=No&Question 2=Yes

I am not sure if you need to remove the spaces in the original names.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Michael, thanks for your reply, it's because I don't know the order of questions I might have to combine two questions ie the first with the latest. So I need a way to tell what I need to get. That's why I have processQuestion in the first xslt and the for each with an if as a filter.
@CelsoCatarinoNeto Where are the values ADM_1 and ADM_2 coming from? Are they supposed to be hard-coded in the stylesheet?
Yes they come from the XML, they are hardcoded because that's how I select what I want from the XML.
1

Why store the values in variables? Is there a need for it?

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="TransactionDataOfRequest">
<xsl:output method="text" indent='yes'/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
    <xsl:variable name="temp">
        <xsl:value-of select="for $var in //x:form/x:add return concat(translate($var/@title,' ',''),$var)" separator="&amp;"/>
    </xsl:variable>
    <xsl:value-of select="concat('OUTPUT1=',$temp)"/>
</xsl:template>
</xsl:stylesheet>

2 Comments

I'm on Visual Studio and I'm getting: XslTransformException --------------------- Expected end of the expression, found '$'.
I'm not sure, but I don't think VS 2013 .NET 4.5 supports xslt 2.0

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.