2012-09-27 97 views
0

我有下面的XML問題轉換XML使用XSLT

<?xml version="1.0" encoding="UTF-8" ?> 
<GovTalkMessage xsi:schemaLocation="http://www.govtalk.gov.uk/CM/envelope  http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd" xmlns="http://www.govtalk.gov.uk/CM/envelope" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > 
    <EnvelopeVersion>1.0</EnvelopeVersion> 
    <Header> 
     <MessageDetails> 
      <Class>NumberSearch</Class> 
      <Qualifier>response</Qualifier> 
      <TransactionID>4c5cf4a9e1a44cbbbe800ad9ea9f06fd</TransactionID> 
      <GatewayTimestamp>2012-09-27T18:34:19-00:00</GatewayTimestamp> 
     </MessageDetails> 
     <SenderDetails> 
     <IDAuthentication> 
      <SenderID>XMLGatewayTestUserID</SenderID> 
       <Authentication> 
        <Method>CHMD5</Method> 
        <Value></Value> 
       </Authentication> 
     </IDAuthentication> 
     </SenderDetails> 
    </Header> 
    <GovTalkDetails> 
    <Keys/> 
    </GovTalkDetails> 
    <Body> 
    <NumberSearch xmlns="http://xmlgw.companieshouse.gov.uk/v1-0/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema http://xmlgw.companieshouse.gov.uk/v1-0/schema/NumberSearch.xsd"> 
    <SearchRows>1</SearchRows> 
    <CoSearchItem> 
     <CompanyName>MILLENNIUM STADIUM PLC</CompanyName> 
     <CompanyNumber>03176906</CompanyNumber> 
     <DataSet>LIVE</DataSet> 
     <CompanyIndexStatus></CompanyIndexStatus> 
     <CompanyDate></CompanyDate> 
    </CoSearchItem> 
</NumberSearch> 
</Body> 
</GovTalkMessage> 

而且我想用XSLT把它翻譯成以下;

<?xml version="1.0"?> 
<CompanySearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <RegistrationNumber>03176906</RegistrationNumber> 
    <RegisteredName>MILLENNIUM STADIUM PLC</RegisteredName> 
</CompanySearchResult> 

目前,我有以下XSLT文件

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:ev="http://www.govtalk.gov.uk/CM/envelope" 
      xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:sr="http://xmlgw.companieshouse.gov.uk/v1-0/schema/NumberSearch.xsd"> 
<xsl:template match="/"> 
    <CompanySearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <RegistrationNumber> 
      <xsl:value-of select="ev:GovTalkMessage/ev:Body/ev:NumberSearch/ev:CoSearchItem/ev:CompanyNumber"/> 
     </RegistrationNumber> 
     <RegisteredName> 
      <xsl:value-of select="ev:GovTalkMessage/ev:Body/ev:NumberSearch/ev:CoSearchItem/ev:CompanyName"/> 
     </RegisteredName> 
    </CompanySearchResult> 
    </xsl:template> 
    </xsl:stylesheet> 

不過我剛開始一個空白的RegistrationNumber和RegistrationName -

什麼我需要改變,以正確地得到這些。

在此先感謝

回答

1

既然你沒有提供的規則通過這種變換應該發生(例如,是否有過不止一個搜索行?),這裏是一個簡短的,而unintelligently生產XSLT是完成你問什麼。

當這個XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:x="http://www.govtalk.gov.uk/CM/envelope" 
    xmlns:t="http://xmlgw.companieshouse.gov.uk/v1-0/schema" 
    exclude-result-prefixes="x t" 
    version="1.0"> 
    <xsl:output omit-xml-declaration="no" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/*"> 
    <CompanySearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <RegistrationNumber> 
     <xsl:value-of select="x:Body/*/*/t:CompanyNumber" />  
     </RegistrationNumber> 
     <RegisteredName> 
     <xsl:value-of select="x:Body/*/*/t:CompanyName" />  
     </RegisteredName> 
    </CompanySearchResult> 
    </xsl:template> 

</xsl:stylesheet> 

......被應用到最初提供XML:

<?xml version="1.0" encoding="UTF-8"?> 
<GovTalkMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns="http://www.govtalk.gov.uk/CM/envelope" xsi:schemaLocation="http://www.govtalk.gov.uk/CM/envelope  http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd"> 
    <EnvelopeVersion>1.0</EnvelopeVersion> 
    <Header> 
    <MessageDetails> 
     <Class>NumberSearch</Class> 
     <Qualifier>response</Qualifier> 
     <TransactionID>4c5cf4a9e1a44cbbbe800ad9ea9f06fd</TransactionID> 
     <GatewayTimestamp>2012-09-27T18:34:19-00:00</GatewayTimestamp> 
    </MessageDetails> 
    <SenderDetails> 
     <IDAuthentication> 
     <SenderID>XMLGatewayTestUserID</SenderID> 
     <Authentication> 
      <Method>CHMD5</Method> 
      <Value/> 
     </Authentication> 
     </IDAuthentication> 
    </SenderDetails> 
    </Header> 
    <GovTalkDetails> 
    <Keys/> 
    </GovTalkDetails> 
    <Body> 
    <NumberSearch xmlns="http://xmlgw.companieshouse.gov.uk/v1-0/schema" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema http://xmlgw.companieshouse.gov.uk/v1-0/schema/NumberSearch.xsd"> 
     <SearchRows>1</SearchRows> 
     <CoSearchItem> 
     <CompanyName>MILLENNIUM STADIUM PLC</CompanyName> 
     <CompanyNumber>03176906</CompanyNumber> 
     <DataSet>LIVE</DataSet> 
     <CompanyIndexStatus/> 
     <CompanyDate/> 
     </CoSearchItem> 
    </NumberSearch> 
    </Body> 
</GovTalkMessage> 

......想要的結果產生:

<?xml version="1.0"?> 
<CompanySearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <RegistrationNumber>03176906</RegistrationNumber> 
    <RegisteredName>MILLENNIUM STADIUM PLC</RegisteredName> 
</CompanySearchResult> 

請注意正確使用這兩個名稱空間來完成此轉換工作。我相信你的失敗是因爲你只指定了一個命名空間(對於<CompanyNumber><CompanyName>元素,這是不正確的)。

+0

謝謝 - 做到了。我不知道是否會有多個條目 - 我嘗試過的每個搜索都只能帶回一個結果 - 即使是通配符也是如此,因此我假設它只返回一個結果。 –