【问题标题】:Getting redundancy in xslt output and How to get exact sequence of elements in xslt output as Input在 xslt 输出中获取冗余以及如何在 xslt 输出中获取精确的元素序列作为输入
【发布时间】:2014-01-31 11:33:52
【问题描述】:

我是 XSLT 的新手。我正在将 XML 文档转换为 XHTML 格式。在我的 XSLT 输出中,我得到了冗余并且没有得到输出中元素的确切序列作为输入。

我的意见:

<TLV1 ID="B01429413.0-7">
  <P>All rights reserved.</P>
  <P>
    <E T="I">Production Services:</E>Aptara, Inc.
  </P>
  <LK>ABCD !!!!!!</LK>
  <P>
    <E T="I">ACSM's Publications Committee Chair:</E>Jeffrey L.
    Roitman, EdD, FACSM
  </P>
  <P>
    <E T="I">ACSM Group Publisher:</E>D. Mark Robertson
  </P>
  <LK>WXYZ !!!!!!</LK>
  <P>&#160;&#160;&#160;p. cm.</P>
  <P>
    To purchase additional copies of this book, call our customer
    service department at
    <E T="B">(301) 223-2320</E>. International customers should call.
  </P>
  <P>
    <E T="BIT">
      Visit Lippincott Williams &amp; Wilkins on the
      Internet: http://www.lww.com.
    </E>Lippincott Williams &amp;
    Wilkins customer service representatives are available from 8:30
    am to 6:00 pm, EST.
  </P>
</TLV1>

我应用的 XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="html" omit-xml-declaration="yes"
  indent="yes" />
  <xsl:strip-space elements="*" />
  <xsl:template match="/">
    <xsl:apply-templates select="//TLV1"/>
  </xsl:template>
  <xsl:template match="TLV1">
    <div>
      <xsl:attribute name="class">TLV1</xsl:attribute>
      <xsl:if test="P">
        <xsl:for-each select="P">
          <div>
            <xsl:attribute name="class">P</xsl:attribute>
            <xsl:apply-templates select="node()[1]" />
            <xsl:if test="E">
              <xsl:for-each select="E">
                <span>
                  <xsl:attribute name="class">emph_I</xsl:attribute>
                  <xsl:apply-templates />
                </span>
                <xsl:apply-templates select="following-sibling::node()[1]" />
              </xsl:for-each>
            </xsl:if>
          </div>
        </xsl:for-each>
        <xsl:if test="LK">
          <xsl:for-each select="LK">
            <xsl:value-of select="LK"/>
          </xsl:for-each>
        </xsl:if>
      </xsl:if>
    </div>
  </xsl:template>
</xsl:stylesheet>

当我将此 xslt 应用于输入时,所有“P”元素首先处理,然后是“LK”,所以我失去了输出顺序。并在“div”和“span”元素中获得冗余。

我的输出:

<div xmlns="http://www.w3.org/1999/xhtml" class="TLV1">
  <div class="P">All rights reserved.</div>
  <div class="P">
    Production Services:<span class="emph_I">Production Services:</span>Aptara, Inc.
  </div>
  <div class="P">
    ACSM's Publications Committee Chair:<span class="emph_I">ACSM's Publications Committee Chair:</span>Jeffrey L.
    Roitman, EdD, FACSM
  </div>
  <div class="P">
    ACSM Group Publisher:<span class="emph_I">ACSM Group Publisher:</span>D. Mark Robertson
  </div>
  <div class="P">&nbsp;&nbsp;&nbsp;p. cm.</div>
  <div class="P">
    To purchase additional copies of this book, call our customer
    service department at
    <span class="emph_I">(301) 223-2320</span>. International customers should call.
  </div>
  <div class="P">
    Visit Lippincott Williams &amp; Wilkins on the
    Internet: http://www.lww.com.<span class="emph_I">
      Visit Lippincott Williams &amp; Wilkins on the
      Internet: http://www.lww.com.
    </span>Lippincott Williams &amp;
    Wilkins customer service representatives are available from 8:30
    am to 6:00 pm, EST.
  </div>
  <div class="LK">
    ABCD !!!!!!
  </div>
  <div class="LK">
    WXYZ !!!!!!
  </div>
</div>

在“LK”处理之后首先处理的所有“P”元素。而 ACSM Group Publisher:在“div”和“span”中获得了 2 次。

预期输出:我想要这个输出..

<div class="TLV1">
  <div class="P">All rights reserved.</div>
  <div class="P">
    <span class="emph_I">Production Services:</span>Aptara,Inc.
  </div>
  <div class="LK">
    ABCD !!!!!!
  </div>
  <div class="P">
    <span class="emph_I">
      ACSM's Publications Committee
      Chair:
    </span>Jeffrey L. Roitman, EdD, FACSM
  </div>
  <div class="P">
    <span class="emph_I">ACSM Group Publisher:</span>D. Mark Robertson
  </div>
  <div class="LK">
    WXYZ !!!!!!
  </div>
  <div class="P">&#160;&#160;&#160;p. cm.</div>
  <div class="P">
    To purchase additional copies of this book, call
    our customer service department at
    <span class="emph_B">(301) 223-2320</span>. International
    customers should call.
  </div>
  <div class="P">
    <span class="emph_BIT">
      Visit Lippincott Williams &amp; Wilkins on
      the Internet:
      <a href="http://www.lww.com">
        http://www.lww.com
      </a>.
    </span>Lippincott Williams &amp; Wilkins
    customer service representatives are available from 8:30 am to
    6:00 pm, EST.
  </div>
</div>

抱歉发了这么久!!

【问题讨论】:

    标签: java xslt


    【解决方案1】:

    您需要让模板在这里为您完成工作,而不是一堆ifs 和for-eaches:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns="http://www.w3.org/1999/xhtml">
      <xsl:output method="html" omit-xml-declaration="yes" indent="yes" />
      <xsl:strip-space elements="*" />
    
      <xsl:template match="/">
        <xsl:apply-templates select="//TLV1"/>
      </xsl:template>
    
      <xsl:template match="TLV1 | P | LK">
        <div class="{local-name()}">
          <xsl:apply-templates />
        </div>
      </xsl:template>
    
      <xsl:template match="E">
        <span class="emph_I">
          <xsl:apply-templates />
        </span>
      </xsl:template>
    </xsl:stylesheet>
    

    在您的示例输入上运行时,结果是:

    <div class="TLV1" xmlns="http://www.w3.org/1999/xhtml">
      <div class="P">All rights reserved.</div>
      <div class="P">
        <span class="emph_I">Production Services:</span>Aptara, Inc.
      </div>
      <div class="LK">ABCD !!!!!!</div>
      <div class="P">
        <span class="emph_I">ACSM's Publications Committee Chair:</span>Jeffrey L.
        Roitman, EdD, FACSM
      </div>
      <div class="P">
        <span class="emph_I">ACSM Group Publisher:</span>D. Mark Robertson
      </div>
      <div class="LK">WXYZ !!!!!!</div>
      <div class="P">   p. cm.</div>
      <div class="P">
        To purchase additional copies of this book, call our customer
        service department at
        <span class="emph_I">(301) 223-2320</span>. International customers should call.
      </div>
      <div class="P">
        <span class="emph_I">
          Visit Lippincott Williams &amp; Wilkins on the
          Internet: http://www.lww.com.
        </span>Lippincott Williams &amp;
        Wilkins customer service representatives are available from 8:30
        am to 6:00 pm, EST.
      </div>
    </div>
    

    【讨论】:

      【解决方案2】:

      您的控制流在这里似乎不必要地复杂化。在大多数情况下,更好的方法是从身份转换开始,为您要处理的每个案例添加模板,然后递归调用&lt;xsl:apply-templates&gt; 来选择当前节点的每个子节点并依次设置其样式(及其内容) .尽可能编写规则——一般和例外——而不是程序。

      此外,当您不需要计算属性的值时,您确实应该更多地利用文字结果属性。

      如果我没看错你的代码,你想要的是:

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
        <xsl:template match="@*|node()">
          <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
        </xsl:template>
      
        <xsl:template match="TLV1">
          <div class="TLV1">
            <xsl:apply-templates/>
          </div>
        </xsl:template>
      
        <xsl:template match="P">
          <div class="P">
            <xsl:apply-templates/>
          </div>
        </xsl:template>
      
        <xsl:template match="LK">
          <div class="LK">
            <xsl:apply-templates/>
          </div>
        </xsl:template>
      
        <xsl:template match="E">
          <span class="emph_I"> 
            <xsl:apply-templates/>
          </span>
        </xsl:template>
      
      
      </xsl:stylesheet>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-31
        • 1970-01-01
        • 1970-01-01
        • 2022-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多