2017-02-28 128 views
0

我有以下xml代碼,需要轉換爲文本文件。我正在用XSLT中的命名空間大規模掙扎。我習慣做Export/Record和簡單的默認命名空間匹配。我有以下XML代碼:從xml中匹配XSLT模板

<?xml version="1.0" encoding="utf-8"?> 
<ArrayOfPerson xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MultiCountryIntegrationService.Core.Entities.Dto"> 
    <Person> 
     <Addresses> 
      <Address> 
       <City>London</City> 
       <Country>GB</Country> 
       <County>London</County> 
       <CreatedDate xmlns:d5p1="http://schemas.datacontract.org/2004/07/System"> 
        <d5p1:DateTime>2017-02-21T11:05:08.8387752Z</d5p1:DateTime> 
        <d5p1:OffsetMinutes>0</d5p1:OffsetMinutes> 
       </CreatedDate> 
       <DeletedDate xmlns:d5p1="http://schemas.datacontract.org/2004/07/System" i:nil="true" /> 
       <EndDate xmlns:d5p1="http://schemas.datacontract.org/2004/07/System" i:nil="true" /> 
       <Extension i:nil="true" /> 
       <Id>8e5b30d0</Id> 
       <ModifiedDate xmlns:d5p1="http://schemas.datacontract.org/2004/07/System" i:nil="true" /> 
       <Number>8</Number> 
       <StartDate xmlns:d5p1="http://schemas.datacontract.org/2004/07/System"> 
        <d5p1:DateTime>2016-06-30T22:00:00Z</d5p1:DateTime> 
        <d5p1:OffsetMinutes>120</d5p1:OffsetMinutes> 
       </StartDate> 
       <Street>Somewhere</Street> 
       <Type>Primary</Type> 
       <ZipCode>L1 1LL</ZipCode> 
      </Address> 
     </Addresses> 
     <PersonLocalReferences xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /> 
    </Person> 
    <Person> 
     <Addresses> 
      <Address> 
       <City>Birmingham</City> 
       <Country>ETC...</Country> 
      </Address> 
     </Addresses> 
     <PersonLocalReferences xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /> 
    </Person> 
</ArrayOfPerson> 

我的XSLT - 正如你可以看到我已經嘗試了多種方法,做了谷歌和計算器無數個小時。如果我從XML中刪除i:xmlns,我可以使樣式表工作,但我無法更改XML。我正在使用Visual Studio 2016來創建並運行xslt。如果我使用"@* | node()",我就可以得到所有的東西,而且我只想將某些信息輸出到文本文件中。

如果我運行下面的xslt,我只是得到頭和EOF,所以它看起來像<xsl:apply-templates select="ArrayOfPerson/Person"/>沒有選擇正確的數據級別。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="i"> 

    <xsl:output omit-xml-declaration="yes" /> 

    <xsl:template match="/"> 
     <xsl:call-template name="header" /> 

     <xsl:text>Person</xsl:text> 
     <xsl:apply-templates select="ArrayOfPerson/Person"/> 

     <xsl:value-of select="$newline" /> 
     <xsl:text>EOF</xsl:text> 
    </xsl:template> 

    <xsl:template match="Person"> 
     <xsl:value-of select="ArrayOfPerson/Person/Addresses/Address/City"/> 
     <xsl:value-of select="Person/Addresses/Address/City"/> 
     <xsl:value-of select="Addresses/Address/City"/> 
     <xsl:value-of select="."/> 
     <xsl:value-of select="$newline" /> 
     <xsl:text>match</xsl:text> 
     <xsl:value-of select="$newline" /> 
    </xsl:template> 
</xsl:stylesheet> 
+0

你已經標記了問題xslt 2.0,你的樣式表示版本=「2.0」,但是你說你正在使用Visual Studio 2016,我認爲它只支持1.0。這會影響您是否可以使用xpath-default-namespace屬性。 –

+0

如果您搜索「XSLT默認命名空間」,您會發現這個問題的一千個答案。 ArrayOfPerson位於名稱空間中,但您在沒有名稱空間中選擇ArrayOfPerson。 –

+0

感謝您的信息。我沒有意識到Visual Studio無法做到v2.0與我得到的其他一些錯誤是合理的。 – Itsallgonepearshaped

回答

0

正如邁克爾在他的評論中寫道,加xpath-default-namespace="http://schemas.datacontract.org/2004/07/MultiCountryIntegrationService.Core.Entities.Dto" 到您的xsl:stylesheet標記。

請注意,您必須包含全部命名空間。