如何使用xslt从xml中提取子标记文本和父标记的扩展文本

时间:2013-05-07 07:43:48

标签: xml xslt

我在下面提到了我的xml文件,xml包含许多内部样式标记(请使用通用xslt代码来提取所有文本)但它应该在启动innertag之前提取innertag和text之后的所有文本。

<?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>

我的XSLT代码在下面提到:

<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>

我的XSLT缺少元素的起始文本和结尾文本,它只读取元素,我想读取所有外部和innertag文本并添加我自己的样式(如粗体,斜体,字体名称和颜色)

我期待OUTPUT如下所示:

WGP03068-CNZH-00 标题简介 xslt 应自动调整字距的最小字体大小。 参考本书 应该自动调整字距的最小字体大小。希望你有一个很好的学习经历。 我期待更多的解决方案。

1 个答案:

答案 0 :(得分:0)

更好的方法是使用“推送”方法,并将XSLT样式表编码为一系列匹配模板,您可以在其中输出所需的html。

例如,要将XML中的 Text 元素转换为段落,您可以使用以下模板。

  <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>

尝试以下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> 

应用于XML时,输出以下内容

<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>

请注意,此XSLT使用XSLT的内置模板来输出文本。 <xsl:apply-templates />和XSLT找到文本节点的位置,如果没有匹配的模板,它会自动为您输出文本。

相关问题