2014-09-27 69 views
0

我是新來的,所以請原諒我,如果我是無知的。這是我的XML數據的一個樣本。xsl:for-each - 如何格式化列表?

<record ID="ART5843"> 
    <title>This is a sample record</title> 
    <edition></edition> 
    <persauthorfull>Lykins, Amy D.</persauthorfull> 
    <persauthorfull>Cantor, James M.</persauthorfull> 
    <persauthorfull>Blanchard, Ray</persauthorfull> 
    <keywords>Generic</keywords> 
    <keywords>Criminal offences</keywords> 
    <keywords>Civil offences</keywords> 
    <keywords>Men</keywords> 
    <keywords>Psychology</keywords> 
    <keywords>Research</keywords> 
    <subjects></subjects> 
</record> 

我需要<persauthorfull>到重命名爲<PA>,然後輸出由分號,例如分離的單線...

<PA>Lykins, Amy D.; Cantor, James M.; Blanchard, Ray</PA> 

我使用XSL的for-each以提取名稱,但我不知道如何格式化它們。

<xsl:for-each select="a:persauthorfull"> 
    <PA> 
    <xsl:attribute name="type"> 
    <xsl:value-of select="."/> 
    </xsl:attribute> 
    </PA> 
</xsl:for-each> 

從我在猜測這是對作者的一個單一的迭代 - 有沒有另一種方式?

+0

請顯示您的完整輸入XML(包括名稱空間定義,例如)和完整的樣式表。你使用什麼版本的XSLT? – 2014-09-27 18:15:31

+0

是的,如果你打算放入XML和XSLT片段,我們將無法爲你提供很多幫助。 – WineSoaked 2014-09-27 18:34:34

回答

1

如果你想獲得:

<PA>Lykins, Amy D.; Cantor, James M.; Blanchard, Ray</PA> 

,那麼你應該做的事情(在XSLT 1.0):

<PA> 
    <xsl:for-each select="persauthorfull"> 
     <xsl:value-of select="."/> 
     <xsl:if test="position()!=last()"> 
      <xsl:text>; </xsl:text> 
     </xsl:if> 
    </xsl:for-each> 
</PA> 
0

繼XSLT生成輸出,並且可以進行調整,以滿足更多不爲人知要求:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="/*"> 
<PA> 
    <xsl:apply-templates select="persauthorfull"/> 
</PA> 
</xsl:template> 
<xsl:template match="persauthorfull"> 
    <xsl:value-of select="."/> 
    <xsl:if test="position()!=last()"><xsl:text>;</xsl:text></xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<PA>Lykins, Amy D.;Cantor, James M.;Blanchard, Ray</PA> 

「我可以調整」我的意思是 - 例如可能會省略XML名稱空間聲明,但也許你想保留它。正如我想的那樣,您可能希望稍後在輸入中添加更多信息,這可以很容易地擴展模板。
如果您是XSLT的新手,只需簡短說明這是如何工作的 - <xsl:template match="/*">與來自根的整個輸入XML匹配。然後<xsl:apply-templates select="persauthorfull"/>僅將模板應用於名爲persauthorfull的節點。由於有一個模板與這些節點相匹配 - <xsl:template match="persauthorfull">,節點在那裏處理。所有其他節點將被省略。並且<xsl:if test="position()!=last()"><xsl:text>;</xsl:text></xsl:if>需要注意在其position()不是last()時在每個節點值後面添加;

1

如果使用XSLT 2.0,string-join()是你在找什麼:

<PA> 
    <xsl:value-of select="string-join(a:persauthorfull,'; ')"/> 
</PA> 

編輯:正如邁克爾·凱認爲,還有一個更簡單的方法:

<xsl:value-of select="persauthorfull" separator="; "/> 

使用任意一行代碼,將所有persauthorfull元素的文本內容連接在一起,用「;」分隔。


讓我來澄清一下你當前的代碼的作用。你寫:

從我所surmising這是對作者

這是真正的單次迭代 - 如果「一次迭代」你的意思是每個a:persauthorfull元素一個迭代。如果有多個作者,這將在輸出中創建多個PA元素。此外,作者的名字中的一個屬性type結束:

<PA type="Blanchard, Ray"/> 

樣式

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:strip-space elements="*"/> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="record"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|persauthorfull[1]/preceding-sibling::*"/> 
      <PA> 
       <xsl:value-of select="string-join(persauthorfull,'; ')"/> 
      </PA> 
      <xsl:apply-templates select="persauthorfull[last()]/following-sibling::*"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:transform> 

XML輸入

<record ID="ART5843"> 
    <title>This is a sample record</title> 
    <edition></edition> 
    <persauthorfull>Lykins, Amy D.</persauthorfull> 
    <persauthorfull>Cantor, James M.</persauthorfull> 
    <persauthorfull>Blanchard, Ray</persauthorfull> 
    <keywords>Generic</keywords> 
    <keywords>Criminal offences</keywords> 
    <keywords>Civil offences</keywords> 
    <keywords>Men</keywords> 
    <keywords>Psychology</keywords> 
    <keywords>Research</keywords> 
    <subjects></subjects> 
</record> 

XML輸出

<record ID="ART5843"> 
    <title>This is a sample record</title> 
    <edition/> 
    <PA>Lykins, Amy D.; Cantor, James M.; Blanchard, Ray</PA> 
    <keywords>Generic</keywords> 
    <keywords>Criminal offences</keywords> 
    <keywords>Civil offences</keywords> 
    <keywords>Men</keywords> 
    <keywords>Psychology</keywords> 
    <keywords>Research</keywords> 
    <subjects/> 
</record> 
+0

XSLT 2.0解決方案實際上並不需要字符串連接,你可以簡單地寫'' – 2014-09-27 21:36:41

+0

@MichaelKay謝謝,我編輯了我的帖子這個。 – 2014-09-27 21:49:36