2011-06-06 75 views
3

我需要把下面的輸入文件如何選擇命名空間內的元素,並在輸出擺脫空的默認命名空間的

<?xml version="1.0"?> 
<soap:Envelope 
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
    <env:Header xmlns:env="http://www.w3.org/2003/05/soap-envelope"> 
     <wsa:Action>http://worksite.imanage.com/SearchDocumentsResponse</wsa:Action> 
     <wsa:MessageID>urn:uuid:b9b79a0f-b4de-4686-9c54-51c5a7f99331</wsa:MessageID> 
     <wsa:RelatesTo>urn:uuid:8e88f531-8369-4557-80bf-e8a9835d8eb3</wsa:RelatesTo> 
     <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To> 
     <wsse:Security> 
      <wsu:Timestamp wsu:Id="Timestamp-ea526ce6-ca66-4aaf-aae9-cf2fe8d5cbad"> 
       <wsu:Created>2011-06-01T07:09:20Z</wsu:Created> 
       <wsu:Expires>2011-06-01T07:14:20Z</wsu:Expires> 
      </wsu:Timestamp> 
     </wsse:Security> 
    </env:Header> 
    <soap:Body> 
     <SearchDocumentsResult xmlns="http://worksite.imanage.com"> 
      <Documents> 
       <Document> 
        <ObjectID>!nrtdms:0:!session:PTHT096:!database:Technical:!document:803226,1:</ObjectID> 
        <Database>Technical</Database> 
        <Profile> 
         <ProfileItem> 
          <AttributeID>imProfileDocNum</AttributeID> 
          <Value>803226</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileDescription</AttributeID> 
          <Value>TC-D60-1087 - Compressor C-301C Installation Work Platform General Arrangement</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileVersion</AttributeID> 
          <Value>1</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileCustom3</AttributeID> 
          <Value>CV</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileDatabase</AttributeID> 
          <Value>Technical</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileExtension</AttributeID> 
          <Value>ACROBAT</Value> 
         </ProfileItem> 
        </Profile> 
       </Document> 
      </Documents> 
     </SearchDocumentsResult> 
    </soap:Body> 
</soap:Envelope> 

變成

<?xml version="1.0"?> 
<soap:Envelope 
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
    <env:Header xmlns:env="http://www.w3.org/2003/05/soap-envelope"> 
     <wsa:Action>http://worksite.imanage.com/SearchDocumentsResponse</wsa:Action> 
     <wsa:MessageID>urn:uuid:b9b79a0f-b4de-4686-9c54-51c5a7f99331</wsa:MessageID> 
     <wsa:RelatesTo>urn:uuid:8e88f531-8369-4557-80bf-e8a9835d8eb3</wsa:RelatesTo> 
     <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To> 
     <wsse:Security> 
      <wsu:Timestamp wsu:Id="Timestamp-ea526ce6-ca66-4aaf-aae9-cf2fe8d5cbad"> 
       <wsu:Created>2011-06-01T07:09:20Z</wsu:Created> 
       <wsu:Expires>2011-06-01T07:14:20Z</wsu:Expires> 
      </wsu:Timestamp> 
     </wsse:Security> 
    </env:Header> 
    <soap:Body> 
     <SearchDocumentsResult xmlns="http://worksite.imanage.com"> 
      <Documents> 
       <Document> 
        <ObjectID>!nrtdms:0:!session:PTHT096:!database:Technical:!document:803226,1:</ObjectID> 
        <Database>Technical</Database> 
        <Profile> 
         <ProfileItem> 
          <AttributeID>imProfileDocNum</AttributeID> 
          <Value>803226</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileDescription</AttributeID> 
          <Value>TC-D60-1087 - Compressor C-301C Installation Work Platform General Arrangement</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileVersion</AttributeID> 
          <Value>1</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileCustom3</AttributeID> 
          <Value>CV</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileDatabase</AttributeID> 
          <Value>Technical</Value> 
         </ProfileItem> 
         <ProfileItem> 
          <AttributeID>imProfileExtension</AttributeID> 
          <Value>pdf</Value> 
         </ProfileItem> 
        </Profile> 
       </Document> 
      </Documents> 
     </SearchDocumentsResult> 
    </soap:Body> 
</soap:Envelope> 

我寫了下面的輸出文件下面的樣式表做

<?xml version="1.0" encoding="UTF-8"?> 

<xsl:stylesheet 
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" 
xmlns:xs = "http://www.w3.org/2001/XMLSchema" 
xmlns:soap = "http://www.w3.org/2003/05/soap-envelope" 
    xmlns:w = "http://worksite.imanage.com" 
exclude-result-prefixes="#all" 
version="2.0"> 

<xsl:output indent="yes"/> 

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

<xsl:template match="attribute()|text()|comment()|processing-instruction()"> 
    <xsl:copy/> 
</xsl:template> 

<xsl:template match = "//w:ProfileItem[6]/child::w:Value">   
    <xsl:call-template name="changeDocumentExtension"/> 
</xsl:template> 

<xsl:template name = "changeDocumentExtension"> 

    <xsl:variable 
    name="iManageExtension" 
    as = "xs:string" 
    select="//w:AttributeID[. = 'imProfileExtension']/following::w:Value" 
    /> 

    <xsl:choose> 
    <xsl:when test="$iManageExtension eq 'ACROBAT'"> 
     <Value>pdf</Value> 
    </xsl:when> 
    <xsl:when test="$iManageExtension eq 'ANSI'"> 
     <Value>txt</Value> 
    </xsl:when> 
    <xsl:when test="$iManageExtension eq 'EXCEL'"> 
     <Value>xsl</Value> 
    </xsl:when> 
    <xsl:when test="$iManageExtension eq 'EXCELX'"> 
     <Value>xslx</Value> 
    </xsl:when> 
    <xsl:when test="$iManageExtension eq 'PDF230'"> 
     <Value>pdf</Value> 
    </xsl:when> 
    <xsl:when test="$iManageExtension eq 'WORD'"> 
     <Value>doc</Value> 
    </xsl:when> 
    <xsl:when test="$iManageExtension eq 'WORDX'"> 
     <Value>docx</Value> 
    </xsl:when> 
    <xsl:when test="$iManageExtension eq 'WORDXT'"> 
     <Value>docx</Value> 
    </xsl:when> 
    </xsl:choose>    

</xsl:template>  

</xsl:stylesheet> 

這是我得到的輸出

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope 
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
    <env:Header xmlns:env="http://www.w3.org/2003/05/soap-envelope"> 
    <wsa:Action>http://worksite.imanage.com/SearchDocumentsResponse</wsa:Action> 
    <wsa:MessageID>urn:uuid:b9b79a0f-b4de-4686-9c54-51c5a7f99331</wsa:MessageID> 
    <wsa:RelatesTo>urn:uuid:8e88f531-8369-4557-80bf-e8a9835d8eb3</wsa:RelatesTo> 
    <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To> 
    <wsse:Security> 
     <wsu:Timestamp wsu:Id="Timestamp-ea526ce6-ca66-4aaf-aae9-cf2fe8d5cbad"> 
     <wsu:Created>2011-06-01T07:09:20Z</wsu:Created> 
     <wsu:Expires>2011-06-01T07:14:20Z</wsu:Expires> 
     </wsu:Timestamp> 
    </wsse:Security> 
    </env:Header> 
    <soap:Body> 
    <SearchDocumentsResult xmlns="http://worksite.imanage.com"> 
     <Documents> 
     <Document> 
      <ObjectID>!nrtdms:0:!session:PTHT096:!database:Technical:!document:803226,1:</ObjectID> 
      <Database>Technical</Database> 
      <Profile> 
      <ProfileItem> 
       <AttributeID>imProfileDocNum</AttributeID> 
       <Value>803226</Value> 
      </ProfileItem> 
      <ProfileItem> 
       <AttributeID>imProfileDescription</AttributeID> 
       <Value>TC-D60-1087 - Compressor C-301C Installation Work Platform General Arrangement</Value> 
      </ProfileItem> 
      <ProfileItem> 
       <AttributeID>imProfileVersion</AttributeID> 
       <Value>1</Value> 
      </ProfileItem> 
      <ProfileItem> 
       <AttributeID>imProfileCustom3</AttributeID> 
       <Value>CV</Value> 
      </ProfileItem> 
      <ProfileItem> 
       <AttributeID>imProfileDatabase</AttributeID> 
       <Value>Technical</Value> 
      </ProfileItem> 
      <ProfileItem> 
       <AttributeID>imProfileExtension</AttributeID> 
       <Value xmlns="">pdf</Value> 
      </ProfileItem> 
      </Profile> 
     </Document> 
     </Documents> 
    </SearchDocumentsResult> 
    </soap:Body> 
</soap:Envelope> 

我想獲得一些幫助,下面的問題:

  1. 有一個不需要的xmlns =「」元素中不能使用排除-result-被刪除輸出前綴屬性。我不知道如何擺脫它。

  2. 樣式表有兩個長XPath。他們完成這項工作並指出了正確的元素,但每次看到它們時,我都忍不住c咽。如果有人可以提供更短,更清潔的替代品,我會非常感激。

+0

更新的樣式表,以反映我實現EMPO的提示後所做的更改。 – 2011-06-07 07:07:53

回答

3

簡化的XPath

我建議你,包括您的轉換前綴爲默認命名空間的聲明。例如,對於

<SearchDocumentsResult xmlns="http://worksite.imanage.com"> 

你可以在樣式表元素添加w前綴:

<xsl:stylesheet 
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" 
xmlns:xs = "http://www.w3.org/2001/XMLSchema" 
xmlns:soap = "http://www.w3.org/2003/05/soap-envelope" 
xmlns:w = "http://worksite.imanage.com" 
exclude-result-prefixes="#all" 
version="2.0"> 

爲您選擇了命名空間的元素這將使更加容易,從而能夠避免對namespace-uri() XPath的比賽,並簡化你的XPath。

關於XPath,您的選擇必須是絕對的?例如,下面的工作一樣對我來說:

"/soap:Envelope/soap:Body[1]/w:SearchDocumentResult/*[1]/*[1]/*[1]/*[6]/*[1]" 

但你也可以使用:

"/soap:Envelope/soap:Body[1]/w:SearchDocumentResult//w:Value[1]" 

刪除空命名空間

爲了擺脫出現空命名空間在Value元素中,您需要修正樣式表,以便每個Value元素都正確地位於第e命名空間。例如:

<xsl:when test="$iManageExtension eq 'ACROBAT'"> 
    <xsl:element 
     name="Value" 
     namespace="http://worksite.imanage.com"> 
     pdf 
    </xsl:element> 

通過這種方式,系統通常會輸出沒有命名空間的元素。


關於簡化的XPath還注意到

注意在某些情況下,其中大部分是要管理一個特定的命名空間,你可以簡單地做:

<xsl:stylesheet 
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" 
xmlns:xs = "http://www.w3.org/2001/XMLSchema" 
xmlns:soap = "http://www.w3.org/2003/05/soap-envelope" 
exclude-result-prefixes="#all" 
xpath-default-namespace="http://worksite.imanage.com" 
version="2.0"> 

和沒有命名空間前綴。

+0

感謝您的建議。使用關於默認名稱空間的提示,我可以簡化兩個XPath。但我仍然不知道如何擺脫出現在輸出中的xmlns =「」。 – 2011-06-07 07:06:37

+0

@Sergey:給我幾分鐘,我正在調查它。 – 2011-06-07 07:19:55

+0

@Sergey:我已經擴展了答案來管理空輸出名稱空間。我希望你現在好了。請提供反饋。 – 2011-06-07 08:13:38

1

下面的樣式做這項工作:

<?xml version="1.0" encoding="UTF-8"?> 

    <xsl:stylesheet 
     xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" 
     xmlns:xs = "http://www.w3.org/2001/XMLSchema" 
     xmlns:w = "http://worksite.imanage.com"  
     exclude-result-prefixes="#all" 
     version="2.0"> 

    <xsl:output indent="yes"/> 

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

    <xsl:template match="attribute()|text()|comment()|processing-instruction()"> 
     <xsl:copy/> 
    </xsl:template> 

    <xsl:template match = "//w:ProfileItem[6]/child::w:Value"> 
     <xsl:element name="{local-name()}" namespace="http://worksite.imanage.com"> 
      <xsl:call-template name="changeDocumentExtension"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template name="changeDocumentExtension"> 

     <xsl:variable 
      name="iManageExtension" 
      as="xs:string" 
      select="//w:AttributeID[. = 'imProfileExtension']/following::w:Value"/> 

     <xsl:choose> 
      <xsl:when test="$iManageExtension eq 'ACROBAT'"> pdf </xsl:when> 
      <xsl:when test="$iManageExtension eq 'ANSI'"> txt </xsl:when> 
      <xsl:when test="$iManageExtension eq 'EXCEL'"> xsl </xsl:when> 
      <xsl:when test="$iManageExtension eq 'EXCELX'"> xslx </xsl:when> 
      <xsl:when test="$iManageExtension eq 'HTML'"> htm </xsl:when> 
      <xsl:when test="$iManageExtension eq 'JPEG'"> jpg </xsl:when> 
      <xsl:when test="$iManageExtension eq 'PDF230'"> pdf </xsl:when> 
      <xsl:when test="$iManageExtension eq 'PPT'"> ppt </xsl:when> 
      <xsl:when test="$iManageExtension eq 'PPTX'"> pptx </xsl:when> 
      <xsl:when test="$iManageExtension eq 'WORD'"> doc </xsl:when> 
      <xsl:when test="$iManageExtension eq 'WORDX'"> docx </xsl:when> 
      <xsl:when test="$iManageExtension eq 'WORDXT'"> docx </xsl:when> 
     </xsl:choose> 

    </xsl:template>  

</xsl:stylesheet> 
+0

+1點努力。對於我的XSLT處理器來說,這兩種解決方案都可行。你在使用哪種處理器? – 2011-06-08 04:48:40

+0

我已經改變了題目的標題以更好地匹配它,因爲如果有必要,我通常會對我的答案被接受的問題進行更正。隨意恢復它,如果你介意。 – 2011-06-08 05:47:32

+0

@empo:Saxon PE 9.3.0.5。 – 2011-06-08 06:57:38