2014-04-15 51 views
0

我有以下XML文件作爲輸入,並且對於<CI_DateTypeCode>元素的每個實例,我需要更改@codeListValue屬性值。XSLT避免在元素屬性之間添加新行

該轉換工作,但我得到輸出不必要/不需要的新行@codeList屬性和@codeListValue屬性之間。

如您所見,源XML文件中有屬性之間沒有新行。 我該如何避免這種行爲?

XML源文件

<?xml version="1.0"?> 
    <?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?> 
    <MD_Metadata xmlns="http://www.isotc211.org/schemas/2005/gmd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/schemas/2005/gco" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.isotc211.org/schemas/2005/gmd/gmd.xsd"> 
     <MD_DataIdentification> 
      <citation> 
       <CI_Citation> 
        <title> 
         <gco:CharacterString>Title</gco:CharacterString> 
        </title> 
        <date> 
         <CI_Date> 
          <date> 
           <gco:CharacterString>2014-04-15</gco:CharacterString> 
          </date> 
          <dateType> 
           <CI_DateTypeCode codeList="./resource/codeList.xml#CI_DateTypeCode" codeListValue="Creazione">Creazione</CI_DateTypeCode> 
          </dateType> 
         </CI_Date> 
        </date> 
       </CI_Citation> 
      </citation> 
     </MD_DataIdentification> 
    </MD_Metadata> 

XSL轉換

<xsl:stylesheet 
    version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:gml="http://www.opengis.net/gml/3.2" 
    xmlns:gco="http://www.isotc211.org/schemas/2005/gco" 
    xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd" 
    xmlns="http://www.isotc211.org/schemas/2005/gmd" 
    > 

    <xsl:strip-space elements="*"/> 

    <xsl:output indent="yes" encoding="UTF-8"/> 

    <!-- identity template --> 
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*" /> 
     </xsl:copy> 
    </xsl:template> 

    <!-- <CI_DateTypeCode>: @codeListValue attribute values --> 
    <xsl:param name="pCIDateTypeCodeCreation" select="'creation'"/> 
    <xsl:param name="pCIDateTypeCodePublication" select="'publication'"/> 
    <xsl:param name="pCIDateTypeCodeRevision" select="'revision'"/> 
    <xsl:param name="pCIDateTypeCodeOther" select="''"/> 

    <!-- override: <CI_DateTypeCode> to apply @codeListValue attribute values --> 
    <xsl:template match="gmd:CI_DateTypeCode/@codeListValue"> 
     <xsl:choose> 
      <xsl:when test="lower-case(.) eq 'creazione'"> 
       <xsl:attribute name="codeListValue"> 
        <xsl:value-of select="$pCIDateTypeCodeCreation"/> 
       </xsl:attribute> 
      </xsl:when> 
      <xsl:when test="lower-case(.) eq 'pubblicazione'"> 
       <xsl:attribute name="codeListValue"> 
        <xsl:value-of select="$pCIDateTypeCodePublication"/> 
       </xsl:attribute> 
      </xsl:when> 
      <xsl:when test="lower-case(.) eq 'revisione'"> 
       <xsl:attribute name="codeListValue"> 
        <xsl:value-of select="$pCIDateTypeCodeRevision"/> 
       </xsl:attribute> 
      </xsl:when> 
      <xsl:when test="lower-case(.) eq 'rilievo'"> 
       <xsl:attribute name="codeListValue"> 
        <xsl:value-of select="$pCIDateTypeCodeOther"/> 
       </xsl:attribute> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:template>  
</xsl:stylesheet> 

實際XML輸出(見與縮進@codeList@codeListValue屬性之間新行):

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?> 
<MD_Metadata xmlns="http://www.isotc211.org/schemas/2005/gmd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:gco="http://www.isotc211.org/schemas/2005/gco" 
      xmlns:gml="http://www.opengis.net/gml" 
      xmlns:xlink="http://www.w3.org/1999/xlink" 
      xsi:schemaLocation="http://www.isotc211.org/schemas/2005/gmd/gmd.xsd"> 
    <MD_DataIdentification> 
     <citation> 
     <CI_Citation> 
      <title> 
       <gco:CharacterString>Title</gco:CharacterString> 
      </title> 
      <date> 
       <CI_Date> 
        <date> 
        <gco:CharacterString>2014-04-15</gco:CharacterString> 
        </date> 
        <dateType> 
        <CI_DateTypeCode codeList="./resource/codeList.xml#CI_DateTypeCode" 
             codeListValue="creation">Creazione</CI_DateTypeCode> 
        </dateType> 
       </CI_Date> 
      </date> 
     </CI_Citation> 
     </citation> 
    </MD_DataIdentification> 
</MD_Metadata> 
+0

爲什麼重要?單個標籤中屬性之間是否存在空格不會影響XML解析器。此外,我不認爲有任何方法可以在XSLT中控制它 - XSLT生成輸出樹結構,但如何將其呈現爲XML取決於序列化程序。 –

+1

更改'的xsl:output'所以不會縮進:'的' – helderdarocha

回答

1

顯示的輸出看起來好像來自撒克遜。我個人認爲它讀得很好。當然,你根本不需要縮進(在xsl:output上設置indent="no")。

如果您的確在使用Saxon,您可以通過使用<xsl:output saxon:line-length="1000"/>增加最大行長來減少屬性換行到新行的可能性,但請注意,只要此行帶有行的文件可能會導致一些文本編輯。

http://www.saxonica.com/documentation/index.html#!extensions/output-extras/line-length

+1

@all如果設置'縮進= 「否」'我失去所有縮進,這不是我想要的......我將Saxon與oXygen XML Editor一起使用,但這只是爲了創建轉換過程中的方便。然後,我需要使用Windows Form Application和.NET'XslCompiledTransform'類將最終轉換應用於許多文件,並傳遞xsl文件。 –