2017-10-10 79 views
0

原始XML:如何在元素名称下显示特定元素的所有值?

<section sectiontype="WITNESSES"> 
        <bodytext> 
         <p> 
          <text> 
           <person:person> 
            <person:name.text>NEIL CAVUTO, FBN ANCHOR</person:name.text> 
           </person:person> 
          </text> 
         </p> 
         <p> 
          <text> 
           <person:person> 
            <person:name.text>REP. BARNEY FRANK, D-MASS.</person:name.text> 
           </person:person> 
          </text> 
         </p> 
        </bodytext> 
       </section> 

XSL模板,我有:

<xsl:template match="base:section[@sectiontype='WITNESSES']/base:bodytext/base:p"> 
    <xsl:element name="nl"/> 
    <xsl:element name="{name()}">   
     <xsl:copy-of select="@*"/> 
     <xsl:attribute name="display">block</xsl:attribute>    
     <xsl:element name="hdr"> 
      <xsl:attribute name="typestyle">BF</xsl:attribute> 
      <xsl:attribute name="inline">Y</xsl:attribute> 
      <xsl:text>WITNESSES:</xsl:text> 
      <xsl:apply-templates/> 
     </xsl:element>      
    </xsl:element> 
</xsl:template> 

电流输出我得到:

目击者:尼尔·卡维托,FBN锚
目击者:REP。 BARNEY FRANK,D-MASS。

所需的输出:

证人:

尼尔·卡维托,FBN ANCHOR

REP。 BARNEY FRANK,D-MASS。

回答

0

您提交了一个模板,它与某些< base:p>元素相匹配。它为每个与其匹配的元素分别实例化,每个实例化在结果树中创建一个值为“WITNESSES”的文本节点,然后对元素的子元素进行转换。如果你想在一个标题下将证人分组,那么你需要通过这些base:p元素的共同祖先元素的变换来输出标题。

例如,

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:base="http://my.com/base" 
    xmlns:person="http://my.com/person"> 
    <xsl:output method="text"/> 

    <xsl:template match="/"> 
    <xsl:apply-templates select=".//base:section"/> 
    </xsl:template> 

    <xsl:template match="base:section[@sectiontype='WITNESSES']"> 
    <xsl:text>WITNESSES:</xsl:text> 
    <xsl:apply-templates select=".//person:name.text"/> 
    </xsl:template> 

    <xsl:template match="person:name.text"> 
    <xsl:text>&#x10;</xsl:text> 
    <xsl:value-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

它的工作太棒了!谢谢。 但我希望的输出是: WITNESSES: NEIL CAVUTO,FBN ANCHOR REP。 BARNEY FRANK,D-MASS。 而我得到的是: WITNESSES:NEIL CAVUTO,FBN ANCHOR REP。 BARNEY FRANK,D-MASS。 –

+0

@JohhnDev,你想要的和观察到的输出之间没有任何区别。无论如何,XSLT之外的因素会影响您的结果在各种媒体中的呈现方式。既然你没有公开演示媒体或过程,我只能解释原则。你必须弄清楚你需要从你的转换中得到什么*** XML ***输出,并应用我提供的信息来生成这些信息。 –

相关问题