2016-09-20 78 views
1

這裏是我的用例的淡化版本。我對轉型exsl:節點集不檢索屬性的值

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"> 

<xsl:output method="text"/> 
<xsl:template match="Message"> 
    <xsl:for-each select="ent"> 
     <xsl:variable name="current_key" select="@key"/> 
     <xsl:variable name="current_type" select="@type"/> 
     <xsl:variable name="Match" select="exsl:node-set(msg)/ent"/> 
     <xsl:copy> 
      <xsl:copy-of select="exsl:node-set($Match)/@type"/> 
      <xsl:copy-of select="exsl:node-set($Match)/@key|exsl:node-set($Match)/translation/text()"/> 
      <!--- <xsl:copy-of select="exsl:node-set($Match)/@key|exsl:node-set($Match)/translation/text()|exsl:node-set($Match)/@type"/> Trial statement --> 
     </xsl:copy> 
    </xsl:for-each> 
    <xsl:call-template name = "Me" select="$Message"/> 
</xsl:template> 
</xsl:stylesheet> 

XSL文件和一個輸入文件如下

<?xml version="1.0" encoding="utf-8"?> 
<msg> 
    <ent key="key1" type="error"> 
     <text>Error: Could not find </text> 
     <translation>Another Error similar to previous one.</translation> 
    </ent> 
    <ent key="key2" type="damage"> 
     <text>Error2: Could not find2 </text> 
     <translation>Another Error2 similar to previous one.</translation> 
    </ent> 
</msg> 

我在Perl使用作爲的libxslt我的轉換引擎。我的轉換腳本已在此answer中提及。每當我執行腳本時,我都會得到如下輸出。

Error: Could not find 
Another Error similar to previous one. 

Error2: Could not find2 
Another Error2 similar to previous one. 

爲什麼type屬性沒有打印?如何在exsl:node-set或任何其他技術的幫助下檢索它?另外,我是否可以在審判聲明中包含屬性type,以便它將在輸出中顯示?

+0

這並不配合在一起,沒有'您的模板在您的輸入中匹配的消息'節點?!你爲什麼試圖在這裏使用'exsl:node-set()'可能無法確定沒有看到匹配的xsl和輸入xml –

+0

我不確定你在說什麼。腳本運行正常,模板被調用並顯示輸出。我不知道如何發佈一個最小的,可重複的和可行的例子,除了這個。 – Recker

+0

它可能是產生輸出的默認模板。當輸入中沒有名爲Message的元素時,您如何期望模板匹配'Message'? - 你最小的例子可能是最小的,但它不再是正確的,可能不會幫助你真正的問題 –

回答

2

以下樣式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:template match="/msg"> 
    <xsl:for-each select="ent"> 
     <xsl:text>KEY: </xsl:text> 
     <xsl:value-of select="@key"/> 
     <xsl:text>&#10;TYPE: </xsl:text> 
     <xsl:value-of select="@type"/> 
     <xsl:text>&#10;TEXT: </xsl:text> 
     <xsl:value-of select="text"/> 
     <xsl:text>&#10;TRANSLATION: </xsl:text> 
     <xsl:value-of select="translation"/> 
     <xsl:text>&#10;&#10;</xsl:text> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

當施加到你的輸入例如,會產生:

KEY: key1 
TYPE: error 
TEXT: Error: Could not find 
TRANSLATION: Another Error similar to previous one. 

KEY: key2 
TYPE: damage 
TEXT: Error2: Could not find2 
TRANSLATION: Another Error2 similar to previous one.