2017-07-30 107 views
0

爲了將XML轉換爲XSLT,提供名稱空間前綴已在SO中進行了廣泛的介紹。見例如XSLT Transform XML with Namespaces,XSLT with XML source that has a default namespace set to xmlnsXSLT Transformating XML to XML。然而,經過無數小時的研究和試錯法,我可以說我沒有成功。在XML中訪問名稱空間中的元素

這是我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<library xmlns="http://void.net/library/1.0"> 
    <catalog> 
     <cd id="c1"> 
      <singer id="s1"> 
       <name>Kate</name> 
       <surname>Apple</surname> 
      </singer> 
     <title>Great CD</title> 
     </cd> 
     <cd id="c2"> 
      <singer id="s2"> 
       <name>Mary</name> 
       <surname>Orange</surname> 
      </singer> 
     <title>Even better CD</title> 
     </cd> 
    </catalog> 
</library> 

這是我想出迄今:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    xmlns:b="http://void.net/library/1.0" 
    exclude-result-prefixes="b"> 
    <xsl:output method="text" indent="no" /> 

    <xsl:template match="/b:library"> 
     <xsl:text>singer,title 
     </xsl:text> 
     <xsl:for-each select="b:catalog/b:cd"> 
      <xsl:value-of select="concat(b:singer/b:name, ' ', b:singer/b:surname, ', ', b:title, ' 
')" /> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

這裏的example and error list

+0

什麼是行不通的? – zx485

+0

@ zx485請查看我更新後的錯誤列表。 – menteith

+1

樣式表中存在語法錯誤。 「變形」後立即移除直角支架。 – mzjn

回答

0

我固定的輸入錯誤的樣式表,並想出了與此:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:b="http://void.net/library/1.0" exclude-result-prefixes="b"> 
    <xsl:output method="text" indent="no" /> 

    <xsl:template match="/b:library"> 
     <xsl:text>singer,title&#xa;</xsl:text> 
     <xsl:for-each select="b:catalog/b:cd"> 
      <xsl:value-of select="concat(b:singer/b:name, ' ', b:singer/b:surname, ', ', b:title, '&#xa;')" /> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

singer,title 
Kate Apple, Great CD 
Mary Orange, Even better CD 

所以一切似乎按預期方式工作。

+0

很多,非常感謝! – menteith

相關問題