使用 xslt 从 xml 转换而来的平面文件中的行数

2024-03-05

下面是我用来将 xml 转换为平面文件的 xsl,它还满足各种其他所需条件。

<xsl:stylesheet version="1.0"      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"      xmlns:ext="http://exslt.org/common">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:variable name="vrtfDefaults">
    <termCat/>
    <termVocab/>
  </xsl:variable>
  <xsl:variable name="vDefaults" select="ext:node-set($vrtfDefaults)"/>
  <xsl:variable name="vQ">"</xsl:variable>
  <xsl:template match="Zthes">
    <xsl:text>"HDR";"PIGLSSTD";"20120112045620";"F"</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>&#xA;"FTR";</xsl:text>
    <xsl:value-of select="count(term)+count(term/termCategory)+count(term/relation/termVocabulary)"/>
  </xsl:template>
  <xsl:template match="term">
    <xsl:variable name="vTerm" select="."/>
    <xsl:variable name="vRow1" select="'&#xA;&quot;GL&quot;;'"/>
    <xsl:for-each select="termCategory | $vDefaults/termCat[not($vTerm/termCategory)]">
      <xsl:variable name="vRow2" select="concat($vRow1, $vQ, ., $vQ, ';')"/>
      <xsl:for-each select="$vTerm/termVocabulary | $vDefaults/termCat[not($vTerm/termVocabulary)]">
        <xsl:variable name="vRow3" select="concat($vRow2, $vQ, ., $vQ, ';')"/>
        <xsl:for-each select="$vTerm/relation/termVocabulary | $vDefaults/termCat[not($vTerm/relation/termVocabulary)]">
          <xsl:value-of select="concat($vRow3, $vQ, ., $vQ, ';')"/>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="text()"/>
</xsl:stylesheet>

示例 XML 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<GetSavedReportResponse>
  <ResponseType>Success</ResponseType>
  <FileModifiedDateTime>2012-01-03T17:05:04</FileModifiedDateTime>
  <FileSizeBytes>7816</FileSizeBytes>
  <FileDataFormat>XML</FileDataFormat>
  <FileData>
    <Zthes>
      <term>
        <termId>49555</termId>
        <termUpdate>add</termUpdate>
        <termName>Active Personnel</termName>
        <termVocabulary>People Status Global Term1</termVocabulary>
        <termVocabulary>Global People Status Term1</termVocabulary>
        <termCategory>PDA Term1</termCategory>
        <termCategory>PDI Term1</termCategory>
        <termCategory>GLB Term1</termCategory>
        <relation weight="100">
          <termId>49556</termId>
          <relationType>EQ Term1</relationType>
          <termName>term name Term1</termName>
          <termVocabulary>term vocabulary Term1</termVocabulary>
        </relation>
        <relation weight="100">
          <termId>49557</termId>
          <relationType>BT</relationType>
          <termName>General Active Personnel</termName>
          <termVocabulary>People Status Global Updated</termVocabulary>
        </relation>
      </term>
      <term>
        <termId>49556</termId>
        <termUpdate>add</termUpdate>
        <termName>Leave of Absence Personnel</termName>
        <termVocabulary>People Status Global Term2</termVocabulary>
        <termCategory>GLB Term2</termCategory>
        <termCategory>PDI Term2</termCategory>
        <relation weight="100">
          <relationType>BT</relationType>
          <termId>49554</termId>
          <termName>General Non-Active Personnel Term2</termName>
          <termVocabulary>People Status Global Term2</termVocabulary>
        </relation>
      </term>
    </Zthes>
  </FileData>
</GetSavedReportResponse>

除了页脚中的行数之外,一切正常,它应该是平面文件中的行数(不包括页眉和页脚)。

预期输出:

"HDR";"PIGLSSTD";"20120112045620";"F"
"GL";"PDA Term1";"People Status Global Term1";"term vocabulary Term1";
"GL";"PDA Term1";"People Status Global Term1";"People Status Global Updated";
"GL";"PDA Term1";"Global People Status Term1";"term vocabulary Term1";
"GL";"PDA Term1";"Global People Status Term1";"People Status Global Updated";
"GL";"PDI Term1";"People Status Global Term1";"term vocabulary Term1";
"GL";"PDI Term1";"People Status Global Term1";"People Status Global Updated";
"GL";"PDI Term1";"Global People Status Term1";"term vocabulary Term1";
"GL";"PDI Term1";"Global People Status Term1";"People Status Global Updated";
"GL";"GLB Term1";"People Status Global Term1";"term vocabulary Term1";
"GL";"GLB Term1";"People Status Global Term1";"People Status Global Updated";
"GL";"GLB Term1";"Global People Status Term1";"term vocabulary Term1";
"GL";"GLB Term1";"Global People Status Term1";"People Status Global Updated";
"GL";"GLB Term2";"People Status Global Term2";"People Status Global Term2";
"GL";"PDI Term2";"People Status Global Term2";"People Status Global Term2";
"FTR";14

这种转变:

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>

     <xsl:variable name="vrtfDefaults">
       <termCat/>
       <termVocab/>
     </xsl:variable>

     <xsl:variable name="vDefaults" select=
      "ext:node-set($vrtfDefaults)"/>

     <xsl:variable name="vQ">"</xsl:variable>

     <xsl:template match="Zthes">
      <xsl:text>HDR";"PIGLSSTD";"20120112045620";"F":</xsl:text>

        <xsl:variable name="vMainOutput">
         <xsl:apply-templates/>
        </xsl:variable>

       <xsl:copy-of select="$vMainOutput"/>

      <xsl:text>&#xA;FTR;</xsl:text>

      <xsl:value-of select=
       "string-length($vMainOutput)
      -
        string-length(translate($vMainOutput, '&#xA;',''))
       "/>
     </xsl:template>

     <xsl:template match="term">
       <xsl:variable name="vTerm" select="."/>

       <xsl:variable name="vRow1" select="'&#xA;&quot;GL&quot;;'"/>

         <xsl:for-each select=
          "termCategory
          |
           $vDefaults/termCat[not($vTerm/termCategory)]">
           <xsl:variable name="vRow2" select=
               "concat($vRow1, $vQ, ., $vQ, ';')"/>

           <xsl:for-each select=
            "$vTerm/termVocabulary
            |
             $vDefaults/termCat[not($vTerm/termVocabulary)]
            ">
             <xsl:variable name="vRow3" select=
               "concat($vRow2, $vQ, ., $vQ, ';')"/>

            <xsl:for-each select=
             "$vTerm/relation/termVocabulary
             |
              $vDefaults/termCat[not($vTerm/relation/termVocabulary)]
             ">
            <xsl:value-of select="concat($vRow3, $vQ, ., $vQ, ';')"/>
          </xsl:for-each>
          </xsl:for-each>
         </xsl:for-each>
     </xsl:template>

     <xsl:template match="text()"/>
</xsl:stylesheet>

当应用于提供的 XML 文档时:

<GetSavedReportResponse>
    <ResponseType>Success</ResponseType>
    <FileModifiedDateTime>2012-01-03T17:05:04</FileModifiedDateTime>
    <FileSizeBytes>7816</FileSizeBytes>
    <FileDataFormat>XML</FileDataFormat>
    <FileData>
        <Zthes>
            <term>
                <termId>49555</termId>
                <termUpdate>add</termUpdate>
                <termName>Active Personnel</termName>
                <termVocabulary>People Status Global Term1</termVocabulary>
                <termVocabulary>Global People Status Term1</termVocabulary>
                <termCategory>PDA Term1</termCategory>
                <termCategory>PDI Term1</termCategory>
                <termCategory>GLB Term1</termCategory>
                <relation weight="100">
                    <termId>49556</termId>
                    <relationType>EQ Term1</relationType>
                    <termName>term name Term1</termName>
                    <termVocabulary>term vocabulary Term1</termVocabulary>
                </relation>
                <relation weight="100">
                    <termId>49557</termId>
                    <relationType>BT</relationType>
                    <termName>General Active Personnel</termName>
                    <termVocabulary>People Status Global Updated</termVocabulary>
                </relation>
            </term>
            <term>
                <termId>49556</termId>
                <termUpdate>add</termUpdate>
                <termName>Leave of Absence Personnel</termName>
                <termVocabulary>People Status Global Term2</termVocabulary>
                <termCategory>GLB Term2</termCategory>
                <termCategory>PDI Term2</termCategory>
                <relation weight="100">
                    <relationType>BT</relationType>
                    <termId>49554</termId>
                    <termName>General Non-Active Personnel Term2</termName>
                    <termVocabulary>People Status Global Term2</termVocabulary>
                </relation>
            </term>
        </Zthes>
    </FileData>
</GetSavedReportResponse>

产生想要的正确结果:

HDR";"PIGLSSTD";"20120112045620";"F":
"GL";"PDA Term1";"People Status Global Term1";"term vocabulary Term1";
"GL";"PDA Term1";"People Status Global Term1";"People Status Global Updated";
"GL";"PDA Term1";"Global People Status Term1";"term vocabulary Term1";
"GL";"PDA Term1";"Global People Status Term1";"People Status Global Updated";
"GL";"PDI Term1";"People Status Global Term1";"term vocabulary Term1";
"GL";"PDI Term1";"People Status Global Term1";"People Status Global Updated";
"GL";"PDI Term1";"Global People Status Term1";"term vocabulary Term1";
"GL";"PDI Term1";"Global People Status Term1";"People Status Global Updated";
"GL";"GLB Term1";"People Status Global Term1";"term vocabulary Term1";
"GL";"GLB Term1";"People Status Global Term1";"People Status Global Updated";
"GL";"GLB Term1";"Global People Status Term1";"term vocabulary Term1";
"GL";"GLB Term1";"Global People Status Term1";"People Status Global Updated";
"GL";"GLB Term2";"People Status Global Term2";"People Status Global Term2";
"GL";"PDI Term2";"People Status Global Term2";"People Status Global Term2";
FTR;14
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 xslt 从 xml 转换而来的平面文件中的行数 的相关文章

随机推荐

  • 将选定的文本扩展为整个单词

    在 IE 中我可以这样做 var rng document selection createRange rng expand word txt rng text 如何在 IE 之外执行相同的操作 使用 getSelection 选择整个单词
  • PyXML 安装 - 此平台上不存在 memmove

    我尝试使用 pip 安装 pyxml 库 但在安装过程中出现以下错误 extensions expat lib xmlparse c 75 2 error error memmove does not exist on this platf
  • Google 日历 API 服务帐户错误

    我收到这个错误 error errors domain calendar reason forbiddenForServiceAccounts message Service accounts cannot invite attendees
  • 在 JPA/Hibernate NamedQuery 中指定数据库函数

    如何在 JPA Hibernate NamedQuery 中指定数据库函数 奇怪的是 hibernate JQL 无法识别 RIGHT 函数 在不使用子字符串的情况下 hibernate 中是否有一种方法可以在 NamedQuery 中指定
  • 仅接受 IComparable 的 SortedList

    我有一个界面IScriptItem实现IComparable
  • Log4j 配置 - 不同的日志记录到不同的文件

    对于某些人来说 这可能是一个非常简单的问题 但就我个人而言 我发现 Log4j 配置非常困难 并且学习进行脑部手术可能不那么具有挑战性 我正在尝试让多个记录器登录到不同的文件中 这是我的 log4j properties 文件中的内容 Ro
  • Github桌面认证失败

    使用 Windows 10 Github Desktop Git 2 19 1 windows 1 64位 VisualStudio VSTS 背景 设法添加我的计算机中的存储库 但我无法用它做任何事情 我可以访问远程存储库 我之前使用过
  • Magento getUrl 不适用于目录/类别对象?

    我已经能够实例化一个类别对象来检索其名称 但是当我使用getUrl方法它不会返回类别列表页面的 URL 或任何其他内容 上面的代码会产生 HTML 输出 li a href name of sub cat a li 有谁知道我如何从
  • 黑莓 Twitter 集成以发布照片

    我正在开发一个应用程序 用户可以在运行 OS 5 0 的 BlackBerry Storm 和 Torch 系列手机上将照片分享到 Facebook 和 Twitter 对于 Facebook 我使用了草莓项目 但对于 Twitter 我找
  • 如何使用多个 CBCharacteristicProperties 和权限初始化 CBMutableCharacteristic

    我正在创建一个新的 CBMutableCharacteristic 以在我正在制作的蓝牙应用程序中使用 我从教程中得到了一些代码 如下所示 customCharacteristic CBMutableCharacteristic alloc
  • 如何在React项目中添加Service Worker

    我想在我的 React 项目中添加 Service Worker 项目已准备就绪 默认服务似乎不起作用 即使当我尝试导入它时 它也会出现以下错误 尝试导入错误 registerServiceWorker 不包含默认导出 导入为 regist
  • GWT 模块的模块标记中的 rename-to 是什么意思?

    GWT 模块中模块标记的 rename to 属性是什么意思 是可选的吗 它 导致编译器表现得好像模块的名称与长的 完全限定的名称不同 http code google com webtoolkit doc latest DevGuideO
  • 在 MySql 中提取具有特定模式的子字符串

    我有一个文本字段 如下所示 option A sum A g3et B 我想获取里面的文字 没有重复项 获得意义 A B 不可能出现双重like的情况 我知道这是一种在数据库中保存数据的可怕方法 我无法更改数据的保存方式 我只需要从本专栏中
  • 红宝石。合并嵌套哈希而不覆盖

    我有一个嵌套哈希 X 1 2 3 gt X O 2 3 gt X O X 3 gt X O X O 我想合并给定的嵌套哈希 X 1 2 3 gt X O 2 3 gt X O 2 X gt X O O X 这样 X 1 2 3 gt X O
  • 如何更改Windows Phone应用程序中按钮的背景颜色?

    我正在使用 C 和 silverlight 4 开发 Windows Phone 7 应用程序 我是 silverlight 的新手 我的应用程序中有两个用于不同目的的按钮 我想在单击按钮时动态更改按钮的颜色 所以我使用下面的代码 Inco
  • 办公自动化、VSTO 和 Open XML SDK 之间有什么区别?

    办公自动化 VSTO 和 Open XML SDK 之间有什么区别 我们需要所有这些还是其中一些已经过时了 办公自动化是指使用 COM 互操作以编程方式操作 Office 程序 或更常见的是通过 Office 程序操作 Office 文档
  • 默默忽略remove()

    实体 A 引用 多对一 实体 B 具有从 B 到 A 的反向 映射 引用 此外 还存在 A 到 C 的引用以及 C 到 A 的反向引用 当我发出entityManager remove A 然后flush 时 不会生成 删除 但也没有例外
  • ld:找不到 AudioUnit 框架

    我正在添加另一个项目 即使我添加了所需的所有库 我也会收到此错误 这是错误详细信息 Ld Users alialzahrani Library Developer Xcode DerivedData IMS3 ezltqoccjhjpvua
  • 如何在 React.js 中预加载图像?

    如何在 React js 中预加载图像 我有下拉选择组件 其工作方式类似于菜单 但我必须预加载项目的图像图标 因为有时它们在第一次打开时不可见 我努力了 https github com sambernard react preload h
  • 使用 xslt 从 xml 转换而来的平面文件中的行数

    下面是我用来将 xml 转换为平面文件的 xsl 它还满足各种其他所需条件