2014-05-13 56 views
0

從XML列表中只挑選1個結果,然後使用我的XSL文檔進行轉換,遇到了麻煩。從XML列表中僅挑選1個結果

原始XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <?xml-stylesheet type="text/xsl" href="transform.xsl"?> 
    <data> 
<index id="TimeStamp"></index> 
<index id="GUID"></index> 
<index id="DOC-GUID"></index> 
<index id="RapportNr">074531</index> 
<index id="SessionId"></index> 
<index id="ClientType"></index> 
<index id="WSClientIp">::1</index> 
<index id="ClientIp"></index> 
<index id="ClientVersion">4.0</index> 
<index id="ClientId"></index> 
<index id="DeviceIP"></index> 
<index id="DeviceID"></index> 
<index id="DeviceName"/> 
<index id="DeviceModel"></index> 
<index id="DeviceLocation"/> 
<index id="IsDuplexScan">False</index> 
<index id="Printer"></index> 
<index id="PrintCount">1</index> 
<index id="PrintEnabled">False</index> 
<index id="Image Name">074531.tif</index> 
    </data> 

變換XSL

<?xml version="1.0" encoding="UTF-8"?> 
<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"/> 
<!-- Main output node --> 
<xsl:template match="/DrivveImage/Documents/Document"> 
    <!-- Start of the output --> 
    <data> 
     <!-- Call field template for each 'Field' node --> 
     <xsl:for-each select="/data/index"> 
      <xsl:call-template name="Field"/> 
     </xsl:for-each> 
     <!-- Output for 'Document' node attribute 'File' --> 
     <index id="RapportNr"> 
       <value> 
       ????????? 
       </value> 
     </index> 
    </data> 
    <!-- End of the output --> 
</xsl:template> 
<!-- Field template --> 
<xsl:template name="Field"> 
    <!-- This is the actual output - what ever you need per field --> 
    <index> 
     <xsl:attribute name="id"><xsl:value-of select="@name"/>     </xsl:attribute> 
     <!-- Output of the field text value --> 
     <xsl:value-of select="."/> 
    </index> 
</xsl:template> 
</xsl:stylesheet> 

結果

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><data> 
<index id="RapportNr"> 
    <value> 
        074531 should be here.... 
    </value> 
</index> 
</data> 

利用價值的選擇和複製的,但似乎沒有爲我工作我用盡。 所以我現在問這裏,如果有人可以幫我用我的代碼。

謝謝!

// Jorki

回答

0

我覺得你讓它太複雜了。下面要足夠:

<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="index[@id = 'RapportNr']"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <value> 
      <xsl:value-of select="."/> 
     </value> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="node() | @*"> 
    <xsl:apply-templates/> 
</xsl:template> 
</xsl:stylesheet> 
0

我真的不知道有什麼在你的XSLT的/DrivveImage/Documents/Document指的是因爲我沒有看到你的文檔中的任何地方,但這應該做你正在嘗試做的:

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

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

    <xsl:template match="/Drivve"> 
     <xsl:apply-templates select="Documents/Document/data" /> 
    </xsl:template> 

    <xsl:template match="data"> 
    <xsl:copy> 
     <xsl:apply-templates select="index[@id = 'RapportNr']" /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="processing-instruction()" /> 
</xsl:stylesheet> 

當你的樣品輸入運行,這將產生:

<?xml version="1.0" encoding="utf-8"?> 
<data> 
    <index id="RapportNr">074531</index> 
</data> 
+0

即時得到到許多信息,當我使用它,沒有像你得到的,這將是要命。我不能「awnser」發佈結果,因爲我是一個新成員。 – user3632448

+0

哦,現在即時獲得與您相同的結果,但不是當我使用名爲「Drivve」的程序對其進行轉換以掃描文檔的同時,應該能夠在生成.xml文件之前轉換文檔。 – user3632448

+0

好吧,我懷疑你的輸入文檔比你給我們展示的還多。你能試試我上面的更新答案嗎? – JLRishe