Style inline text along with nested tags with XSLT

2019-09-04 09:25发布

问题:

I have an XML structure that looks something like this.

<root>

  <outer-tag>
    Some Text
    <inner-tag> Some more text </inner-tag>
    finally some more text
  </outer-tag>

  <outer-tag>
    ....
  </outer-tag>

  ...
</root>

How would the XSLT look like for the above structure?

I am doing something like this, which I know is wrong.

<xsl:for-each select="outer-tag">
    <xsl:value-of select="."/>
    <b><xsl:value-of select="inner-tag"/></b>
</xsl:for-each>

And the output for this XSLT looks like

Some text Some more text finally some more text <b>Some more text</b>

My actual output should be

Some text <b>Some more text</b> finally some more text 

Thanks in advance for your help.

回答1:

Use push style processing with template matching and apply-templates e.g.

<xsl:template match="inner-tag">
   <b>
     <xsl:apply-templates/>
   </b>
</xsl:template>

then the default templates would take care of making sure the processing is kept up or you can write your own doing that with e.g.

<xsl:template match="outer-tag">
  <div>
    <xsl:apply-templates/>
  </div>
</xsl:template>