我需要进行子节点转换。
由于某些原因,<extra>
标签在XSLT应用程序之后丢失了它的子节点。
原始XML文件
<?xml version="1.0" encoding="utf-8"?>
<item id="1.0.14797349">
<metadata>
<general>
<somemeta>some data</somemeta>
</general>
</metadata>
<content>
<grouphead>
<headline><p>Alabama vs. Clemson: Keys to winning national championship game</p></headline>
</grouphead>
<text>
<p>Lorem ipsum dolor sit amet</p>
<extra><title>Lorem ipsum</title><p>Lorem ipsum dolor sit amet</p></extra>
<p>Excepteur sint occaecat cupidatat non proident</p>
<crosshead>Some title</crosshead>
<p>At vero eos et accusamus et iusto odio dignissimos ducimus</p>
</text>
</content>
</item>
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/item">
<ARTICLE type="article" visibility="hidden">
<SOMEMETA><xsl:value-of select="//metadata/general/somemeta" /></SOMEMETA>
<TITRE><xsl:value-of select="//content/grouphead/headline" /></TITRE>
<TEXTE>
<xsl:apply-templates />
</TEXTE>
</ARTICLE>
</xsl:template>
<xsl:template match="crosshead" priority="1">
<h4 class="title">
<xsl:value-of select="."/>
</h4>
</xsl:template>
<xsl:template match="content/text/node()">
<xsl:copy>
<xsl:value-of select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
预期的XML结果
<?xml version="1.0" encoding="UTF-8"?>
<ARTICLE type="article" visibility="hidden">
<SOMEMETA>some data</SOMEMETA>
<TITRE>Alabama vs. Clemson: Keys to winning national championship game</TITRE>
<TEXTE>
<p>Lorem ipsum dolor sit amet</p>
<extra><title>Lorem ipsum</title><p>Lorem ipsum dolor sit amet</p></extra>
<p>Excepteur sint occaecat cupidatat non proident</p>
<h4 class="title">Some title</h4>
<p>At vero eos et accusamus et iusto odio dignissimos ducimus</p>
</TEXTE>
</ARTICLE>
谢谢
答案 0 :(得分:1)
怎么样:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/item">
<ARTICLE type="article" visibility="hidden">
<SOMEMETA>
<xsl:value-of select="metadata/general/somemeta" />
</SOMEMETA>
<TITRE>
<xsl:value-of select="content/grouphead/headline" />
</TITRE>
<TEXTE>
<xsl:apply-templates select="content/text/*"/>
</TEXTE>
</ARTICLE>
</xsl:template>
<xsl:template match="crosshead">
<h4 class="title">
<xsl:value-of select="."/>
</h4>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>