Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

How to apply XSLT to embedded XML in another XML

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...