2013-07-18 147 views
1

嵌套属性我有布局这样的XML文件:转换XML使用XSLT

<root> 
    <node> 
     <descriptors> 
      <attribute attr="important"/> 
     </descriptors> 
     <value> 
      Some 
     </value> 
     <value> 
      text 
     </value> 
     <value> 
      here. 
     </value> 
    <node> 
</root> 

有许多节点和每一个是通过在descriptors元素中的元素区分。我需要提取文本,以便我有这样的:

<element> 
    Sometexthere. 
</element> 

我有一个XSL转换:

<xsl:template match="//attribute[@attr='important']"> 
    <element> 
     <xsl:for-each select="../../value"> 
      <xsl:value-of select="." /> 
     </xsl:for-each> 
    </element> 
</xsl:template> 

这不是仅仅写在所有文件中的每一个元素nodevalue元素。 我认为我并不真正了解作用域的工作方式(例如.指向当前元素/节点,但循环中当前元素/节点是什么)。

我该如何解决这个问题?

编辑

我实际的XSL样式表(我转化的docx文件):

<xsl:template match="/"> 
     <order> 
      <xsl:apply-templates/> 
     </order> 
    </xsl:template> 
    <xsl:template match="/w:document/w:body/w:p[w:pPr/w:pStyle/@w:val='Pealkiri1']"> 
     <name> 
      <xsl:for-each select="w:t"> 
       <xsl:value-of select="." /> 
      </xsl:for-each> 
     </name> 
    </xsl:template> 

回答

2

你可以尝试含有descriptors/attribute/@attr=...而不是attribute元素本身的node元件匹配。

for-each上下文将相对于该node

<xsl:template match="/"> 
    <order> 
     <xsl:apply-templates select="//node[descriptors/attribute/@attr='important']" /> 
    </order> 
</xsl:template> 

<xsl:template match="node"> 
    <element> 
     <xsl:for-each select="value"> 
      <xsl:value-of select="." /> 
     </xsl:for-each> 
    </element> 
</xsl:template> 
+0

这产生了相同的结果,但省略了''标记。所以现在它只是文字。很奇怪。 – j0ntech

+0

你可以发布你的样式表的其余部分吗?我想你错过了'或者搞乱了一个''。 –

+0

我有匹配'/'的模板,但是它只是用我自己的根元素('order')包围文本。 – j0ntech