为什么XSLT转换不是预期的输出

时间:2017-09-15 13:23:10

标签: xslt xslt-1.0

环境: XSLT 1.0

预期输出:仅限第2级元素的文本节点

实际输出:level1和level2文本输出

xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="text"/>

  <xsl:template match="/*">
    <xsl:apply-templates select="/data/section1" />
  </xsl:template>

  <xsl:template match="level2">
    <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>

xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="SelectingAndMatching.xslt"?>

<data>
  <section1>
    <level1>hello world 1</level1>
    <level2>unlocked achievement level 2</level2>
  </section1>
  <section2>
    <product1></product1>
    <product2></product2>
    <product3></product3>
  </section2>
</data>

1 个答案:

答案 0 :(得分:2)

处理从没有模板的文档节点<div class="card"> <div class="card-header"> <ul class="nav nav-tabs card-header-tabs" id="outerTab" role="tablist"> <li class="nav-item"> <a class="nav-link active" data-toggle="tab" href="#tabc" aria-controls="tabc" role="tab" aria-expanded="true">Tab-1</a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="tab" href="#tabb" aria-controls="tabb" role="tab">Tab-2</a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="tab" href="#taba" aria-controls="taba" role="tab">Tab-3</a> </li> </ul> </div> <div class="card-body tab-content"> <div class="tab-pane active" id="tabc" role="tabpanel"> Content for Tab-1 and: <div class="card"> <div class="card-header"> <ul class="nav nav-tabs card-header-tabs" id="innerTab" role="tablist"> <li class="nav-item"> <a class="nav-link active" data-toggle="tab" href="#tab1" aria-controls="tab1" role="tab" aria-expanded="true">Nested Tab-1</a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="tab" href="#tab2" aria-controls="tab2" role="tab">Nested Tab-2</a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="tab" href="#tab3" aria-controls="tab3" role="tab">Nested Tab-2</a> </li> </ul> </div> <div class="card-body tab-content"> <div class="tab-pane active" id="tab1" role="tabpanel"> Content for Nested Tab-1 </div> <div class="tab-pane" id="tab2" role="tabpanel"> Content for Nested Tab-2 </div> <div class="tab-pane" id="tab3" role="tabpanel"> Content for Nested Tab-3 </div> </div> </div> </div> <div class="tab-pane" id="tabb" role="tabpanel"> Content for Tab-2 </div> <div class="tab-pane" id="taba" role="tabpanel"> Content for Tab-3 </div> </div> </div> 开始,因此built-in template之一使用了/,即处理所有子节点,在您的情况下是<xsl:apply-templates/>元素与您的模板data匹配,后者为您没有模板的match="/*"进行处理,因此使用了内置的/data/section1,即处理<xsl:apply-templates/>的所有子节点section1包含level1元素,其中包含模板,因此使用内置处理所有子节点,对于那些文本节点,您没有模板,文本节点的内置复制它们到输出。