简体   繁体   English

如何使用xslt从XML提取元素的属性并以不同的样式显示

[英]how to extract attributes of an element from an XML using xslt and display in different style

I am new to XSLT. 我是XSLT的新手。 I am working transforming an XML file from one format to another format. 我正在将XML文件从一种格式转换为另一种格式。 I also want to extract values from an element and display the them in bold format. 我还想从元素中提取值,并以粗体显示它们。

Sample source XML: 样本源XML:

 <Content xmlns="uuid:4522eb85">
      <first xmlns="uuid:4522eb85">Hello World. This is first field</first>
      <second author="XYZ">Hi iam second field</second>
 </Content>

Output format required: 所需的输出格式:

  <root>
     <aaa>Hello World. This is first field</aaa>
     <bbb><author>**XYZ**</author>Hi iam second field</bbb>
  <root>

I am unable to extract attributes from a tag and display with style(say bold). 我无法从标签中提取属性并显示样式(如粗体)。

Please help. 请帮忙。 Thank you in advance. 先感谢您。

This XSLT outputs exactly what you ask for. 该XSLT输出的正是您所要求的。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="uuid:4522eb85" exclude-result-prefixes="c">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/c:Content">
        <root>
            <aaa><xsl:value-of select="c:first"/></aaa>
            <bbb><author>**<xsl:value-of select="c:second/@author" />**</author><xsl:value-of select="c:second" /></bbb>
        </root>
    </xsl:template>
</xsl:stylesheet>

But as Jeremy suggests, you may want to consider first taking an (online) XSLT training if you want to get much further. 但是正如Jeremy所建议的那样,如果您想进一步学习,可能需要考虑首先参加(在线)XSLT培训。

If you want to display the text with style then you have to display the content in html. 如果要显示带有样式的文本,则必须以html显示内容。 You should use XSLT to get the info from XML and create a HTML output with you required style 您应该使用XSLT从XML获取信息,并使用所需的样式创建HTML输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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