Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change attribute value of empty tags

Status
Not open for further replies.

jammer1221

Programmer
Jul 30, 2003
352
US
Hi all,

I'm fairly new to XSL/T and I've been trying to get this problem to work. Here's the set up. I have a document full of MT elements. They contain a N attribute and a V attribute. The N's always have values but the V's sometimes don't. Is it possible to match all empty V attributes and if empty re-write the value for the N attribute?

I've been trying to do it like this:
Code:
<xsl:template match="@V[. = '']">
  <xsl:attribute name="N">some new format</xsl:attribute>
</xsl:template>

But, to no avail. Is this sort of thing even possible? Where did I go wrong? Let me know if you need more information!

Thanks,

Matt
 
You can devise a template for that purpose in conjunction with the other templates you have.
[tt]
<xsl:template match="//MT[@V='']/@N">
<xsl:attribute name="{name()}">
<xsl:value-of select="'some new format'" />
</xsl:attribute>
</xsl:template>
[/tt]
 
tsuji,

Thanks for the reply and I'm glad you posted that example because it means I haven't been entirely off. Well, I've tried your example (as well as many variations!) and it's not working. So, I'm posting a minimized version of my code in the hopes that you can visually spot an error.

XML:
Code:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<GSP>
  <RES SN="1" EN="7">
    <R N="1">
      <MT N="VLV-title" V="Title 1"/>
      <MT N="VLV-phone" V="(702) 293-xxxx"/>
    </R>
    <R N="2" L="2">
      <MT N="VLV-title" V="Title 2"/>
      <MT N="VLV-phone" V="(702) 385-xxxx"/>
    </R>
    <R N="3">
      <MT N="VLV-title" V="Title 3"/>
      <MT N="VLV-phone" V="(702) 785-xxxx"/>
      <MT N="VLV-fax" V="(702) 785-xxxx"/>
    </R>
    <R N="4">
      <MT N="VLV-title" V="Title 4"/>
      <MT N="VLV-phone" V="(702) 456-xxxx"/>
      <MT N="VLV-fax" V=""/>
    </R>
    <R N="5">
      <MT N="VLV-title" V="Title 5"/>
      <MT N="VLV-phone" V="(702) 456-xxxx"/>
      <MT N="VLV-fax" V="(702) 456-xxxx"/>
    </R>
  </RES>
</GSP>
XSL:
Code:
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="2.0">
<xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" omit-xml-declaration="yes" />
<xsl:template match="//MT[@V='']/@N">
    <xsl:attribute name="V">s</xsl:attribute>
</xsl:template>

<xsl:template match="GSP">
	<xsl:for-each select="RES">
		<xsl:for-each select="R">
    	<div>
        	<div style="float:left;">
			<xsl:apply-templates select="MT[@N='VLV-title']"/>
            </div>
            <div style="float:right">
            <xsl:apply-templates select="MT[@N='VLV-phone']"/>
            <xsl:if test="MT[@N='VLV-phone'] and MT[@N='VLV-fax']"> | </xsl:if>
            <xsl:apply-templates select="MT[@N='VLV-fax']"/>
            </div>
        </div><br />

		</xsl:for-each>
	</xsl:for-each>
</xsl:template>
<xsl:template match="MT">
	<xsl:if test="@V!='' and contains(@V,'null')=false">
	    <xsl:value-of select="@V" disable-output-escaping="yes"/>
    </xsl:if>
</xsl:template>   
</xsl:stylesheet>

Expected Results:
Code:
Title 1
(702) 293-xxxx

Title 2
(702) 385-xxxx

Title 3
(702) 785-xxxx | (702) 785-xxxx

Title 4 
(702) 456-xxxx

Title 5
(702) 456-xxxx | (702) 456-xxxx

As you can tell, I'm outputting the value and then in the next line checking if an adjacent MT element is there; if so, then ouput a pipe and then the value. If not, then no pipe.

Thanks again for your help! It is much appreciated!

Matt
 
That sure would be a different question than what I would reasonably deduce form the original post. Try this.
[tt]
<xsl:stylesheet xmlns:xsl="[ignore][/ignore]" version="2.0">
<xsl:eek:utput method="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" omit-xml-declaration="yes" />

<xsl:template match="GSP">
<xsl:for-each select="RES">
<xsl:for-each select="R">
<xsl:apply-templates select="MT[@N='VLV-title']"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template match="MT[@N='VLV-title']">
<div style="float:left;">
<xsl:value-of select="@V" />
</div>
<div style="float:right">
<xsl:for-each select="following-sibling::MT[@V!='']">
<xsl:value-of select="@V" />
<xsl:if test="following-sibling::MT[@V!='']">
<xsl:value-of select="' | '" />
</xsl:if>
</xsl:for-each>
</div>
<br />
</xsl:template>
</xsl:stylesheet>
[/tt]
 
tsuji,

That code works great for that specific example, but, the XML I posted was a simplified version. In case you feel like a challenge this early in the morning...
Code:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<GSP VER="3.2">
  <TM>0.016818</TM>
  <Q>test</Q>
  <PARAM name="access" value="p" original_value="p"/>
  <PARAM name="entqr" value="0" original_value="0"/>
  <RES SN="1" EN="7">
    <M>27</M>
    <FI/>
    <R N="1">
      <U>URL</U>
      <T>Test1</T>
      <MT N="revisit-after" V="10 days"/>
      <MT N="VLV-image" V="10515-12.jpeg"/>
      <MT N="VLV-title" V="Test 1"/>
      <MT N="VLV-address" V="US Hwy St"/>
      <MT N="VLV-city" V="city"/>
      <MT N="VLV-state" V="XX"/>
      <MT N="VLV-zip" V="00000"/>
      <MT N="VLV-phone" V="(xxx) xxxxxxx"/>
      <MT N="VLV-description" V="Test Description"/>
      <MT N="VLV-property" V="Test &amp; Casino"/>
      <MT N="VLV-category" V="Buffet"/>
      <MT N="VLV-cost" V="&lt; $15"/>
      <MT N="VLV-tollFree" V="xxx"/>
      <S>sample sample sample</S>
    </R>
    <R N="2">
      <U>URL</U>
      <T>Test2</T>
      <MT N="revisit-after" V="10 days"/>
      <MT N="VLV-image" V="10515-12.jpeg"/>
      <MT N="VLV-title" V="Test 2"/>
      <MT N="VLV-address" V="US Hwy St"/>
      <MT N="VLV-city" V="city"/>
      <MT N="VLV-state" V="XX"/>
      <MT N="VLV-zip" V="00000"/>
      <MT N="VLV-phone" V="(xxx) xxxxxxx"/>
	  <MT N="VLV-fax" V=""/>
      <MT N="VLV-description" V="Test Description"/>
      <MT N="VLV-property" V="Test &amp; Casino"/>
      <MT N="VLV-category" V="Buffet"/>
      <MT N="VLV-cost" V="&lt; $15"/>
      <MT N="VLV-tollFree" V="xxx"/>
      <S>go go go</S>
    </R>
    <R N="3">
      <U>URL</U>
      <T>Test3</T>
      <MT N="revisit-after" V="10 days"/>
      <MT N="VLV-image" V="10515-12.jpeg"/>
      <MT N="VLV-title" V="Test 3"/>
      <MT N="VLV-address" V="US Hwy St"/>
      <MT N="VLV-city" V="city"/>
      <MT N="VLV-state" V="XX"/>
      <MT N="VLV-zip" V="00000"/>
      <MT N="VLV-phone" V="(xxx) xxxxxxx"/>
	  <MT N="VLV-fax" V="xxxxxx"/>
      <MT N="VLV-description" V="Test Description"/>
      <MT N="VLV-property" V="Test &amp; Casino"/>
      <MT N="VLV-category" V="Buffet"/>
      <MT N="VLV-cost" V="&lt; $15"/>
      <MT N="VLV-tollFree" V="xxx"/>
      <S>test test test</S>
    </R>
  </RES>
</GSP>

I can post the XLST I have this far too...

Thanks a ton!

Matt
 
Just modify the specific template.
[tt]
<xsl:template match="MT[@N='VLV-title']">
<div style="float:left;">
<xsl:value-of select="@V" />
</div>
<div style="float:right">
<xsl:for-each select="following-sibling::MT[@V!=''][@N='VLV-phone' or @N='VLV-fax']">
<xsl:value-of select="@V" />
<xsl:if test="following-sibling::MT[@V!=''][@N='VLV-phone' or @N='VLV-fax']">
<xsl:value-of select="' | '" />
</xsl:if>
</xsl:for-each>
</div>
<br />
</xsl:template>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top