2016-08-17 59 views
0

我正在重構批處理XML文檔,並且此過程涉及將xml重新構建爲新的修訂後的DTD架構。由於使用了新的DTD,最初使用的許多元素都被重新使用,重新嵌套在其他元素中,或者被完全刪除。 以下示例在針對DTD進行驗證時是無效的xml文檔。爲了加速重構XML的過程,我想可能是XQuery腳本或XSLT轉換可能會有所幫助。不過,我對這兩者都沒有任何經驗,而且對XML還是比較陌生。有人能夠向我解釋在重構這些文檔時,XQuery,XSLT或Xpath是最重要的哪種語言。重構使用XSLT或XQuery腳本重新嵌套元素

無效XML:

<PartsDoc foo=」」 baa=」」 bar=」」 revno=」」 docno=」」 > 
    <PartsDocInfo> 
     <repairlvl level=」shop」 /> 
     <title id=」123」> Foo Electrical Control Box </title> 
    </PartsDocInfo> 

    <Parts.Category> 

    <figure id=」123」 > 
     <title id=」123」> Control Box Panels </title> 

    <subfig id=」123」> 
      <graphic img=」foo.jpg」 /> 
     </subfig> 
    <!- - everything above is valid, the below portion is not - -> 



<parts.item> 
      <callout id=」123」 config=」123」 label=」1」 /> 
      <mrs service=」shop」 sc=」」 mc=」」 rec=」」 /> 
      <nsn niin=」00-123-4567」> 4444-00-123-5467</nsn> 
      <cageno>12345</cageno> 
      <partno>12345</partno> 
      <name/> 
      <desc id=」123」 > Bolt 1/2inch </desc> 
      <qty>4</qty> 
<parts.item> 
    </parts.category> 

所需的輸出:

<PartsDoc foo=」」 baa=」」 bar=」」 revno=」」 docno=」」 > 

     <PartsDocInfo> 
     <repairlvl level=」shop」 /> 
     <title id=」123」> Foo Electrical Control Box </title> 
    </PartsDocInfo> 
<Parts.Category> 
    <figure id=」123」 > 
     <title id=」123」> Control Box Panels </title> 
<subfig id=」123」> 
      <graphic img=」foo.jpg」 /> 
</subfig> 
    <parts.item> 
     <callout id=」123」 config=」123」 label=」1」 /> 
<qty>4</qty> 
<mrs service=」shop」 sc=」」 mc=」」 rec=」」 /> 
<nsn> 
     <fsc>4444</fsc> 
     <niin>00-12-5467 
</nsn> 
     <partno>12345</partno> 
     <cageno>12345</cageno> 
     <name/> 
     <desc id=」123」 > Bolt 1/2inch </desc> 
    <parts.item>  
</parts.category> 

*注意:<qty>移動 *注意:<partno>移動 *注意<nsn>不包括與排序的內容子元素

此外,某些實例包括作爲子項嵌套在<desc>中的<uoc>元素。

​​

<uoc>實際上應該是<callout>後,和之前

<qty> 

使用XSLT樣式表或XQuery腳本任何幫助將不勝感激,以及爲什麼選擇在其他一種語言簡短說明。我目前使用的氧氣17 XML編輯器

回答

2

當輸出的主要部分是一樣的輸入,XSLT通常符合該法案更好。總的原則是編寫一個包含通用規則的樣式表,以遞歸地複製元素,然後爲要執行不同操作的元素添加規則。

在XSLT 3.0的一般規則是:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="3.0"> 
    <xsl:mode on-no-match="shallow-copy"/> 

    ... other code goes here ... 
</xsl:transform> 

雖然在早期版本中,它是:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 

    <xsl:template match="*"> 
    <xsl:copy> 
    <xsl:copy-of select="@*"> 
    <xsl:apply-templates/> 
    </xsl:copy> 
    </xsl:template> 

    ... other code goes here ... 
</xsl:transform> 

你的模板規則重新排序parts.item可以寫成:

<xsl:template match="parts.item"> 
    <parts.item> 
    <xsl:copy-of select="callout"/> 
    <xsl:copy-of select="qty"/> 
    <xsl:copy-of select="mrs"/> 
    <nsn> 
     <fsc><xsl:value-of select="substring-before(nsn, '-')"/></fsc> 
     <niin><xsl:value-of select="nsn/@niin"/></niin> 
    </nsn> 
    <xsl:copy-of select="partno"/> 
    <xsl:copy-of select="cageno"/> 
    <xsl:copy-of select="name"/> 
    <xsl:copy-of select="desc"/> 
</parts.item> 

把這個在一起,下面的XSLT 2.0樣式表:適用於以下源文件

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 

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

    <xsl:template match="*"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="parts.item"> 
     <parts.item> 
      <xsl:copy-of select="callout"/> 
      <xsl:copy-of select="qty"/> 
      <xsl:copy-of select="mrs"/> 
      <nsn> 
       <fsc><xsl:value-of select="substring-before(nsn, '-')"/></fsc> 
       <niin><xsl:value-of select="nsn/@niin"/></niin> 
      </nsn> 
      <xsl:copy-of select="partno"/> 
      <xsl:copy-of select="cageno"/> 
      <xsl:copy-of select="name"/> 
      <xsl:copy-of select="desc"/> 
     </parts.item> 
    </xsl:template> 
</xsl:transform> 

<PartsDoc foo="" baa="" bar="" revno="" docno="" > 
    <PartsDocInfo> 
     <repairlvl level="shop" /> 
     <title id="123"> Foo Electrical Control Box </title> 
    </PartsDocInfo> 

    <Parts.Category> 

     <figure id="123" > 
     <title id="123"> Control Box Panels </title> 

     <subfig id="123"> 
        <graphic img="foo.jpg" /> 
     </subfig> 
       <!-- everything above is valid, the below portion is not --> 

       <parts.item> 
        <callout id="123" config="123" label="1" /> 
        <mrs service="shop" sc="" mc="" rec="" /> 
        <nsn niin="00-123-4567"> 4444-00-123-5467</nsn> 
        <cageno>12345</cageno> 
        <partno>12345</partno> 
        <name/> 
        <desc id="123" > Bolt 1/2inch </desc> 
        <qty>4</qty> 
       </parts.item> 
     </figure> 
    </Parts.Category> 
</PartsDoc> 

產生以下輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<PartsDoc foo="" baa="" bar="" revno="" docno=""> 
    <PartsDocInfo> 
     <repairlvl level="shop"/> 
     <title id="123"> Foo Electrical Control Box </title> 
    </PartsDocInfo> 
    <Parts.Category> 
     <figure id="123"> 
     <title id="123"> Control Box Panels </title> 
     <subfig id="123"> 
      <graphic img="foo.jpg"/> 
     </subfig> 
     <parts.item> 
      <callout id="123" config="123" label="1"/> 
      <qty>4</qty> 
      <mrs service="shop" sc="" mc="" rec=""/> 
      <nsn> 
       <fsc> 4444</fsc> 
       <niin>00-123-4567</niin> 
      </nsn> 
      <partno>12345</partno> 
      <cageno>12345</cageno> 
      <name/> 
      <desc id="123"> Bolt 1/2inch </desc> 
     </parts.item> 
     </figure> 
    </Parts.Category> 
</PartsDoc> 
+1

然後你做錯了什麼。在提供的XML和我的XSLT中有一些微不足道的拼寫錯誤,但是在修復這些錯誤之後,它按預期工作,並添加了完整的詳細信息。 –

+0

邁克爾如果我想保留parts.item元素中的任何屬性,我該怎麼做? 需要通過用@ *運算符進一步篩選來更改嗎? – Akpan

+0

請不要以評論形式提出補充問題。 SO問題/答案格式不是爲此設計的。如果您有補充問題,請將其作爲一個新問題提出來,並從最初原則進行解釋。 –