2012-04-28 105 views
2

我用XSL解析XSL文件。我有一个动态查找节点的问题。下面是这种情况:XSL:动态选择节点

<linkbase xmlns="http://www.xbrl.org/2003/linkbase" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xbrl.org/2003/linkbase http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd"> 
    <labelLink xmlns:xlink="http://www.w3.org/1999/xlink" xlink:role="http://www.xbrl.org/2003/role/link" xlink:type="extended"> 
     <loc xlink:type="locator" xlink:href="de-gaap-ci-2010-12-16.xsd#de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other" xlink:label="de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other"/> 

     <!-- many <loc... elements --> 

     <labelArc xlink:from="de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other" xlink:to="label_de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other" priority="1" xlink:arcrole="http://www.xbrl.org/2003/arcrole/concept-label" xlink:type="arc"/> 

     <!-- many <labelArc... elements --> 

    </labelLink> 
</linkbase> 

我解析的labelArc元素,并希望包括来自loc元素的信息。这与SAP/ABAP做...

我的XSL代码如下:

<xsl:template match="lb:labelArc"> 
    <xsl:variable name="arc_to"  select="@xlink:to"/> 

    <TY_T_LABELARC> 
    <LOC> <xsl:value-of select="//lb:loc[@xlink:label='$arc_to']/@xlink:href"/> </LOC> 
    <FROM> <xsl:value-of select="@xlink:from"/> </FROM> 
    <TO> <xsl:value-of select="@xlink:to"/> </TO> 
    <!-- Other values follow --> 
    </TY_T_LABELARC> 

</xsl:template> 

我想包括从loc标签到输出的@href。我可以找到相应的loc标签,其值为@to,每个标签的值为labelArc

我的问题是,这个语句返回空值:

<xsl:value-of select="//lb:loc[@label='$arc_to']/@href"/> 

我与领先的命名空间试过都“的XLink:”每个属性,没有它...

任何想法?

回答

3

有两个问题与您的代码:

首先

<xsl:variable name="arc_to"  
     select="@xlink:to"/> 

请注意的labelArc开始的xlink:to与字符串"label_"的价值 - 和xlink:label属性loc不以此字符串开头。

其次

<xsl:value-of select="//lb:loc[@xlink:label='$arc_to']/@xlink:href"/> 

这个比较@xlink:label"$arc_to" - 不变量$arc_to

校正代码

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:lb="http://www.xbrl.org/2003/linkbase" 
xmlns:xlink="http://www.w3.org/1999/xlink" 
    exclude-result-prefixes="lb xlink"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="lb:labelArc"> 
    <xsl:variable name="arc_to" 
     select="substring-after(@xlink:to, 'label_')"/> 

    <TY_T_LABELARC> 
    <LOC> <xsl:value-of select="//lb:loc[@xlink:label= $arc_to]/@xlink:href"/> </LOC> 
    <FROM> <xsl:value-of select="@xlink:from"/> </FROM> 
    <TO> <xsl:value-of select="@xlink:to"/> </TO> 
    <!-- Other values follow --> 
    </TY_T_LABELARC> 
</xsl:template> 
</xsl:stylesheet> 

当该变换被应用到所提供的XML文档:

<linkbase 
    xmlns="http://www.xbrl.org/2003/linkbase" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.xbrl.org/2003/linkbase http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd"> 
    <labelLink xmlns:xlink="http://www.w3.org/1999/xlink" 
      xlink:role="http://www.xbrl.org/2003/role/link" 
      xlink:type="extended"> 
    <loc xlink:type="locator" 
     xlink:href="de-gaap-ci-2010-12-16.xsd#de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other" 
     xlink:label="de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other"/> 

      <!-- many <loc... elements --> 

    <labelArc priority="1" xlink:type="arc" 
    xlink:from="de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other" 
    xlink:to="label_de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other" 
    xlink:arcrole="http://www.xbrl.org/2003/arcrole/concept-label" /> 

      <!-- many <labelArc... elements --> 

</labelLink> 
</linkbase> 

有用,正确的结果产生:

<TY_T_LABELARC> 
    <LOC>de-gaap-ci-2010-12-16.xsd#de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other</LOC> 
    <FROM>de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other</FROM> 
    <TO>label_de-gaap-ci_bs.ass.fixAss.fin.otherLoans.other</TO> 
</TY_T_LABELARC> 
+0

感谢您指出我错过“label_”的错误!你隐含地解决了我以前没有认识到的另一个我的问题:-) – 2012-04-28 22:21:49

+0

@MarcoNätlitz:不客气。 – 2012-04-28 22:25:44

1

尝试:

<xsl:value-of select="//lb:loc[@label=$arc_to]/@href"/> 

如果你写

<xsl:value-of select="//lb:loc[@label='$arc_to']/@href"/> 

然后你告诉XSL处理器相匹配的对字符串 '$ arc_to',而不是arc_to变量的值。