2014-09-22 192 views
0

InputXML:轉化與命名空間

<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <json:object name="Customers"> 
     <json:array name="Order"> 
      <json:object> 
       <json:string name="Name">john</json:string> 
       <json:string name="Password">Doe</json:string> 
      </json:object> 
      <json:object> 
       <json:string name="Name">Adam</json:string> 
       <json:string name="Password">eve</json:string> 
      </json:object> 
     </json:array> 
    </json:object> 
</json:object> 

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:tns="http://some-other-namespace" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" exclude-result-prefixes="json"> 
    <xsl:template match="/json:object"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="json:array[@name]"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="json:object[@name]"> 
     <xsl:element name="{@name}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:object"> 
     <xsl:element name="{../@name}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:string[@name]"> 
     <xsl:element name="{@name}"> 
      <xsl:value-of select="."/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

實際輸出:

<Customers> 
    <Order> 
     <Name>john</Name> 
     <Password>Doe</Password> 
    </Order> 
    <Order> 
     <Name>Adam</Name> 
     <Password>eve</Password> 
    </Order> 
</Customers> 

所需的輸出:

<Customers xmlns:tns="http://some-other-namespace"> 
    <Order> 
     <Name>john</Name> 
     <Password>Doe</Password> 
    </Order> 
    <Order> 
     <Name>Adam</Name> 
     <Password>eve</Password> 
    </Order> 
</Customers> 

我知道我們可以做到通過將實際輸出作爲其輸入的身份在另一個XSL轉換,並獲得我所需的輸出。

但我想在一個單一的樣式表中做所有事情。如何實現使用單個xsl

+0

您的預期結果沒有多大意義。它和實際輸出之間的唯一區別是**未使用的**名稱空間聲明。對接收應用程序完全不應有任何改變。 – 2014-09-22 21:20:56

+0

我同意。但是我想用它來進行模式驗證 – mnvbrtn 2014-09-22 23:09:23

回答

0

如果您確實想生成任何元素或屬性名稱中未實際使用的名稱空間聲明,並且該名稱空間聲明未出現在源文檔中,則可以使用xsl:namespace指令(一個相當專業的指令只存在於這個模糊的目的中)。

+0

當我提出這樣的建議時,我假設你會選擇你最喜歡的XSLT教科書並查找它 - 我不打算給出教程。您可以像使用xsl:attribute一樣使用它。 – 2014-09-23 07:16:35