首页 文章

XSLT新手 - 循环和匹配元素

提问于
浏览
1

全新的XSLT,但设法破解我的方式来提取我需要的数据...

我现在停留在将数据提取为必需的格式以上传到数据库...我需要将每个PGBLK与其相应的TASK匹配(请参见下面的示例) .

很确定我想使用XSLT循环,但说实话我不确定,任何帮助将不胜感激!

这是源XML:

<?xml version="1.0" encoding="UTF-8"?>
 <PGBLK CHAPNBR="05" CHG="U" CONFNBR="0" PGBLKNBR="2" REVDATE="20160608" SECTNBR="00" SUBJNBR="00">
  <TASK CHAPNBR="05" CHG="R" FUNC="912" PGBLKNBR="2" REVDATE="20160520" SECTNBR="00" SEQ="001"
    SUBJNBR="00"/>
    <EFFECT EFFRG="000" EFFTEXT="ALL"/>
  <TASK CHAPNBR="05" CHG="R" FUNC="910" PGBLKNBR="2" REVDATE="20160520" SECTNBR="00" SEQ="801"
    SUBJNBR="00">
    <EFFECT EFFRG="000" EFFTEXT="ALL"/>
   </TASK>
</PGBLK>

目前的XSLT:

<xsl:template match="RTM">

 <REFERENCES>
    <xsl:attribute name="DATE">
        <xsl:value-of select="current-date()"/>
    </xsl:attribute>
        <xsl:for-each select="//PGBLK">
            <REF>
             <PGBLK>
              <xsl:value-of select="concat(@CHAPNBR, '-', @SECTNBR, '-', @SUBJNBR, '-', @PGBLKNBR, '-0')"/>
                </PGBLK>
            <TASK>
             <xsl:value-of select="TASK/concat(@CHAPNBR, '-', @SECTNBR, '-', @SUBJNBR, '-', @FUNC, '-', @SEQ, ',')"/>
            </TASK>
            </REF>
        </xsl:for-each>
    </REFERENCES>
</xsl:template>

当前XML输出:

<REFERENCES DATE="2017-11-15-06:00">
 <REF>
  <PGBLK>05-00-00-2-0</PGBLK>
  <TASK>05-00-00-912-001, 05-00-00-910-801,</TASK>
 </REF>
</REFERENCES>

期望的XML输出:

<REFERENCES DATE="2017-11-15-06:00">
 <REF>
  <PGBLK>05-00-00-2-0</PGBLK><TASK>05-00-00-912-001</TASK>
  <PGBLK>05-00-00-2-0</PGBLK><TASK>05-00-00-910-801</TASK>
 </REF>
</REFERENCES>

2 回答

  • 0

    如果你想将 PGBLK 映射到 REF ,然后将每个 TASK 映射到一对 PGBLKTASK ,那么以下内容应该有所帮助:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    
    <xsl:output indent="yes"/>
    
    <xsl:template match="RTM">
        <REFERENCES DATA="{current-date()}">
            <xsl:apply-templates/>
        </REFERENCES>
    </xsl:template>
    
    <xsl:template match="PGBLK">
        <REF>
            <xsl:apply-templates select="TASK"/>
        </REF>
    </xsl:template>
    
    <xsl:template match="TASK">
        <PGBLK>
            <xsl:value-of select="../((@CHAPNBR, @SECTNBR, @SUBJNBR, @PGBLKNBR)/string(), '0')" separator="-"/>
        </PGBLK>
        <xsl:copy>
            <xsl:value-of select="@CHAPNBR, @SECTNBR, @SUBJNBR, @FUNC, @SEQ" separator="-"/>
        </xsl:copy>
    </xsl:template>
    
    </xsl:transform>
    

    http://xsltransform.net/ehVYZP2

  • 1

    关于如何将 <TASK> 元素映射到其父 <PGBLK> 元素的主要问题,这非常简单:选择 <TASK> 进行转换,并在其模板内部

    • 通过XPath表达式访问父级(例如 ..

    • 通过来自词汇周围范围的变量或通过模板动态范围提供的模板参数提供父数据 .

    由于 <PGBLK> 可能没有那么严重的可能性,这个过程有点复杂 . 这是一个贴近原始模板的变体:

    <xsl:template match="RTM">
      <!-- NOTE: no need for xsl-attribute unless the attribute name
           or namespace is dynamically determined -->
      <REFERENCES DATE="{current-date()}">
        <xsl:for-each select=".//PGBLK">
          <!-- capture the relevant details of the current PGBLK for re-use
               in nested contexts -->
          <xsl:variable name="blk-code"
              select="concat(@CHAPNBR, '-', @SECTNBR, '-', @SUBJNBR, '-', @PGBLKNBR, '-0')"/>
    
          <REF>
    
            <!-- transform all the PGBLK's TASKs, providing a PGBLK with each: -->
            <xsl:for-each select="TASK">
              <PGBLK>
                <xsl:value-of select="$blk-code"/>
              </PGBLK>
              <TASK>
                <xsl:value-of select="concat(@CHAPNBR, '-', @SECTNBR, '-', @SUBJNBR, '-', @FUNC, '-', @SEQ)"/>
              </TASK>
            </xsl:for-each>
    
            <!-- handle the case of a PGBLK without any TASKs (in which case the
                 for-each will not have added anything to the result tree: -->
            <xsl:if test="not(TASK)">
              <PGBLK>
                <xsl:value-of select="$blk-code"/>
              </PGBLK>
              <TASK/>
            </xsl:if>
    
          </REF>
        </xsl:for-each>
      </REFERENCES>
    </xsl:template>
    

相关问题