问题
given the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<report>
<![CDATA[<?xml version="1.0" encoding="UTF-8"?><whatever><title>GREETING</title><greeting>Hi</greeting><name>Dave</name></whatever>]]>
</report>
</root>
How can I use XSL-T to take into account this "embedded" XML?
An example output I would want to get after XSL-Transformations is like this:
<?xml version="1.0" encoding="UTF-8"?>
<TransformedRoot>
<data><html><head><title>GREETING</title></head><body><p>Hi, Dave!</p></body></html>
</TransformedRoot>
Assuming this is the standard XSL-T I am using:
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="/root">
<TransformedRoot>
<data><!-- How do I get the elements here? --></data>
</TransformedRoot>
</xsl:template>
回答1:
With commercial editions of Saxon 9:
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="/root">
<TransformedRoot>
<data>
<xsl:apply-templates/>
</data>
</TransformedRoot>
</xsl:template>
<xsl:template match="report">
<xsl:apply-templates select="saxon:parse(normalize-space(.))/node()"/>
</xsl:template>
<xsl:template match="whatever">
<html>
<head>
<xsl:copy-of select="title"/>
</head>
<body>
<p>
<xsl:apply-templates/>
</p>
</body>
</html>
</xsl:template>
<xsl:template match="greeting">
<xsl:value-of select="concat(., ', ')"/>
</xsl:template>
<xsl:template match="name">
<xsl:value-of select="concat(., '!')"/>
</xsl:template>
来源:https://stackoverflow.com/questions/19421847/how-to-apply-xslt-to-embedded-xml-in-another-xml