首页 > 解决方案 > 在 XSLT 属性名称中使用空格

问题描述

我有以下 XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <document>
      <name>Example</name>
      <date>2020-10-31</date>
      <level>X_LEGAL_ABC</level>
      <bundle>
         <b001>E_AT01</b001>
         <example_id>00000000000007372165</example_id>
         <field>
            <field_value>EUR</field_value>
            <fieldname>CUR007</fieldname>
         </field>
         <field>
            <field_value>2018-06-30</field_value>
            <fieldname>C207</fieldname>
         </field>
         <field>
            <field_value>2014-07-01</field_value>
            <fieldname>C206</fieldname>
         </field>
      </bundle>
      <bundle>
         <b001>E_AT01</b001>
         <example_id>00000000000007372163</example_id>
         <field>
            <field_value>EUR</field_value>
            <fieldname>CUR007</fieldname>
         </field>
         <field>
            <field_value>2020-05-31</field_value>
            <fieldname>C207</fieldname>
         </field>
         <field>
            <field_value>2014-06-01</field_value>
            <fieldname>C206</fieldname>
         </field>
      </bundle>
</document>
</root>

使用以下 XSLT 文件:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version='1.1' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    <xsl:output method="xml" indent="yes"/> 
    
    <xsl:template match="/">
        <document>
            <xsl:attribute name="name"><xsl:value-of select="/root/document/name"/></xsl:attribute>
            <xsl:attribute name="date"><xsl:value-of select="/root/document/date"/></xsl:attribute>
            <xsl:attribute name="level"><xsl:value-of select="/root/document/level"/></xsl:attribute>
            <xsl:for-each select="/root/document/bundle">
        <bundle>
            <xsl:attribute name="Example_id"><xsl:value-of select="example_id"/></xsl:attribute>
            <xsl:attribute name="B001"><xsl:value-of select="B001"/></xsl:attribute>
            <xsl:for-each select="./field">
        
                <field>
                <xsl:attribute name="FIELDVALUE"><xsl:value-of select="field_value"/></xsl:attribute>
                <xsl:attribute name="FIELDNAME"><xsl:value-of select="fieldname"/></xsl:attribute>
                </field>
            </xsl:for-each>
        </bundle>
            </xsl:for-each>
        </document>
        
    </xsl:template>
</xsl:stylesheet>

这为我提供了一种新的 XML 格式:

<?xml version="1.0" encoding="UTF-8"?>
<document name="Example" date="2020-10-31" level="X_LEGAL_ABC">
   <bundle Example_id="00000000000007372165" B001="">
      <field FIELDVALUE="EUR" FIELDNAME="CUR007"/>
      <field FIELDVALUE="2018-06-30" FIELDNAME="C207"/>
      <field FIELDVALUE="2014-07-01" FIELDNAME="C206"/>
   </bundle>
   <bundle Example_id="00000000000007372163" B001="">
      <field FIELDVALUE="EUR" FIELDNAME="CUR007"/>
      <field FIELDVALUE="2020-05-31" FIELDNAME="C207"/>
      <field FIELDVALUE="2014-06-01" FIELDNAME="C206"/>
   </bundle>
</document>

但是,我希望字段字段值显示为字段值。所以中间有一个空格。这可能在 XSLT 文件中吗?

我可以更改:<xsl:attribute name="FIELDVALUE"

变成类似:<xsl:attribute name="FIELD VALUE"

这可能吗?因为如果我尝试运行它,它就不起作用。

我是这个 XSLT 工作的新手,所以任何帮助都将不胜感激。

此致,

迈克尔

标签: xmlxslt

解决方案


不,这是不可能的*,因为 XML规范不允许元素和属性的名称中有空格。


(*) 如果您将输出构造为文本,这是可能的,但结果不会是格式良好的 XML 文档。


推荐阅读