[经验总结]XSLT开发总结

2024-03-30 23:38

本文主要是介绍[经验总结]XSLT开发总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

    注意XSLT不能对输入文档的DOM树进行更改
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
    1 循环 for-each
    xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" indent="no"/>
    
        <xsl:template match="a">
            <xsl:for-each select="./node()">
                <xsl:choose>
                    <!--Text node -->
                    <xsl:when test="./self::text()">StartATag<xsl:copy select="."/>EndATag</xsl:when>
    
                    <!--Element node-->
                    <xsl:when test="./self::*">StartBTag<xsl:copy-of select="./node()"/>EndBTag</xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </xsl:template>
    
    </xsl:stylesheet>

==================================================================

    2 div 定义元素与属性
                  
    <xsl:element name="div">
                                                   <xsl:attribute name="type"><xsl:text>maintitle</xsl:text></xsl:attribute>
                                                   <xsl:attribute name="name"><xsl:value-of select="./PAM_Attributes/PAM_Attr[@name='tqID']/@value"/></xsl:attribute>
                                                   <xsl:attribute name="class"><xsl:text>maintitle</xsl:text></xsl:attribute>
                        <xsl:copy-of select="./PAM_Attributes/following-sibling::*"/>
                                         </xsl:element>
 
==================================================================

    3 在XSLT中操作多个XML文件
    <xsl:variable name="docs1" select="document('./file1.XML')"/>
    <xsl:copy-of select="$docs1"/>
    
    <xsl:variable name="docs2" select="document('./file2.XML')"/>
    <xsl:copy-of select="$docs2"/>
 
==================================================================

    4 无穷递归 错误提示
    The XSL processor stack has overflowed - probable cause is
    infinite template recursion.

==================================================================

    5 变量算术操作示例
    <xsl:value-of select="$nsLength*10"/>
 
==================================================================

    6 xslt模板 参数 param  
    <?xml version="1.0" encoding="UTF-8" ?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:param name="Key1"/>
      <xsl:param name="Path"/>
 
==================================================================
    7  Makefile for xsltproc
    # For XSLT
    PARSE = xsltproc
    SRC = main1.xml
    STYLESHEET = ../../xsl/makepaper.xsl
    OUTPUT = ../../output.html
    
    
    all :  $(SRC) $(STYLE)
             $(PARSE)  $(STYLESHEET) $(SRC) > $(OUTPUT)  
    
    clean:
             del $(OUTPUT)
 
==================================================================

    8 libxslt库辅助工具xlstproc 用法示例
    带参数的xslt para xslt
    WURFL XSLT Tools
    by Roland Guelle
    
    The WURFL information is stored in XML. So the simpliest way to manipulate and transformm WURLF is XSLT.
    If you have ideas or own XSLT, your help is welcome :)
    
    These stylesheets are tested with xsltproc from the package libxslt. I only use this on the command line, if you need a GUI - there are lot of tools that helps you working with XSLT.
    Some exslt extensions are used, so please check if your XSLT processor supports this.
    
    If you have never worked with a XSLT processor or the command line before, add to WURFL and open the file with Firefox.
    
    Available XSLTs
    
    patch_wurfl.xsl
    Patch WURFL with your own devices, groups and capabilities.
    Example:
    run: xsltproc --stringparam 'file' "your_patch_file" patch_wurfl.xsl wurfl.xml > wurfl_patched.xml
    If you like to format the output (with xmllint):
    run: xsltproc --stringparam 'file' "your_patch_file" patch_wurfl.xsl wurfl.xml | xmllint --format - > wurfl_patched.xml
    If you like to format the output (with tidy):
    run: xsltproc --stringparam 'file' "your_patch_file" patch_wurfl.xsl wurfl.xml | tidy -config tidy_config - > wurfl_patched.xml
    
    check_wurfl.xsl
    Checks your patched/modified WURFL consistency if:
    id attribute is present and unique
    user_agent attribute is present and unique
    fall_back is available and could be resolved
    
    Example:
    run: xsltproc check_wurfl.xsl wurfl.xml
    
    roll out XSLT
    Resolve WURFL fallbacks for all values and capabilities defined in roll_out_capabilities.xml.
    Check the parameters in the XSLT for more options.
    roll_out_into_txt.xsl
    Create text (maybe CSV compatible) version of WURFL.
    Example:
    run: xsltproc roll_out_into_txt.xsl wurfl.xml > wurfl.txt
    roll_out_into_xml.xsl
    Create a flat WURFL version, with each capability as device attribute SQL.
    Example:
    run: xsltproc roll_out_into_xml.xsl wurfl.xml > flat_wurfl.xml
    roll_out_into_sql.xsl
    Create SQL statements for insert WURFL into a SQL database.
    If you like, you can also generate the create statement with this script (default).
    Example:
    run: xsltproc roll_out_into_sql.xsl wurfl.xml > wurfl.sql
    
    remove_elements.xsl
    WURFL lists many capabilities and groups. If you don't need each of these elements, you will find this tool really useful.
    Create a small XML listing the unwanted groups, run the XSLT and you'll get a new WURFL without the groups and the capabilities that you have selected. This should help you keep WURFL smaller and make your applications faster.
    Example:
    edit remove_elements.xml
 
          <?xml version="1.0" encoding="UTF-8"?>
          <elements>
          <groups>
            <group>wta</group>
            <group>object_download</group>
            <group>streaming</group>
            <group>wap_push</group>
            <group>j2me</group>
            <group>mms</group>
            <group>sms</group>
            <group>sound_format</group>
          </groups>
          <capabilities>
            <capability>tiff</capability>
          </capabilities>
          </elements>
      
 
    run: xsltproc --stringparam 'file' "remove_elements.xml" remove_elements.xsl wurfl.xml > clean_wurfl.xml
    
    Some (maybe) outdated stylesheets:
    get_capabilities.xsl
    Get capabilities without groups from WURFL for selected user agent.
    Example:
    run: xsltproc --stringparam 'ua' "Nokia3650" get_capabilities.xsl wurfl.xml
    
    convert_wurfl_markups.xsl
    Convert the WURLF markups to the 'old' names.
    The markup names have changed from wml11 to wml_1_1 standard.
    If you don't want to change your application, you can use this XSLT to transform the names into the old format.
    Example:
    run: xsltproc convert_wurfl_markups.xsl wurfl.xml > wurfl_converted.xml
    
    count_useragents.xsl
    Answers your question "how many useragents/capabilities are stored in WURFL?"
    Example:
    run: xsltproc count_useragents.xsl wurfl.xml
    
    convert_2_html.xsl
    Create an HTML page from the WURFL.xml
    Example:
    run: xsltproc convert_2_html.xsl wurfl.xml > wurfl.html
 
==================================================================

    9 XSLT 2.0 使用 XPath 2.0 和 XSLT 2.0 节省开发时间并减少代码量
 
    http://www.ibm.com/developerworks/cn/xml/x-xslt20xpath20/
 
==================================================================
10 XSLT中模拟 map与 固定次数的循环
    <!--将数字转换成字母 以用作选项-->
    <xsl:template name='digital2letter'>
      <xsl:param name='digit' select='0'/>
      <xsl:choose>
        <xsl:when test='$digit = 0'>A</xsl:when>
        <xsl:when test='$digit = 1'>B</xsl:when>
        <xsl:when test='$digit = 2'>C</xsl:when>
        <xsl:when test='$digit = 3'>D</xsl:when>
        <xsl:when test='$digit = 4'>E</xsl:when>
        <xsl:when test='$digit = 5'>F</xsl:when>
        <xsl:when test='$digit = 6'>G</xsl:when>
 
        <xsl:otherwise><p>Error Choice</p></xsl:otherwise>
      </xsl:choose>
    </xsl:template>
 
 
         <xsl:template name="AddGeneralChoises"> 
                   <xsl:param name="QuestionID"/>        
                   <xsl:param name="level" select="0" /> <!--递归深度-->
 
 
        <xsl:if test="$level &lt; 6"> <!--固定添加六个选项-->
            <xsl:variable name="choiceLetter">
                <xsl:call-template name="digital2letter"> 
                    <xsl:with-param name="digit" select="$level"/>
                </xsl:call-template>
            </xsl:variable>
 
            <xsl:element name="input">
                <xsl:attribute name="type">radio</xsl:attribute>
                <xsl:attribute name="name"><xsl:value-of select="$QuestionID"/></xsl:attribute>
                <xsl:attribute name="choice"> <xsl:value-of select="."/> </xsl:attribute>
                <xsl:attribute name="onfocus"><xsl:text>blur()</xsl:text></xsl:attribute>
            </xsl:element>
 
            <xsl:value-of select="$choiceLetter"/>
            <xsl:text>  </xsl:text>
 
            <xsl:variable name="levelPlus" select="$level + 1" />
 
            <xsl:call-template name="AddGeneralChoises"> <!--XSLT 1.0不支持固定循环次数的for-each,只能用递归模拟循环-->
                <xsl:with-param name="QuestionID" select="$QuestionID"/>
                <xsl:with-param name="level" select="$levelPlus"/>
            </xsl:call-template>
 
        </xsl:if>
 
    </xsl:template>

==================================================================

    15 xslt  输出方法  outmethod 很重要
      标准文档: http://www.w3.org/TR/xslt#section-XML-Output-Method
      同样一个<b/>  html输出成<b></b>  xml输出成<b/>

==================================================================

    16  xslt output method引起的网页显示问题。
 
      环境:Windows XP SP3 + IE 8.0
    
     xslt:      
      makepaper.xsl:  <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="yes"/>
    
      mainbody.html:  <html>
    
      问题:  <br />输成<br/>  IE8对<br/>当成<br>解析,导致<br/>后面的文字都变成黑体。
    
      将<xsl:output method="xml" />改为<xs:output method="html" />
      出现新问题,图片不显示,图片链接中的汉字为乱码:
        file:///E:/%25E5%25B1%25B1%25E8%25A5%25BF%25E5%2585%2589%25E7%259B%2598/web/data/demo/image/image2.jpeg
    
      file:///E:/光盘/web/data/demo/image/image2.jpeg
    
      最终解决办法:
        更改mainbody.html: 为xhtml格式
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    
      xsl的输出格式为xml,xhtml中能正确解析 <br/>而不是将之当成<br>处理。

==================================================================

    17 处理文本结果的技巧
         <xsl:template match="text()">
            <xsl:variable name="parentName" select="name(..)" />
 
            <xsl:if test='$parentName != "OOXML"'> <!--过滤OOXML的信息-->
                <xsl:copy-of select="."/>
            </xsl:if>
         </xsl:template>
==================================================================

    18  XSLT 判断XML结点的类型 XSLT

    <?xml version="1.0"?>
    <!--以下代码摘自XSLTSL工程-->
    <!--在XSLT中判断XML文档的结点类型 -->
    
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:doc="http://xsltsl.org/xsl/documentation/1.0"
        xmlns:node="http://xsltsl.org/node"
        extension-element-prefixes="doc node">
    
      <xsl:template name="node:type">
        <xsl:param name="node" select="."/>
    
        <xsl:choose>
          <xsl:when test="not($node)"/>
          <!--运用了self轴-->
          
          <xsl:when test="$node[1]/self::*">                     <!--元素结点-->
               <xsl:text>element</xsl:text>
          </xsl:when>
    
          <xsl:when test="$node[1]/self::text()">                <!--文本结点-->
               <xsl:text>text</xsl:text>
          </xsl:when>
    
          <xsl:when test="$node[1]/self::comment()">             <!--注释结点-->
               <xsl:text>comment</xsl:text>
          </xsl:when>
    
          <xsl:when test="$node[1]/self::processing-instruction()">  <!--处理指令结点-->
        <xsl:text>processing instruction</xsl:text>
          </xsl:when>
          <xsl:when test="not($node[1]/parent::*)">                  <!--根结点-->
            <xsl:text>root</xsl:text>
          </xsl:when>
    
          <!--下面两种类型的判断巧妙运用了集合的概念-->
          <xsl:when test="count($node[1] | $node[1]/../namespace::*) = count($node[1]/../namespace::*)">
            <xsl:text>namespace</xsl:text>
          </xsl:when>
    
          <xsl:when test="count($node[1] | $node[1]/../@*) = count($node[1]/../@*)">
            <xsl:text>attribute</xsl:text>
          </xsl:when>
    
        </xsl:choose>
      </xsl:template>
    
    </xsl:stylesheet>

==================================================================

    9 在XSLT载入XML文档,注意XML文件不存在时,为避免在网页报错,需将文件名置空。
    <xsl:variable name="propertyDoc" select="document($propertyFile)"/>

这篇关于[经验总结]XSLT开发总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/862882

相关文章

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

基于Python开发PPTX压缩工具

《基于Python开发PPTX压缩工具》在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,不便于传输和存储,所以本文将使用Python开发一个PPTX压缩工具,需要的可以了解下... 目录引言全部代码环境准备代码结构代码实现运行结果引言在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

Git提交代码详细流程及问题总结

《Git提交代码详细流程及问题总结》:本文主要介绍Git的三大分区,分别是工作区、暂存区和版本库,并详细描述了提交、推送、拉取代码和合并分支的流程,文中通过代码介绍的非常详解,需要的朋友可以参考下... 目录1.git 三大分区2.Git提交、推送、拉取代码、合并分支详细流程3.问题总结4.git push

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

Kubernetes常用命令大全近期总结

《Kubernetes常用命令大全近期总结》Kubernetes是用于大规模部署和管理这些容器的开源软件-在希腊语中,这个词还有“舵手”或“飞行员”的意思,使用Kubernetes(有时被称为“... 目录前言Kubernetes 的工作原理为什么要使用 Kubernetes?Kubernetes常用命令总

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

Python中实现进度条的多种方法总结

《Python中实现进度条的多种方法总结》在Python编程中,进度条是一个非常有用的功能,它能让用户直观地了解任务的进度,提升用户体验,本文将介绍几种在Python中实现进度条的常用方法,并通过代码... 目录一、简单的打印方式二、使用tqdm库三、使用alive-progress库四、使用progres

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要