简体   繁体   中英

Transforming nested child nodes with xslt

XML data file

<?xml version="1.0" encoding="utf-8"?>
<page>
    <tab dim="70">

        <tab dim="50">
        alpha

        </tab>
        <tab dim="50">
        alpha

        </tab>

    </tab>
    <tab dim="30">
        gama
    </tab>
</page>

XSLT File

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0"
encoding="iso-8859-1" indent="yes"/>

<xsl:template match="/">
    <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html&gt;</xsl:text>
    <html>
    <head>
        <title>Title</title>
        <link type="text/css" href="/css/framework.css" rel="stylesheet"/>

    </head>
    <body>
    <div id="page-base">
        <xsl:for-each select="//tab">
        <div class="wrapper tab">
            <xsl:attribute name="style"> 
                width:<xsl:value-of select="@dim" />%;
                min-width:<xsl:value-of select="@dim" />%;
                max-width:<xsl:value-of select="@dim" />%;
            </xsl:attribute>
            <xsl:value-of select="." />
        </div>
        </xsl:for-each>
    </div>
    </body>
    </html>
</xsl:template>

</xsl:stylesheet>

Ouput

<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="/css/framework.css" type="text/css">
</head>
<body>
<div id="page-base">
 <div class="wrapper tab" style=" width:70%; min-width:70%; max-width:70%; ">
   alpha alpha   
 </div>
 <div class="wrapper tab" style=" width:50%; min-width:50%; max-width:50%; "> alpha </div>
 <div class="wrapper tab" style=" width:50%; min-width:50%; max-width:50%; "> alpha </div>
 <div class="wrapper tab" style=" width:30%; min-width:30%; max-width:30%; "> gama </div>
</div>
</body>

Desired output

<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="/css/framework.css" type="text/css">
</head>
<body>
<div id="page-base">
 <div class="wrapper tab" style=" width:70%; min-width:70%; max-width:70%; ">
  <div class="wrapper tab" style=" width:50%; min-width:50%; max-width:50%; "> alpha </div>
  <div class="wrapper tab" style=" width:50%; min-width:50%; max-width:50%; "> alpha </div>
 </div>
 <div class="wrapper tab" style=" width:30%; min-width:30%; max-width:30%; "> gama </div>
</div>
</body>
</html>

The key is not to use <xsl:for-each> at all. Use template matching instead.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="/">
    <html>
      <head>
        <title>Title</title>
        <link type="text/css" href="/css/framework.css" rel="stylesheet"/>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="page">
    <div id="page-base">
      <xsl:apply-templates />
    </div>
  </xsl:template>

  <xsl:template match="tab">
    <div class="wrapper tab" style="width:{@dim}%; min-width:{@dim}%; max-width:{@dim}%;">
      <xsl:apply-templates />
    </div>
  </xsl:template>
</xsl:stylesheet>

Result: (also see http://xsltransform.net/gWmuiK6 )

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Title</title>
      <link type="text/css" href="/css/framework.css" rel="stylesheet">
   </head>
   <body>
      <div id="page-base">
         <div class="wrapper tab" style="width:70%; min-width:70%; max-width:70%;">
            <div class="wrapper tab" style="width:50%; min-width:50%; max-width:50%;">
               alpha


            </div>
            <div class="wrapper tab" style="width:50%; min-width:50%; max-width:50%;">
               alpha


            </div>
         </div>
         <div class="wrapper tab" style="width:30%; min-width:30%; max-width:30%;">
            gama

         </div>
      </div>
   </body>
</html>

To get rid of the white-space in the output, all you need to do is to add another template:

<xsl:template match="tab/text()">
  <xsl:value-of select="normalize-space()" />
</xsl:template>

There are specific cases where <xsl:for-each> is the right thing to use. This is not one of them.

Whenever you feel inclined to use <xsl:for-each> you should stop and think. In the vast majority of cases <xsl:apply-templates> is what you really want.

Use <xsl:for-each> only if you find a compelling reason against <xsl:apply-templates> . If you can't think of one such reason, then there probably is none.


To output nodes that appear in the input without changing them, add the identity template:

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()" />
  </xsl:copy>
</xsl:template>

To mute nodes that you do not want in the output, add an empty template:

<xsl:template match="node/to/match" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM