2016-05-02 30 views
0

我很難讓XSL轉換在.NET代碼中成功運行,即使它在VS2012中通過XSLT調試器運行時運行良好。我使用的是VS2012,因爲XSLT調試器在VS2015中不起作用。XslCompiled在.NET代碼中轉換錯誤,但轉換在調試器中工作

我剝了下來我的代碼下面的步驟,其產生XmlException「空字符串‘’不是一個有效的名稱」:

string xsltpath = "switch-onix-tagnames-1.1.xsl"; 
string xmlpath = "Testing.xml"; 
var xct = new System.Xml.Xsl.XslCompiledTransform(true); 
var settings = new XmlWriterSettings(); 

var xtr = new XmlTextReader(xmlpath); 

settings.OmitXmlDeclaration = false; 
settings.ConformanceLevel = ConformanceLevel.Document; 
settings.CloseOutput = false; 

using (var ms = new MemoryStream()) 
{ 
    using (XmlWriter xw = XmlWriter.Create(ms, settings)) 
    { 
     xct.Load(xsltpath); 
     xct.Transform(xtr, xw); 
     Debugger.Break(); 
    } 
} 

的「Testing.xml」文件是:

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE ONIXmessage SYSTEM "http://www.editeur.org/onix/2.1/short/onix-international.dtd" > 
<ONIXmessage release="2.1"> 
    <header> 
    <m174>Some Publisher</m174> 
    <m175>Some Person 978-999-9999</m175> 
    <m283>[email protected]</m283> 
    <m182>20160226</m182> 
    <m183>Title information from Some Publisher</m183> 
    <m184>eng</m184> 
    <m185>01</m185> 
    <m186>USD</m186> 
    <m187>in</m187> 
    <m193>General Trade</m193> 
    </header> 
    <product> 
    <a001>001001-48747641</a001> 
    <a002>03</a002> 
    <productidentifier> 
     <b221>02</b221> 
     <b244>1433692376</b244> 
    </productidentifier> 
    <productidentifier> 
     <b221>03</b221> 
     <b244>9781433692376</b244> 
    </productidentifier> 
    <productidentifier> 
     <b221>15</b221> 
     <b244>9781433692376</b244> 
    </productidentifier> 
    <b012>DG</b012> 
    <b014>EPUB</b014> 
    <b211>029</b211> 
    <b213>Open Ebook Format</b213> 
    <b214>06</b214> 
    <series> 
     <b018>The Gospel Project</b018> 
    </series> 
    <title> 
     <b202>01</b202> 
     <b203>La Gran Historia, Relatos B&#237;blicos para los m&#225;s peque&#241;os, del Antiguo Testamento</b203> 
    </title> 
    <workidentifier> 
     <b201>15</b201> 
     <b244>9781433692376</b244> 
    </workidentifier> 
    <contributor> 
     <b034>1</b034> 
     <b035>A01</b035> 
     <b047>Some Editorial Staff</b047> 
    </contributor> 
    <contributor> 
     <b034>2</b034> 
     <b035>A12</b035> 
     <b036>Heath McPherson</b036> 
     <b037>McPherson, Heath</b037> 
     <b039>Heath</b039> 
     <b040>McPherson</b040> 
     <b044><![CDATA[<DIV><B>Heath McPherson</B> dirige un peque&ntilde;o estudio art&iacute;stico que produce ilustraciones ingeniosas y animaci&oacute;n para organizaciones en diversas localidades de los Estados Unidos. Su obra incluye <I>Timmy and Tammy&#39;s Train of Thought</I> (El tren del pensamiento de Timmy y Tammy), escrito por Oliver Chin, como as&iacute; tambi&eacute;n varios comerciales animados.<BR /><BR /><B>Heath McPherson</B> runs a small studio that creates whimsical illustrations and animation for folks across the country. His work includes <I>Timmy and Tammy&#39;s Train of Thought</I> by Oliver Chin as well a number of animated commercials.</DIV>]]></b044> 
    </contributor> 
    <b049>by Some Editorial Staff and Heath McPherson</b049> 
    <b058>Spanish Language Edition</b058> 
    <language> 
     <b253>01</b253> 
     <b252>spa</b252> 
    </language> 
    <b061>44</b061> 
    <b064>REL012000</b064> 
    <audience> 
     <b204>01</b204> 
     <b206>02</b206> 
    </audience> 
    <audiencerange> 
     <b074>11</b074> 
     <b075>04</b075> 
     <b076>P</b076> 
    </audiencerange> 
    <audiencerange> 
     <b074>17</b074> 
     <b075>03</b075> 
     <b076>4</b076> 
     <b075>04</b075> 
     <b076>4</b076> 
    </audiencerange> 
    <othertext> 
     <d102>01</d102> 
     <d103>02</d103> 
     <d104><![CDATA[<DIV><I>Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento)<BR /> Presente la Biblia a los aprendices m&aacute;s j&oacute;venes con <I>Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento), edici&oacute;n de cart&oacute;n</I>. Incluye veinte historias del Antiguo Testamento en un nuevo formato interactivo, divertido y atractivo de relatos b&iacute;blicos para los chiquitos que empiezan a caminar.<BR /> Cada historia se caracteriza por ilustraciones en cuatro colores resaltados que, utilizadas en conjunto con la aplicaci&oacute;n &ldquo;B&amp;H Kids AR&rdquo; [realidad aumentada], cobrar&aacute;n vida con im&aacute;genes animadas y la lectura grabada por Jenna Lucado.<BR /> Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento), edici&oacute;n de cart&oacute;n, </I>es el producto m&aacute;s reciente en la familia de libros de <I>Big Picture Interactive</I> &mdash;una serie dise&ntilde;ada para crecer con la familia y fortalecer relaciones en Cristo por medio de publicaciones centradas en la Biblia y difundidas por plataformas m&uacute;ltiples para apasionar a lectores de toda edad.</DIV>]]></d104> 
    </othertext> 
    <othertext> 
     <d102>02</d102> 
     <d103>02</d103> 
     <d104><![CDATA[<DIV><I>Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento)<BR /> Presente la Biblia a los aprendices m&aacute;s j&oacute;venes con <I>Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento), edici&oacute;n de cart&oacute;n</I>. Incluye veinte historias del Antiguo Testamento en un nuevo formato interactivo, divertido y atractivo de relatos b&iacute;blicos para los chiquitos que empiezan a caminar.<BR /> Cada historia se caracteriza por ilustraciones en cuatro colores resaltados que, utilizadas en conjunto con la aplicaci&oacute;n &ldquo;B&amp;H Kids AR&rdquo; [realidad aumentada], cobrar&aacute;n vida con im&aacute;genes animadas y la lectura grabada por Jenna Lucado.<BR /> Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento), edici&oacute;n de cart&oacute;n, </I>es el producto m&aacute;s reciente en la familia de libros de <I>Big Picture Interactive</I> &mdash;una serie dise&ntilde;ada para crecer con la familia y fortalecer relaciones en Cristo por medio de publicaciones centradas en la Biblia y difundidas por plataformas m&uacute;ltiples para apasionar a lectores de toda edad.<BR /><BR /> The Big Picture Interactive Bible Stories for Toddlers Old Testament<BR /> Introduce early learners to the Bible with <I>The Big Picture Interactive Bible Stories for Toddlers Old Testament Board Book</I>, featuring twenty Old Testament Bible Stories in this fun, interactive and engaging new Bible storybook designed just for toddlers.<BR /> Each story features enhanced four-color illustrations that used in conjunction with the B&amp;H Kids AR [Augmented Reality] app brings the pictures to life with pop-up imaging and read-a-long narration from Jenna Lucado.<BR /><I>The Big Picture Interactive Bible Stories for Toddlers Old Testament Board Book</I> is the latest in the family of <I>Big Picture Interactive</I> books &ndash; a series designed to grow with families and strengthen relationships in Christ through Bible-centered, multi-platform products that engage readers of all ages.<DIV>&#160;</DIV><DIV>&#160;</DIV><DIV>&#160;</DIV></DIV>]]></d104> 
    </othertext> 
    <othertext> 
     <d102>13</d102> 
     <d103>02</d103> 
     <d104><![CDATA[<DIV><B>Heath McPherson</B> dirige un peque&ntilde;o estudio art&iacute;stico que produce ilustraciones ingeniosas y animaci&oacute;n para organizaciones en diversas localidades de los Estados Unidos. Su obra incluye <I>Timmy and Tammy&#39;s Train of Thought</I> (El tren del pensamiento de Timmy y Tammy), escrito por Oliver Chin, como as&iacute; tambi&eacute;n varios comerciales animados.<BR /><BR /><B>Heath McPherson</B> runs a small studio that creates whimsical illustrations and animation for folks across the country. His work includes <I>Timmy and Tammy&#39;s Train of Thought</I> by Oliver Chin as well a number of animated commercials.</DIV>]]></d104> 
    </othertext> 
    <imprint> 
     <b241>01</b241> 
     <b242>Publisher Cloud Imprint Key</b242> 
     <b243>4028300</b243> 
     <b079>Publisher</b079> 
    </imprint> 
    <publisher> 
     <b291>01</b291> 
     <b241>01</b241> 
     <b242>Publisher Cloud Publisher Key</b242> 
     <b243>4028289</b243> 
     <b081>Some Publisher Group</b081> 
    </publisher> 
    <b394>04</b394> 
    <b003>20160201</b003> 
    <b087>2016</b087> 
    <salesrights> 
     <b089>02</b089> 
     <b090>AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW</b090> 
    </salesrights> 
    <measure> 
     <c093>03</c093> 
     <c094>1</c094> 
     <c095>in</c095> 
    </measure> 
    <measure> 
     <c093>03</c093> 
     <c094>25.4</c094> 
     <c095>mm</c095> 
    </measure> 
    <measure> 
     <c093>08</c093> 
     <c094>1.1</c094> 
     <c095>lb</c095> 
    </measure> 
    <measure> 
     <c093>08</c093> 
     <c094>17.6</c094> 
     <c095>oz</c095> 
    </measure> 
    <measure> 
     <c093>08</c093> 
     <c094>498.95</c094> 
     <c095>gr</c095> 
    </measure> 
    <relatedproduct> 
     <h208>13</h208> 
     <productidentifier> 
     <b221>15</b221> 
     <b244>9781433689680</b244> 
     </productidentifier> 
    </relatedproduct> 
    <supplydetail> 
     <j137>Some Publisher</j137> 
     <j268>02</j268> 
     <j269>Y</j269> 
     <j141>IP</j141> 
     <j396>20</j396> 
     <j143>20160201</j143> 
     <j145>30</j145> 
     <price> 
     <j148>01</j148> 
     <discountcoded> 
      <j363>02</j363> 
      <j364>0040</j364> 
     </discountcoded> 
     <j151>9.99</j151> 
     <j152>USD</j152> 
     <j161>20160127</j161> 
     </price> 
    </supplydetail> 
    </product> 
</ONIXmessage> 

和XSL文件是:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <xsl:variable name="release" select="/*/@release" /> 

    <xsl:variable name="target"> 
     <xsl:choose> 
      <xsl:when test="/ONIXMessage">short</xsl:when> 

      <xsl:otherwise>reference</xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 

    <xsl:variable name="dtd-url"> 
     http://www.editeur.org/onix/ 
     <xsl:value-of select="$release" /> 

     /
     <xsl:value-of select="$target" /> 

     /onix-international.dtd 
    </xsl:variable> 

    <xsl:output method="xml" doctype-system="{$dtd-url}" /> 

    <xsl:template match="*"> 
     <xsl:variable name="target-name"> 
      <xsl:choose> 
       <xsl:when test="$target='short'"> 
        <xsl:value-of select="@shortname" /> 
       </xsl:when> 

       <xsl:otherwise> 
        <xsl:value-of select="@refname" /> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:variable> 

     <xsl:element name="{$target-name}"> 
      <xsl:copy-of select="@*[not(name()='refname' or name()='shortname')]" /> 

      <xsl:apply-templates select="*|text()" /> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="text()"> 
     <xsl:copy /> 
    </xsl:template> 
</xsl:stylesheet> 
我目前使用.NET 4.6.1

,但它似乎不是T o對我使用的.NET版本有所不同。

我在這裏錯過了一些明顯的東西嗎?我不是XML的專家,所以請在我身上輕鬆一下!

+0

一注的人測試此代碼 - 的DTD在XML文件的頂部沒有在線時間更長,我已經通過將其託管在我的Intranet上並在我的HOSTS文件中指向它來補償這一點。 – Billious

回答

1

請注意,*在您的xsl:template匹配所有元素在輸入XML。然後,在該模板中,將創建一個新元素,其名稱取自匹配元素的@shortname@refname屬性。據我所看到的,在XML不是所有的元素都有@shortname@refname屬性,這將導致以下異常在這樣的元素提高被處理:

空字符串「」不是有效的名稱。

至少,請確保該模板只有要麼@shortname@refname屬性流程元素:

<xsl:template match="*[@shortname or @refname]"> 
+0

謝謝,這正是我正在尋找的解決方法。不幸的是,它似乎沒有解決實際問題,但是 - 轉換運行正常,但生成的XML與原始版本幾乎沒有變化。我認爲問題可能是DTD沒有正確加載,結果是@shortname沒有在任何節點上匹配。我試圖使用XSD,希望這會有所作爲。 – Billious

+0

我剛剛嘗試過在執行轉換時運行Fiddler,並且沒有跡象表明DTD正在被加載。嗯... – Billious