我是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>   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 & Wilkins on the
Internet: http://www.lww.com.
</E>Lippincott Williams &
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"> 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 & Wilkins on the
Internet: http://www.lww.com.<span class="emph_I">
Visit Lippincott Williams & Wilkins on the
Internet: http://www.lww.com.
</span>Lippincott Williams &
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”中获得2次,然后在“ 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">   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 & Wilkins on
the Internet:
<a href="http://www.lww.com">
http://www.lww.com
</a>.
</span>Lippincott Williams & Wilkins
customer service representatives are available from 8:30 am to
6:00 pm, EST.
</div>
</div>
很抱歉,该职位!
最佳答案
您需要让模板在这里为您完成工作,而不是一堆if
和for-each
es:
<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 & Wilkins on the
Internet: http://www.lww.com.
</span>Lippincott Williams &
Wilkins customer service representatives are available from 8:30
am to 6:00 pm, EST.
</div>
</div>