How to extract child tags text and extended text o

2019-06-14 22:01发布

问题:

I mentioned below my xml file, xml contains many inner style tags(Please use generic xslt code to extract all the text) but it should extract all text after innertag and text before start the innertag.

<?xml version="1.0" encoding="UTF-8"?>
<Values>
    <Value AttributeID="11218">
        <Text>WGP03068-CNZH-00
                <style name="bold">Introduction of the Title</style>xslt
                <style name="TextStyle1">
                    The smallest font size for which kerning should be automatically adjusted.
                </style>
                <style name="bold">Reference for this book
                  <style name="TextStyle1">
                    The smallest font size for which kerning should be automatically adjusted.
                </style>
                Hope you had a good learning experience.
                </style>
                I am expecting more solution for my doughts.
            </Text>
        </Value>
    </Values>

My XSLT code is mentioned Below :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <HTML>
            <xsl:apply-templates />
        </HTML>
    </xsl:template>

    <xsl:template match="Values">
        <xsl:for-each select="Value">
            <xsl:for-each select="Text">
                <p>
                <xsl:for-each select="style">
                    <xsl:value-of select="." />
                </xsl:for-each>
                </p>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

My XSLT is missing starting text and ending text of element it reads only element, I want to read all outside and innertag text and add my own styles(like bold,Italic, font name and color)

I am expecting OUTPUT like below :

WGP03068-CNZH-00 Introduction of the Title xslt The smallest font size for which kerning should be automatically adjusted. Reference for this book The smallest font size for which kerning should be automatically adjusted.Hope you had a good learning experience. I am expecting more solution for my doughts.

回答1:

A better approach would be use a 'push' approach, and code your XSLT stylesheet as a series of matching templates, where you output the html you require.

For example, to convert a Text element in your XML to a paragraph, you would use the following template.

  <xsl:template match="Text">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>

And to output a style element as bold, you would have a template like this:

  <xsl:template match="style[@name='bold']">
    <span style="font-weight:bold">
      <xsl:apply-templates />
      </span>
  </xsl:template>

Try the following XSLT

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

  <xsl:template match="/">
    <HTML>
      <xsl:apply-templates />
    </HTML>
  </xsl:template>

  <xsl:template match="Text">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>

  <xsl:template match="style[@name='bold']">
    <span style="font-weight:bold">
      <xsl:apply-templates />
      </span>
  </xsl:template>

  <xsl:template match="style">
    <span>
      <xsl:apply-templates />
    </span>
  </xsl:template>
</xsl:stylesheet> 

When applied to your XML, the following is output

<HTML>
   <p>WGP03068-CNZH-00
      <span style="font-weight:bold">Introduction of the Title</span>xslt
      <span>
         The smallest font size for which kerning should be automatically adjusted.
         </span><span style="font-weight:bold">Reference for this book
         <span>
            The smallest font size for which kerning should be automatically adjusted.
            </span>
         Hope you had a good learning experience.
         </span>
      I am expecting more solution for my doughts.

   </p>
</HTML>

Do note that this XSLT makes use of XSLT's built-in templates to ouput the text. Where it does <xsl:apply-templates /> and XSLT finds a text node, if there is no matching template for it, it will automatically output the text for you.



标签: xml xslt