2016-02-27 100 views
0

我似乎有重新格式化几个XML文件转变成一种对其他工具我的工作更多个可摄入分组的一个问题...使用XSLT 1.0重整旗鼓节点

另外,我也只能使用XSLT 1.0为这个问题。

这里是我的XML输入文件如何在文件夹中组织了一个样本式的“C:/ samplefolder的”:

<data> 
    <PersonID>12345</PersonID> 
    <PersonName>Gary Johnson</PersonName> 
    <PersonDetails> 
     <CurrAcct status="Y"> 
     <LastUpdated>26 FEB 2016</LastUpdated> 
     <Comments>THIS IS AN INTERESTING COMMENT</Comments> 
     </CurrAcct> 
     <Threshold status="Y"> 
     <LastUpdated>01 FEB 2016</LastUpdated> 
     <Comments>Blah Blah Blah</Comments> 
     </Threshold> 
    </PersonDetails> 
</data> 

这里是我怎么想它看起来:

<data> 
    <PersonID>12345</PersonID> 
    <PersonName>Gary Johnson</PersonName> 
    <PersonDetails> 
     <CurAcct>Y</CurrAcct> 
     <CurrAcctLastUpdated>26 FEB 2016</CurrAcctLastUpdated> 
     <CurrAcctComments>THIS IS AN INTERESTING COMMENT</CurrAcctComments> 
     <Threshold>Y</Threshold> 
     <ThresholdLastUpdated>01 FEB 2016</ThresholdLastUpdated> 
     <ThresholdComments>Blah Blah Blah</ThresholdComments> 
    </PersonDetails> 
</data> 

所以基本上我想要做的是三件事(按优先顺序):

  1. 我想是能够重新分组所有节点直接落入“PersonDetails”

  2. 对以前的子节点进行唯一地重命名,以便在将xml中的数据导出到表中时,它们不会全部塞入到一个字段中。

  3. 我希望能够以某种方式批处理所有的人一次......我试图先解决这个问题,但似乎无法找到任何东西,可以让我这样做有效(即:使用通配符拾取XLST 1.0中的所有xmls,因为collection()函数不可用。

附加说明:

- 在现实中,XML文件是多了很多细节要大很多,最终我将只是nugging出针对每个不同分组的具体格式化标准。除非你们都认为这是一个完全愚蠢的想法,在这种情况下 - 我总是愿意学习新的东西。

好吧,我想就是这样。我真的碰上了砖墙与这一个,所以谢谢你提前时间大家的......

+0

[编辑]您的问题,并添加您当前的xslt。 – usr2564301

+0

我更新并修复了下面的答案。 – zx485

回答

2

一种可能实现这一目标是以下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="/data"> 
    <data> 
     <xsl:copy-of select="*[local-name() != 'PersonDetails']" /> 
     <PersonDetails> 
      <xsl:apply-templates select="PersonDetails" /> 
     </PersonDetails> 
    </data> 
    </xsl:template> 

    <xsl:template match="CurrAcct | Threshold"> 
    <xsl:variable name="cur" select="local-name()" /> 
    <xsl:element name="{$cur}"> 
     <xsl:value-of select="@status" /> 
    </xsl:element> 
    <xsl:for-each select="*"> 
     <xsl:element name="{concat($cur,local-name())}"> 
      <xsl:value-of select="text()" /> 
     </xsl:element> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

结果是

<?xml version="1.0" encoding="UTF-8"?> 
<data> 
    <PersonID>12345</PersonID> 
    <PersonName>Gary Johnson</PersonName> 
    <PersonDetails> 
     <CurrAcct>Y</CurrAcct> 
     <CurrAcctLastUpdated>26 FEB 2016</CurrAcctLastUpdated> 
     <CurrAcctComments>THIS IS AN INTERESTING COMMENT</CurrAcctComments> 
     <Threshold>Y</Threshold> 
     <ThresholdLastUpdated>01 FEB 2016</ThresholdLastUpdated> 
     <ThresholdComments>Blah Blah Blah</ThresholdComments> 
    </PersonDetails> 
</data>