Xml 使用xslt复制嵌套节点的奇怪行为
我有一个XML文档,它包含一个包含xhtml的“body”元素。我正试图处理该html以删除一些非标准标记。源xml文档中未使用名称空间Xml 使用xslt复制嵌套节点的奇怪行为,xml,xslt,Xml,Xslt,我有一个XML文档,它包含一个包含xhtml的“body”元素。我正试图处理该html以删除一些非标准标记。源xml文档中未使用名称空间 <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="p"&g
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
XML如下所示:
<article>
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3 <fig></fig></p>
</body>
</article>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
第1款
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
第2款
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
第3款
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
XSLT如下所示:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
点击P]]>
输出是这样的-我不明白为什么它只找到第一个p标记:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<div>HIT A P</div>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3 <fig></fig></p>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
点击P
第1款
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
第2款
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
第3款
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
你知道为什么p模板只在第一次而不是在所有3段中被触发吗
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
我还试图弄明白为什么添加此项不会导致“fig”元素被删除:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="fig" />
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
谢谢你抽出时间来帮我。
更新:非常感谢您的回复。我试图把问题简单化。我真正做的是两个XSLT过程——一个是将数据组织成标准格式,另一个是XSLT过程,它查看正文中的HTML并复制除某些非标准标记之外的所有内容
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
我认为我遇到的问题是,在第一个XSLT过程之后,主体中的HTML是HTML编码的,而第二个XSLT过程似乎无法转换HTML。这里有一个更好的例子来说明实际发生的情况:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
这是新的XML(它是早期xslt转换的结果,因此对文本进行了编码):
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
示例标题
图1/p
图2/p
p图3/p
P
第4段-包含非标准fig标签
无花果
graphic href=“testgraphic.jpg”/
/无花果
/p
以下是新的XSLT:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
点击P]]>
来自BODYTEXT的您好]]>
当我运行它时,我得到以下信息:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
<document>
<article>
<title>SAMPLE TITLE</title>
<div>HELLO FROM BODYTEXT</div><bodytext>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<p>
Paragraph 4 - contains non-standard fig tag
<fig>
<graphic href="testgraphic.jpg"/>
</fig>
</p>
</bodytext>
</article>
</document>
示例标题
divHELLO FROM BODYTEXT/div
图1/p
图2/p
p图3/p
P
第4段-包含非标准fig标签
无花果
graphic href=“testgraphic.jpg”/
/无花果
/p
在本例中,它无法处理每个段落并删除fig。但是,如果XML不是htmlencoded,它就可以工作。以下是工作XML:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
<document>
<article>
<title>SAMPLE TITLE</title>
<bodytext>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3 <fig></fig></p>
</bodytext>
</article>
</document>
示例标题
第1款
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
第2款
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
第3款
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
这是输出:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
<document>
<article>
<title>SAMPLE TITLE</title>
<div>HELLO FROM BODYTEXT</div><bodytext>
<div>HIT A P</div>Paragraph 1
<div>HIT A P</div>Paragraph 2
<div>HIT A P</div>Paragraph 3
</bodytext>
</article>
</document>
示例标题
divHELLO FROM BODYTEXT/div
diva P/diva第1段
diva P/diva第2段
diva P/diva第3段
你知道当传入的数据被htmlencoded时,我如何进行第二个过程吗?再次感谢。对提供的输入XML运行XSLT,我不会得到意外的输出。我得到这个输出
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>
<article>
<body>
<div>HIT A P</div>
Paragraph 1
<div>HIT A P</div>
Paragraph 2
<div>HIT A P</div>
Paragraph 3
</body>
</article>
diva P/div
第1款
diva P/div
第2款
diva P/div
第3款
这正是XSLT应该生成的内容。这里没有什么奇怪的。您显示的不是htmlencoded,而是转义XML。转义XML不是XML—请参阅:@michael.hor257k对您的问题进行了评论,您不能期望
与转义XML匹配,因为转义XML的本质是,告诉解析器不要认为它是XML标记——这就是逃脱XML的意思。那么PHP中唯一的方法是引入一个单独的进程来创建一个新的DOMDOWN,并将未逃逸的内容加载到它中吗?我刚刚发现禁用输出逃逸=“是”——这似乎通过保持它不被逃脱来解决问题。谢谢大家的帮助。使用copy of而不是value of可能是朝着正确的方向迈出的一步。我想在您的TODO列表中添加对您使用CDATA的目的的回顾。
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<![CDATA[<div>HIT A P</div>]]>
<xsl:apply-templates mode="copy" select="@*|node()"/>
</xsl:template>
<xsl:template match="bodytext">
<![CDATA[<div>HELLO FROM BODYTEXT</div>]]>
<xsl:element name="bodytext">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- THIS APPEARS TO NEVER GET HIT -->
<xsl:template match="fig" />
</xsl:stylesheet>