Jump to content

Xslt Param Or Creating If Template.


Scrier
 Share

Recommended Posts

Hello.I'm currently trying to make c++ code from xsl / xslt and making if cases from subelements and have made it to a certain extent so far but i'm currently stuck at one part.The xml is built up in the following way:

<element namespace="4014" classname="002" id="_3">	<element namespace="4004" classname="012" id="_3_1" /></element><element namespace="4014" classname="001" id="_4">	<element namespace="281" classname="431" id="_4_1" />	<element namespace="282" classname="431" id="_4_2" />	<element namespace="4130" classname="007" id="_4_3" /></element><element namespace="4014" classname="001" id="_5">	<element namespace="4014" classname="002" id="_5_1">		<element namespace="4028" classname="001" id="_5_1_1" />	</element>	<element namespace="4014" classname="002" id="_5_2">		<element namespace="365" classname="401" id="_5_2_1" />	</element>	<element namespace="4014" classname="002" id="_5_3">		<element namespace="4028" classname="005" id="_5_3_1" />	</element>	<element namespace="4014" classname="002" id="_5_4">		<element namespace="4037" classname="002" id="_5_4_1" />	</element></element>

and the xsl is in the following manner:

<!-- loop through all elements and Delete this function --><xsl:template match="sequence" mode="sourceMandatory">	<xsl:for-each select="element">		<xsl:apply-templates select="." mode="sourceMandatoryElement" />	</xsl:for-each></xsl:template><!-- find each namespace element --><xsl:template match="element" mode="sourceMandatoryElement">	<xsl:choose>		<xsl:when test="@namespace = 4014">			<xsl:apply-templates select="." mode="sourceMandatoryElementIf" />		</xsl:when>		<xsl:when test="@namespace = 4045">				</xsl:when>	</xsl:choose></xsl:template><!-- handle if cases --><xsl:template match="element" mode="sourceMandatoryElementIf">	<xsl:variable name="testcase" select="' '" />	<xsl:for-each select="./element">		<xsl:choose>			<xsl:when test="@namespace = 4014">				<xsl:apply-templates select="." mode="sourceMandatoryElementIf" />			</xsl:when>			<xsl:otherwise>				<xsl:choose>					<xsl:when test="position() = 1 and position() = last()">						<xsl:text>if( </xsl:text><xsl:value-of select="@id" /><xsl:text>->isValueSet() )</xsl:text>					</xsl:when>					<xsl:when test="position() = 1">						<xsl:text>if( </xsl:text><xsl:value-of select="@id" /><xsl:text>->isValueSet()</xsl:text>					</xsl:when>					<xsl:when test="position() = last()">					 <xsl:text> || </xsl:text><xsl:value-of select="@id" /><xsl:text>->isValueSet() )</xsl:text>					</xsl:when>					<xsl:otherwise>						<xsl:text> || </xsl:text><xsl:value-of select="@id" /><xsl:text>->isValueSet()</xsl:text>					</xsl:otherwise>				</xsl:choose>			</xsl:otherwise>		</xsl:choose>	</xsl:for-each>	<xsl:text>{</xsl:text>	<xsl:choose>		<xsl:when test="@classname = 001">			<xsl:value-of select="@id" /><xsl:text>->setValue(namespace_4014::classname_001::PRESENT);</xsl:text>			<xsl:text>}</xsl:text>			<xsl:text>else</xsl:text>			<xsl:text>{</xsl:text>			<xsl:value-of select="@id" /><xsl:text>->setValue(namespace_4014::classname_001::NOT_PRESENT);</xsl:text>		</xsl:when>		<xsl:otherwise>			<xsl:value-of select="@id" /><xsl:text>->setValue(namespace_4014::classname_002::PRESENT);</xsl:text>			<xsl:text>}</xsl:text>			<xsl:text>else</xsl:text>			<xsl:text>{</xsl:text>			<xsl:value-of select="@id" /><xsl:text>->setValue(namespace_4014::classname_002::NOT_PRESENT);</xsl:text>		</xsl:otherwise>	</xsl:choose>	<xsl:text>}</xsl:text></xsl:template>

And the output looks fine, i.e:

if( _3_1->isValueSet() ){_3->setValue(DFI_4014::DUI_002::PRESENT);}else{_3->setValue(DFI_4014::DUI_002::NOT_PRESENT);}if( _4_1->isValueSet() || _4_2->isValueSet() || _4_3->isValueSet() ){_4->setValue(DFI_4014::DUI_001::PRESENT);}else{_4->setValue(DFI_4014::DUI_001::NOT_PRESENT);}if( _5_1_1->isValueSet() ){_5_1->setValue(DFI_4014::DUI_002::PRESENT);}else{_5_1->setValue(DFI_4014::DUI_002::NOT_PRESENT);}

But then when I try to get the following code

if( _5_1->isValueSet() || _5_2->isValueSet() || _5_3->isValueSet() || _5_4->isValueSet() ){_5->setValue(DFI_4014::DUI_002::PRESENT);}else{_5->setValue(DFI_4014::DUI_002::NOT_PRESENT);}

i.e the ones with elements under elements I have some issues, saving in a variable or adding it in a new template call won't work, Kinda stuck on this as it seems it wont save in xsl:when templates. Any help appreciated and ask if clarification is needed as im not sure what i want to achieve is clarified as good as I wan't to.

Link to comment
Share on other sites

I found out how to manage this with some trial and error.

<!-- find each namespace element --><xsl:template match="element" mode="sourceMandatoryElement">	<xsl:choose>		<xsl:when test="@namespace = 4014">			<xsl:apply-templates select="./element" mode="sourceMandatoryElement" />			<xsl:apply-templates select="." mode="sourceMandatoryElementIf" />		</xsl:when>		<xsl:when test="@namespace = 4045">				</xsl:when>	</xsl:choose></xsl:template><!-- handle if cases --><xsl:template match="element" mode="sourceMandatoryElementIf">	<xsl:variable name="testcase" select="' '" />	<xsl:for-each select="./element">				<xsl:choose>					<xsl:when test="position() = 1 and position() = last()">						<xsl:text>if( </xsl:text><xsl:value-of select="@id" /><xsl:text>->isValueSet() )</xsl:text>					</xsl:when>					<xsl:when test="position() = 1">						<xsl:text>if( </xsl:text><xsl:value-of select="@id" /><xsl:text>->isValueSet()</xsl:text>					</xsl:when>					<xsl:when test="position() = last()">					 <xsl:text> || </xsl:text><xsl:value-of select="@id" /><xsl:text>->isValueSet() )</xsl:text>					</xsl:when>					<xsl:otherwise>						<xsl:text> || </xsl:text><xsl:value-of select="@id" /><xsl:text>->isValueSet()</xsl:text>					</xsl:otherwise>				</xsl:choose>	</xsl:for-each>	<xsl:text>{</xsl:text>	<xsl:choose>		<xsl:when test="@classname = 001">			<xsl:value-of select="@id" /><xsl:text>->setValue(namespace_4014::classname_001::PRESENT);</xsl:text>			<xsl:text>}</xsl:text>			<xsl:text>else</xsl:text>			<xsl:text>{</xsl:text>			<xsl:value-of select="@id" /><xsl:text>->setValue(namespace_4014::classname_001::NOT_PRESENT);</xsl:text>		</xsl:when>		<xsl:otherwise>			<xsl:value-of select="@id" /><xsl:text>->setValue(namespace_4014::classname_002::PRESENT);</xsl:text>			<xsl:text>}</xsl:text>			<xsl:text>else</xsl:text>			<xsl:text>{</xsl:text>			<xsl:value-of select="@id" /><xsl:text>->setValue(namespace_4014::classname_002::NOT_PRESENT);</xsl:text>		</xsl:otherwise>	</xsl:choose>	<xsl:text>}</xsl:text></xsl:template>

What it does is that you check a state for all underlying elements and depending if its true or false we set a value of the overlying element.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...