Frage

I am trying to figure out how to ignore empty returns for Xslt.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" ><xsl:output indent="no" method="text"/>
 <xsl:strip-space elements="*" />
 <xsl:template match="/">
Famous Mountains of the World
<xsl:apply-templates />
</xsl:template><xsl:template match="mountain[string-length() !=0]">
Mountain Name:<xsl:value-of select="name[@language='English']"/>
Mountain Name:(<xsl:value-of select="name[@language='PigLatin'] "/>)</xsl:template>
</xsl:stylesheet>

As you can see, it has a selector for piglatin and I am trying to get it to not return blank data when the nodes doesn't have a piglatin option. Here is the xml I am formatting.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Note: This is a comment-->
<?xml-stylesheet type="text/xsl" href="Asg08.xsl"?>
<FamousMountains>
  <mountain>
    <name language="English">Mount Everest</name>
    <name language="PigLatin">ountMa verestEa</name>
    <location>Nepal</location>
    <height units="feet">29035</height>
  </mountain>
  <mountain>
    <name language="English">Mount Ranier</name>
    <location>Washington</location>
    <height units="feet">14411</height>
  </mountain>
  <mountain>
    <name language="English">Mount St. Helens</name>
    <location>Washington</location>
    <height units="feet">8364</height>
  </mountain>
  <mountain>
    <name language="English">Mount Washington</name>
    <name language="PigLatin">ountMa ashingtonWa</name>
    <location>New Hampshire</location>
    <height units="feet">6288</height>
  </mountain>
  <mountain>
    <name language="English">Mount Bonnell</name>
    <name language="PigLatin">ountMa onnellBa</name>
    <location>Austin</location>
    <height units="feet">800</height>
  </mountain>
  <mountain>
    <name language="English">Mount Vesuvius</name>
    <name language="PigLatin">ountMa esuviusVa</name>
    <location>Italy</location>
    <height units="feet">4203</height>
  </mountain>
  <mountain>
    <name language="English">Mount Etna</name>
    <name language="PigLatin">ountMa tnaEa</name>
    <location>Sicily</location>
    <height units="feet">10922</height>
  </mountain>
</FamousMountains>

with the output looking like that

Mountain Name: Mount Everest  
Pig Latin Name: ountMa verestEa  
Mountain Name: Mount Ranier  
Mountain Name: Mount St. Helens  
Mountain Name: Mount Washington  
Pig Latin Name: ountMa ashingtonWa  
Mountain Name: Mount Bonnell  
Pig Latin Name: ountMa onnellBa  
Mountain Name: Mount Vesuvius  
Pig Latin Name: ountMa esuviusVa  
Mountain Name: Mount Etna  
Pig Latin Name: ountMa tnaEa  

Any help or insight to what I am doing wrong would be appreciated.

War es hilfreich?

Lösung

Instead of outputting the value of the name elements in the template that matches "mountain", have separate templates for them

<xsl:template match="name[@language='English']">
Mountain Name: <xsl:value-of select="." />
</xsl:template>

<xsl:template match="name[@language='PigLatin']">
Mountain Name:(<xsl:value-of select="." />)
</xsl:template>

Then replace the xsl:value-of with xsl:apply-templates* in the main template

<xsl:apply-templates select="name[@language='English']" />
<xsl:apply-templates select="name[@language='PigLatin']" />

That way, if there is no such name, nothing is output.

Try this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" ><xsl:output indent="no" method="text"/>
  <xsl:strip-space elements="*" />
  <xsl:template match="/">
    Famous Mountains of the World
  <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="mountain[string-length() !=0]">
    <xsl:apply-templates select="name[@language='English']" />
    <xsl:apply-templates select="name[@language='PigLatin']" />
  </xsl:template>

  <xsl:template match="name[@language='English']">
    Mountain Name: <xsl:value-of select="." />
  </xsl:template>

  <xsl:template match="name[@language='PigLatin']">
    Mountain Name:(<xsl:value-of select="." />)
  </xsl:template>
</xsl:stylesheet>

Note that if "English" always occurs before "Piglatin" in your XML, you could replace the two xsl:apply-templates with a single one

<xsl:apply-templates select="name" />
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top