【问题标题】:XSLT avoid new line added between element attributesXSLT 避免在元素属性之间添加新行
【发布时间】:2014-04-15 13:11:50
【问题描述】:

我有以下 XML 文件作为输入,对于 <CI_DateTypeCode> 元素的每个实例,我需要更改 @codeListValue 属性值。

转换有效,但我在@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>

【问题讨论】:

  • 为什么重要?单个标记中属性之间是否存在空格对 XML 解析器没有影响。此外,我认为在 XSLT 中没有任何方法可以控制这一点 - XSLT 会生成输出树结构,但如何将其呈现为 XML 取决于序列化程序。
  • 更改您的xsl:output,使其不会缩进:&lt;xsl:output indent="no" encoding="UTF-8"/&gt;

标签: xml xslt newline spaces


【解决方案1】:

显示的输出看起来好像来自撒克逊语。个人觉得读起来还不错。当然,您根本不需要缩进(在xsl:output 上设置indent="no")。

如果您确实在使用 Saxon,则可以通过使用 &lt;xsl:output saxon:line-length="1000"/&gt; 增加最大行长度来减少属性换行到新行的机会,例如:尽管请注意带有行的文件,只要这可能会搞砸一些文本编辑器。

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

【讨论】:

  • @all 如果我设置了indent="no",我会松开所有缩进,这不是我想要的...我正在使用 Saxon 和 oXygen XML 编辑器,但这只是为了在创建转换过程中方便.然后我需要使用 Windows 窗体应用程序和 .NET XslCompiledTransform 类将我的最终转换应用于许多文件,并传递 xsl 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 1970-01-01
  • 2018-03-18
  • 2020-10-09
  • 2013-08-25
  • 2016-12-03
  • 1970-01-01
相关资源
最近更新 更多