Hi,

Looking at this xml:

<root>
  <nodeA />
  <nodeB />
</root>

Is it then possible to set the value of nodeB when inside nodeA? For example:

<xsl:template match="nodeA">
  <!-- Here do something like nodeB.Text = "hi" -->
</<xsl:template>

So that the output of matching nodeA could be (the thing to notice is the content of nodeB):

<root>
  <nodeA />
  <nodeB>hi</nodeB>
</root>

?

--
Werner

Recommended Answers

All 3 Replies

Hi,

Well not surprisingly I just learned that this isn't possible. But let me examplify my problem. Perhaps you see some solution, consider this xml:

<root id="1">
    <node1>
      <node1_1 />
    </node1>
</root>

And this xslt:

<xsl:stylesheet
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 version="1.0">
<xsl:template match="@*|node()">
  <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
 </xsl:template>
<xsl:template match="*[not(descendant-or-self::*[text()[normalize-space()] | @*])]">
   <xsl:message>Node <xsl:value-of select="local-name()"/> removed</xsl:message>
</xsl:template>
</xsl:stylesheet>

The result is 1 message:
"Node node1 removed"

However I need also to register that node1_1 was removed. So that I can write the information about what the xslt removed from the input to some log. The input xml could be any valid xml.

--
Werner

Something like that ?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:template match="/">
		<xsl:apply-templates mode="copy"/>
	</xsl:template>
	<xsl:template match="@*|node()" mode="copy">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()" mode="copy"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="*[not(descendant-or-self::*[text() | @*])]" mode="copy">
		<xsl:variable name="names">
			<xsl:apply-templates select="self::node()" mode="delete"/>
		</xsl:variable>
		<xsl:message>
			<xsl:value-of select="$names"/>
		</xsl:message>
	</xsl:template>
	<xsl:template match="*" mode="delete">
	Node <xsl:value-of select="local-name()"/> removed;<xsl:apply-templates select="*" mode="delete"/>
	</xsl:template>
</xsl:stylesheet>

Something like that ?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:template match="/">
		<xsl:apply-templates mode="copy"/>
	</xsl:template>
	<xsl:template match="@*|node()" mode="copy">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()" mode="copy"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="*[not(descendant-or-self::*[text() | @*])]" mode="copy">
		<xsl:variable name="names">
			<xsl:apply-templates select="self::node()" mode="delete"/>
		</xsl:variable>
		<xsl:message>
			<xsl:value-of select="$names"/>
		</xsl:message>
	</xsl:template>
	<xsl:template match="*" mode="delete">
	Node <xsl:value-of select="local-name()"/> removed;<xsl:apply-templates select="*" mode="delete"/>
	</xsl:template>
</xsl:stylesheet>

Not quite as it seems to break the recursiveness. It removes node1_1 but then it should also remove node1 as it also is empty now. BUT again I have seen a solution that gave me an eye-opener: The "mode" - and I'm pretty sure that using this is a valid solution to my problem.

I got a similar answer here which actually seems to work 100% as I wanted it to:
http://social.msdn.microsoft.com/Forums...

Thanks for answering and for helping out!

--
Werner

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.