XSLT将带有重复sibbling的xml转换为平面文件

时间:2012-01-17 07:30:02

标签: xslt xslt-2.0 xslt-1.0 xslcompiledtransform

我有一个像下面这样的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</termVocabulary>
        <termVocabulary>Global People Status</termVocabulary>
        <termCategory>PDA</termCategory>
        <termCategory>PDI</termCategory>
        <termCategory>GLB</termCategory>
        <relation weight="100">
          <termId>49556</termId>
          <relationType>EQ</relationType>
          <termName>term name</termName>
          <termVocabulary>term vocabulary</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</termVocabulary>
        <termCategory>GLB</termCategory>
        <termCategory>PDI</termCategory>
        <relation weight="100">
          <relationType>BT</relationType>
          <termId>49554</termId>
          <termName>General Non-Active Personnel</termName>
          <termVocabulary>People Status Global</termVocabulary>
        </relation>
      </term>
    </Zthes>
  </FileData>
</GetSavedReportResponse>

我需要将其转换为平面文件。为此,我写了以下xsl

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" />
  <xsl:template match="Zthes">
    <xsl:text>&#10;</xsl:text>
    <xsl:for-each select="term">
      <xsl:text>"</xsl:text>
      <xsl:text>GL</xsl:text>
      <xsl:text>"</xsl:text>
      <xsl:text>;</xsl:text>
      <xsl:text>"</xsl:text>
      <xsl:for-each select="termCategory">
        <xsl:value-of select="." />
      </xsl:for-each>
      <xsl:text>"</xsl:text>
      <xsl:text>;</xsl:text>
      <xsl:text>"</xsl:text>
      <xsl:for-each select="termVocabulary">
        <xsl:value-of select="." />
      </xsl:for-each>
      <xsl:text>"</xsl:text>
      <xsl:text>;</xsl:text>
      <xsl:text>"</xsl:text>
      <xsl:for-each select="relation/termVocabulary">
          <xsl:value-of select="." />
      </xsl:for-each>
      <xsl:text>"</xsl:text>
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

所以,输出应该是
“HDR”, “文本”, “20120112045620”; “F”
“GL”;“PDA”;“People Status Global”;“词汇词汇”
“GL”;“PDA”;“People Status Global”;“People Status Global Updated”
“GL”;“PDA”;“全球人民身份”;“术语词汇”
“GL”;“PDA”;“全球人民身份”;“人民身份全球更新”
“GL”;“PDI”;“People Status Global”;“词汇词汇”
“GL”;“PDI”;“People Status Global”;“People Status Global Updated”
“GL”;“PDI”;“全球人民身份”;“术语词汇”
“GL”;“PDI”;“全球人民身份”;“人口状况全球更新”
“GL”;“GLB”;“People Status Global”;“词汇词汇”
“GL”;“GLB”;“People Status Global”;“People Status Global Updated”
“GL”;“GLB”;“全球人民身份”;“术语词汇”
“GL”;“GLB”;“全球人民身份”;“人口状况全球更新”
“FTR”; 12

我的xsl我是单行:
“GL”;“PDAPDIGLB”;“人们身份全球全球人口状况”;“术语词汇人员状态全球更新”

标题行:
“HDR”, “PIGLSSTD”, “20120112045620”; “F”:
应该在开头附加一个页脚行     “FTR”;
    
在底部。

1 个答案:

答案 0 :(得分:0)

你想要这样的东西

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

 <my:defaults>
   <termCat/>
   <termVocab/>
 </my:defaults>

 <xsl:variable name="vDefaults" select="document('')/*/my:defaults"/>

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

 <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</termVocabulary>
                <termVocabulary>Global People Status</termVocabulary>
                <termCategory>PDA</termCategory>
                <termCategory>PDI</termCategory>
                <termCategory>GLB</termCategory>
                <relation weight="100">
                    <termId>49556</termId>
                    <relationType>EQ</relationType>
                    <termName>term name</termName>
                    <termVocabulary>term vocabulary</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</termVocabulary>
                <termCategory>GLB</termCategory>
                <termCategory>PDI</termCategory>
                <relation weight="100">
                    <relationType>BT</relationType>
                    <termId>49554</termId>
                    <termName>General Non-Active Personnel</termName>
                    <termVocabulary>People Status Global</termVocabulary>
                </relation>
            </term>
        </Zthes>
    </FileData>
</GetSavedReportResponse>

产生了想要的正确结果

"GL";"PDA";"People Status Global";"term vocabulary";
"GL";"PDA";"People Status Global";"People Status Global Updated";
"GL";"PDA";"Global People Status";"term vocabulary";
"GL";"PDA";"Global People Status";"People Status Global Updated";
"GL";"PDI";"People Status Global";"term vocabulary";
"GL";"PDI";"People Status Global";"People Status Global Updated";
"GL";"PDI";"Global People Status";"term vocabulary";
"GL";"PDI";"Global People Status";"People Status Global Updated";
"GL";"GLB";"People Status Global";"term vocabulary";
"GL";"GLB";"People Status Global";"People Status Global Updated";
"GL";"GLB";"Global People Status";"term vocabulary";
"GL";"GLB";"Global People Status";"People Status Global Updated";
"GL";"GLB";"People Status Global";"People Status Global";
"GL";"PDI";"People Status Global";"People Status Global";

解释:您只想在形成完整的行时才输出 - 而不是在此之前。

更新:OP适用于禁用document()功能的环境。他还想要一个页眉和一个页脚。

在这种情况下,可以使用稍微修改过的转换(使用exslt:node-set()扩展函数):

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