Xslt 如何在标题之间获取数据?
我是xslt新手。我希望将以下输入转换为如下所示的输出: 输入:Xslt 如何在标题之间获取数据?,xslt,xslt-1.0,Xslt,Xslt 1.0,我是xslt新手。我希望将以下输入转换为如下所示的输出: 输入: <ATTRIBUTE-VALUE> <THE-VALUE> <div xmlns="http://www.w3.org/1999/xhtml"> <h1 dir="ltr" id="_1536217498885">Main Description</h1> Line1 The main descrip
<ATTRIBUTE-VALUE>
<THE-VALUE>
<div xmlns="http://www.w3.org/1999/xhtml">
<h1 dir="ltr" id="_1536217498885">Main Description</h1>
Line1 The main description text goes here.
<p>Line2 The main description text goes here.</p>
<p>Line3 The main description text goes here.</p>
<p><img alt="Embedded Image" class="embeddedImageLink" id="_1536739954166" src="_9c3778a0-d596-4eef-85fa-052a5e1b2166.jpg"/></p>
<h1 dir="ltr" id="_1536217498886">Key Consideration</h1>
<p>Line1 The key consideration text goes here.</p>
<p>Line2 The key consideration text goes here.</p>
<h1 dir="ltr" id="_1536217498887">Skills</h1>
<p>Line1 The Skills text goes here.</p>
<p>Line2 The Skills text goes here.</p>
<p>Line3 The Skills text goes here.</p>
<h1 dir="ltr" id="_1536217498888">Synonyms</h1>
<p>The Synonyms text goes here.</p>
</div>
</THE-VALUE>
</ATTRIBUTE-VALUE>
输出应为:
<MainDescription>
<![CDATA[
<p>Line1 The main description text goes here.</p>
<p>Line2 The main description text goes here.</p>
<p>Line3 The main description text goes here.</p>
<p><img alt="Embedded Image" class="embeddedImageLink" id="_1536739954166" src="_9c3778a0-d596-4eef-85fa-052a5e1b2166.jpg"/></p>
]]>
</MainDescription>
<KeyConsiderations>
<![CDATA[
<p>Line1 The key consideration text goes here.</p>
<p>Line2 The key consideration text goes here.</p>
]]>
</KeyConsiderations>
<Skills>
<p>Line1 The Skills text goes here.</p>
<p>Line2 The Skills text goes here.</p>
<p>Line3 The Skills text goes here.</p>
</Skills>
<Synonyms>
<p>The Synonyms text goes here.</p>
</Synonyms>
我希望数据介于和之间,它可以包含任何应在输出中生成的html标记。我在以下位置尝试了代码:。但它仅在数据包含在html标记下时才提供数据。请提供如何实现所需输出的指针
XSL代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xhtml exsl"
version="1.0">
<xsl:import href="http://lenzconsulting.com/xml-to-string/xml-to-string.xsl"/>
<xsl:output method="xml" indent="yes"
cdata-section-elements="MainDescription KeyConsideration"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="h1-group" match="xhtml:div/*[not(self::xhtml:h1)]" use="generate-id(preceding-sibling::xhtml:h1[1])"/>
<xsl:template match="xhtml:div[xhtml:h1]">
<xsl:apply-templates select="xhtml:h1"/>
</xsl:template>
<xsl:template match="xhtml:h1">
<xsl:element name="{translate(., ' ', '')}">
<xsl:variable name="rtf-with-xhtml-ns-stripped">
<xsl:apply-templates select="key('h1-group', generate-id())"/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($rtf-with-xhtml-ns-stripped)/node()" mode="xml-to-string"/>
</xsl:element>
</xsl:template>
<xsl:template match="xhtml:p">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
</xsl:stylesheet>
我得到的输出是:
<ATTRIBUTE-VALUE>
<THE-VALUE>
<MainDescription><![CDATA[<p>Line2 The main description text goes here.</p><p><img alt="Embedded Image" class="embeddedImageLink" id="_1536739954166" src="_9c3778a0-d596-4eef-85fa-052a5e1b2166.jpg" xmlns="http://www.w3.org/1999/xhtml"/></p>]]></MainDescription>
<KeyConsideration><![CDATA[<p>Line1 The key consideration text goes here.</p><p>Line2 The key consideration text goes here.</p>]]></KeyConsideration>
<Skills><p>Line1 The Skills text goes here.</p><p>Line2 The Skills text goes here.</p><p>Line3 The Skills text goes here.</p></Skills>
<Synonyms />
</THE-VALUE>
</ATTRIBUTE-VALUE>
如果将代码更改为“在节点上匹配”而不是“在元素上匹配”,则会包含文本节点:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xhtml exsl"
version="1.0">
<xsl:import href="http://lenzconsulting.com/xml-to-string/xml-to-string.xsl"/>
<xsl:output method="xml" indent="yes"
cdata-section-elements="MainDescription KeyConsideration"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="h1-group" match="xhtml:div/node()[not(self::xhtml:h1)]" use="generate-id(preceding-sibling::xhtml:h1[1])"/>
<xsl:template match="xhtml:div[xhtml:h1]">
<xsl:apply-templates select="xhtml:h1"/>
</xsl:template>
<xsl:template match="xhtml:h1[. = 'Main Description' or . = 'Key Consideration']">
<xsl:element name="{translate(., ' ', '')}">
<xsl:variable name="rtf-with-xhtml-ns-stripped">
<xsl:apply-templates select="key('h1-group', generate-id())"/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($rtf-with-xhtml-ns-stripped)/node()" mode="xml-to-string"/>
</xsl:element>
</xsl:template>
<xsl:template match="xhtml:h1">
<xsl:element name="{translate(., ' ', '')}">
<xsl:variable name="rtf-with-xhtml-ns-stripped">
<xsl:apply-templates select="key('h1-group', generate-id())"/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($rtf-with-xhtml-ns-stripped)/node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
<xsl:template match="xhtml:p">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
</xsl:stylesheet>
我还没有测试这是否会破坏任何东西。你能在问题中包括你的代码和你得到的当前结果,然后解释结果与预期不符的方式吗?您链接到的示例中的示例输入是否有意与问题中显示的示例输入不同?我在中尝试了代码,并将其应用于本文中提到的输入。如果文本包含在html标记中,则此代码工作正常。但是如果文本不在html标记中,我什么也得不到。好吧,您链接到的代码将元素节点与*匹配,如果您还有其他节点也要用标题包装,请参见。但是您的输入/输出转换似乎有几个要求,不清楚您何时需要CDATA,也不清楚主描述文本所需输出中的p元素在哪里。来自哪里,您似乎有各种要求,最好在单独的问题中单独提出`谢谢你,马丁。对于非cdata部分,该代码按预期工作。cdata部分也需要相同的输出。例如,pLine3的主要描述文本显示在此处。/p Cdata部分中的主要描述文本应转换为Line3的主要描述文本显示在此处。谢谢Martin,这正是我想要的。还有一个查询,解决方案可以使用,但不能在带有xalan_2.7.1的eclipse java transformer中使用。如果我用node替换*的话,我不会在标签中得到任何东西,比如,或。你知道吗?@VishalSharnagat,不知道,我不使用Xalan,因为在Java世界中,Saxon很容易给你XSLT 2或3。如果您在这个特定问题上需要帮助,我认为您最好的选择是提出一个新的、单独的问题,其中包含XML、XSLT、Java代码的最少细节,以便其他人能够轻松地重现这个问题。希望有人能告诉我如何解决这个问题。
<xsl:template match="text()" mode="xml-to-string">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>