2012-04-28 107 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變量的值。