如何使用xslt从xml中提取子标签文本和父标签的扩展文本 [英] How to extract child tags text and extended text of parent tag from xml using xslt

查看:41
本文介绍了如何使用xslt从xml中提取子标签文本和父标签的扩展文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

下面提到了我的 XSLT 代码:

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>

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

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)

我期待如下输出:

WGP03068-CNZH-00标题介绍 xslt应自动调整字距调整的最小字体大小.本书参考应自动调整字距调整的最小字体大小.希望您有良好的学习体验.我期待更多的解决方案.

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.

推荐答案

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

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.

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

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>

并且要将 style 元素输出为粗体,您将有一个这样的模板:

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>

试试下面的 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 时,输出如下

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>

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

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.

这篇关于如何使用xslt从xml中提取子标签文本和父标签的扩展文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆