2

我的输入 XML 包含以下内容,

<root>
    <entry>
        <type>U</type>
        <value>111</value>
    </entry>
    <entry>
        <type>X</type>
        <value>222</value>
    </entry>
    <entry>
        <type>E</type>
        <value>333</value>
    </entry>
    <entry>
        <type>Q</type>
        <value>444</value>
    </entry>
    <entry>
        <value>555</value>
    </entry>
    <entry>
        <value>666</value>
    </entry>
</root>

输出我需要,

<ROOT>
    <ENTRY>
        <SLNO>1</SLNO>
        <VALUE>111</VALUE>
    </ENTRY>
    <ENTRY>
        <SLNO>1</SLNO>
        <VALUE>222</VALUE>
    </ENTRY>
    <ENTRY>
         <SLNO>1</SLNO>
         <VALUE>333</VALUE>
    </ENTRY>
    <ENTRY>
        <SLNO>2</SLNO>
        <VALUE>444</VALUE>
    </ENTRY>
    <ENTRY>
        <SLNO>3</SLNO>
        <VALUE>555<VALUE>
    </ENTRY>
    <ENTRY>
        <SLNO>4</SLNO>
        <VALUE>666</VALUE>
    </ENTRY>
</ROOT>

要求是为 X、E、Y 和 K 以外的类型以及类型标签本身缺失的情况生成新的 SLNO。对于所有其他类型,我们需要显示新的序列号。

我已经写了一个 for-each to for 相同的,因为我必须对另一个值进行更多处理,所以 for-each 是必须的。

我怎样才能做到这一点?

我的示例 XSL 代码如下,

<xsl:for-each select="/ROOT/ENTRY">
    <xsl:if test="(TYPE != 'X') and (TYPE != 'E')">             
        <xsl:text><![CDATA[<SLNO>]]></xsl:text>


        <xsl:if test="((type != 'X') and (type != 'E') and (type != 'Y') and (type != 'K'))">
            <xsl:value-of select="count(preceding::type[((. != 'X' and . != 'E' and . != 'Y' and . !='K') or . ='')]) + 1" /> 
        </xsl:if>                                                               

        <xsl:if test="(type = 'X') or (type = 'E') or (type = 'Y') or (type = 'K')">
            <xsl:value-of select="count(preceding::type[(. != 'X' and . != 'E' and . != 'Y' and . !='K')])" />                                                        
        </xsl:if>



        <xsl:text><![CDATA[</SLNO>]]></xsl:text>
    </xsl:if>
    <!-- Printing remaining values -->
</xsl:for-each>

这段代码的问题是,我没有得到没有类型标签的条目的 SLNO。

请帮忙。

4

3 回答 3

3

Warning:

The code provided in this question isn't syntactically legal XSLT -- there is no XSLT instruction <xsl:type-of> in the language.

Also, this code generates text that looks like opening and closing element tags.

Never do such thing, when you want to generate an element -- test is not markup -- it is just ... text.; and the generated output is not what you wanted it to be.


Solution:

This transformation:

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

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="entry">
  <entry>
   <slno>
    <xsl:value-of select=
     "count((.
            |
              preceding-sibling::entry)
               [not(type)
               or
                not(contains('+X+E+Y+K+', concat('+', type, '+')))
                ]
            )"/>
    </slno>
    <xsl:apply-templates select="node()[not(self::type)]"/>
  </entry>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<root>
    <entry>
        <type>U</type>
        <value>111</value>
    </entry>
    <entry>
        <type>X</type>
        <value>222</value>
    </entry>
    <entry>
        <type>E</type>
        <value>333</value>
    </entry>
    <entry>
        <type>Q</type>
        <value>444</value>
    </entry>
    <entry>
        <value>555</value>
    </entry>
    <entry>
        <value>666</value>
    </entry>
</root>

produces the wanted, correct result:

<root>
   <entry>
      <slno>1</slno>
      <value>111</value>
   </entry>
   <entry>
      <slno>1</slno>
      <value>222</value>
   </entry>
   <entry>
      <slno>1</slno>
      <value>333</value>
   </entry>
   <entry>
      <slno>2</slno>
      <value>444</value>
   </entry>
   <entry>
      <slno>3</slno>
      <value>555</value>
   </entry>
   <entry>
      <slno>4</slno>
      <value>666</value>
   </entry>
</root>
于 2012-07-02T12:45:06.833 回答
1
  • Don't EVER use <xsl:text><![CDATA[<SLNO>]]></xsl:text> to output XML elements. Ever. This is just bad and wrong (and ugly at that, but ugly isn't even an argument here).
  • Do not use <xsl:for-each>. Use <xsl:template>/<xsl:apply-templates>
  • You can use translate() to your advantage, see below. WARNING This shortcut will only work if you have single letter types.
  • Think if you really need to upper-case those element names. This does not look like a necessary change to me. Keeping the original case is much easier because you can use the identity template. Also see Dimitre's answer.

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
  <xsl:output indent="yes" />

  <xsl:template match="root">
    <ROOT>
      <xsl:apply-templates select="entry" />
    </ROOT>
  </xsl:template>

  <xsl:template match="entry">
    <ENTRY>
      <SLNO>
        <xsl:value-of select="
          count(
            (. | preceding-sibling::entry)[not(type and translate(type, 'EKXY', '') = '')]
          )
        " />
      </SLNO>
      <xsl:apply-templates select="value" />
    </ENTRY>
  </xsl:template>

  <xsl:template match="value">
    <VALUE>
      <xsl:value-of select="." />
    </VALUE>
  </xsl:template>
</xsl:stylesheet>

produces:

<ROOT>
  <ENTRY>
    <SLNO>1</SLNO>
    <VALUE>111</VALUE>
  </ENTRY>
  <ENTRY>
    <SLNO>1</SLNO>
    <VALUE>222</VALUE>
  </ENTRY>
  <ENTRY>
    <SLNO>1</SLNO>
    <VALUE>333</VALUE>
  </ENTRY>
  <ENTRY>
    <SLNO>2</SLNO>
    <VALUE>444</VALUE>
  </ENTRY>
  <ENTRY>
    <SLNO>3</SLNO>
    <VALUE>555</VALUE>
  </ENTRY>
  <ENTRY>
    <SLNO>4</SLNO>
    <VALUE>666</VALUE>
  </ENTRY>
</ROOT>
于 2012-07-02T12:45:49.970 回答
0

在你的支票上

(TYPE != 'X') and (TYPE != 'E')

您需要为没有 TYPE 节点的 ENTRY 节点包含一个“OR”语句

就像是

((TYPE != 'X') and (TYPE != 'E')) or count(TYPE) = 0
于 2012-07-02T12:16:01.473 回答