本文介绍了使用 xslt 从 xml 转换的平面文件中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below is the xsl I have used for transforming an xml to a flat file, whci also satisfies various other required conditions.

<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 如下所示:

sample XML looks like below:

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

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

Everything works fine except the count of rows in the footer, which should be the number of rows (not including the header and footer) in the flat file.

预期输出:

"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

这篇关于使用 xslt 从 xml 转换的平面文件中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 06:43