简体   繁体   English

使用XSL转换XML时添加名称空间

[英]Adding a namespace when transforming XML using XSL

I am trying to use a XSL stylesheet to convert the Source.xml file to the Desired.xml format using the Transform.xsl file. 我正在尝试使用XSL样式表使用Transform.xsl文件将Source.xml文件转换为Desired.xml格式。 I managed to transform it correctly except the namespace attribute in the node. 我设法正确地对其进行了转换,但节点中的名称空间属性除外。 Does anyone know what I need to put into the xsl transform file to add that attribute? 有谁知道我需要在xsl转换文件中添加什么才能添加该属性?

Desired.xml Desired.xml

<?xml version="1.0"?>
<XML_FAX_SUBMIT java="0" xmlns="x-schema:C:\rf\XML_FAX_SUBMIT_schema.xml" stylesheet="C:\rf\XML_FAX_SUBMIT.XSL">
  <SENDER>
    <FROM_NAME>John Public</FROM_NAME>
    <RF_USER>JOHN</RF_USER>
  </SENDER>
  <DESTINATIONS>
    <FAX unique_id="1">
      <TO_FAXNUM>1234</TO_FAXNUM>
      <TO_NAME>Public</TO_NAME>
      <TO_COMPANY>ACME</TO_COMPANY>
      <NOTIFY_HOST SuccessTemplate="1.inc" FailureTemplate="2.inc" Name="NotifyImportServer"/>
    </FAX>
  </DESTINATIONS>
  <ATTACHMENT>
    <FILE path="c:\test\test.tif"/>
  </ATTACHMENT>
</XML_FAX_SUBMIT>

Source.xml Source.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DrivveImage>
  <Documents>
    <Document Profile="Test" Profile-ID="0xA84E80AD7068B048B1B99E12E258F1B3" File="test.tif" Destination="C:\test" ImageFilePath="C:\test\test.tif" Pages="1">
      <Fields>        
        <Field Name="FROM_NAME">John Public</Field>
        <Field Name="RF_USER">JOHN</Field>
        <Field Name="TO_FAXNUM">1234</Field>
        <Field Name="TO_NAME">Public</Field>
        <Field Name="TO_COMPANY">ACME</Field>
        <Field Name="UNIQUE_ID">1</Field>
       </Fields>
    </Document>
  </Documents>
</DrivveImage>

Transform.xsl 转换文件

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" media-type="text/xml" indent="yes"/>
    <xsl:template match="/DrivveImage/Documents/Document">
        <xsl:element name="XML_FAX_SUBMIT">
            <xsl:attribute name="stylesheet">
                <xsl:value-of select="string('C:\RF\XML_FAX_SUBMIT.XSL')"/>
            </xsl:attribute>
            <xsl:attribute name="java">
                <xsl:value-of select="0"/>
            </xsl:attribute> 
            <SENDER>      
                <FROM_NAME>
                    <xsl:value-of select="Fields/Field[@Name='FROM_NAME']"/>
                </FROM_NAME>
                <RF_USER>
                    <xsl:value-of select="Fields/Field[@Name='RF_USER']"/>
                </RF_USER>
            </SENDER>
            <DESTINATIONS>        
                <FAX>
                    <xsl:attribute name="unique_id">
                        <xsl:value-of select="Fields/Field[@Name='UNIQUE_ID']"/>
                    </xsl:attribute>         
                    <TO_FAXNUM>
                        <xsl:value-of select="Fields/Field[@Name='TO_FAXNUM']"/>
                    </TO_FAXNUM>
                    <TO_NAME>
                        <xsl:value-of select="Fields/Field[@Name='TO_NAME']"/>
                    </TO_NAME>
                    <TO_COMPANY>
                        <xsl:value-of select="Fields/Field[@Name='TO_COMPANY']"/>
                    </TO_COMPANY>
                    <NOTIFY_HOST SuccessTemplate="1.inc" FailureTemplate="1.inc" Name="NotifyImportServer" />
                </FAX>
            </DESTINATIONS>
            <xsl:element name="ATTACHMENT"> 
                <FILE>
                    <xsl:attribute name="path"><xsl:value-of select="@Destination"/>\<xsl:value-of select="@File"/></xsl:attribute>
                </FILE>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Thanks in advance. 提前致谢。

Strictly speaking, it is not a namespace attribute, but a namespace declaration. 严格来说,它不是名称空间属性,而是名称空间声明。 You are declaring that all elements within your desired XML are part of this namespace. 您声明所需XML中的所有元素都是此名称空间的一部分。 However, in your source XML, there is no namespace declaration, and so none of the source elements are in a namespace. 但是,在您的源XML中,没有名称空间声明,因此没有任何源元素在名称空间中。 Furthermore, there is no reference to the namespace in the XSLT, and so as a result your current output has no namespaces. 此外,XSLT中没有引用名称空间,因此,您当前的输出没有名称空间。

What you need to do is ensure that the elements you output in your XSLT belong to the namespace your require. 您需要做的是确保您在XSLT中输出的元素属于您所需的名称空间。 You can do use by using the namespace attribute on the xsl:element command used to create the root XML_FAX_SUBMIT element: 您可以通过在用于创建根XML_FAX_SUBMIT元素的xsl:element命令上使用namespace属性来使用:

<xsl:element name="XML_FAX_SUBMIT" namespace="x-schema:C:\rf\XML_FAX_SUBMIT_schema.xml">

However, when used in this way it will only set the namespace of the specified element. 但是,以这种方式使用时,它将仅设置指定元素的名称空间。 You will also need to set this on the SENDER and DESTINATIONS element, for example 例如,您还需要在SENDERDESTINATIONS元素上进行设置

<SENDER xmlns="x-schema:C:\rf\XML_FAX_SUBMIT_schema.xml">

<DESTINATIONS xmlns="x-schema:C:\rf\XML_FAX_SUBMIT_schema.xml">

Doing it this way means all the child elements belong to the same namespace too. 这样做意味着所有子元素也属于同一个名称空间。

Infact, it would be easier if you created XML_FAX_SUBMIT element in this way 实际上,如果以这种方式创建XML_FAX_SUBMIT元素会更容易

<XML_FAX_SUBMIT xmlns="x-schema:C:\rf\XML_FAX_SUBMIT_schema.xml">

Then you would only have to add the namespace declaration in one place. 然后,您只需要在一个地方添加名称空间声明即可。

Try the following XSLT 尝试以下XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" encoding="UTF-8" media-type="text/xml" indent="yes"/>
  <xsl:template match="/DrivveImage/Documents/Document">
    <XML_FAX_SUBMIT xmlns="x-schema:C:\rf\XML_FAX_SUBMIT_schema.xml" stylesheet="{string('C:\RF\XML_FAX_SUBMIT.XSL')}" java="0">
      <xsl:attribute name="stylesheet">
        <xsl:value-of select="string('C:\RF\XML_FAX_SUBMIT.XSL')"/>
      </xsl:attribute>
      <xsl:attribute name="java">
        <xsl:value-of select="0"/>
      </xsl:attribute>
      <SENDER>
        <FROM_NAME>
          <xsl:value-of select="Fields/Field[@Name='FROM_NAME']"/>
        </FROM_NAME>
        <RF_USER>
          <xsl:value-of select="Fields/Field[@Name='RF_USER']"/>
        </RF_USER>
      </SENDER>
      <DESTINATIONS>
        <FAX unique_id="{Fields/Field[@Name='UNIQUE_ID']}">
          <TO_FAXNUM>
            <xsl:value-of select="Fields/Field[@Name='TO_FAXNUM']"/>
          </TO_FAXNUM>
          <TO_NAME>
            <xsl:value-of select="Fields/Field[@Name='TO_NAME']"/>
          </TO_NAME>
          <TO_COMPANY>
            <xsl:value-of select="Fields/Field[@Name='TO_COMPANY']"/>
          </TO_COMPANY>
          <NOTIFY_HOST SuccessTemplate="1.inc" FailureTemplate="1.inc" Name="NotifyImportServer" />
        </FAX>
      </DESTINATIONS>
      <ATTACHMENT>
        <FILE path="{@Destination}\{@File}">
        </FILE>
      </ATTACHMENT>
    </XML_FAX_SUBMIT>
  </xsl:template>
</xsl:stylesheet>

Also note the use of "Attribute Value Templates" to set the attributes, instead of xsl:attribute . 还要注意使用“属性值模板”来设置属性,而不是xsl:attribute Where you see curly braces { } in an attribute, this indicates an expression to be evaluated, as opposed output literally. 在属性中看到花括号{}的地方,这表示要求值的表达式,与字面上的输出相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM