1

我们正在尝试在我们的 XML 文件中添加两个额外的命名空间/属性。该结构是一个外部定义 (XSD),我们想知道是否可以通过 XSLT 添加两个新的属性/命名空间,或者这是否应该包含在外部定义中?(当然,更新外部定义是最简单的方法。)

我已经在这里查看了几个问题,例如:
向节点
添加属性 使用 xslt 向子元素添加命名空间
XSLT 转换会引发错误

但是我仍然对如何使这项工作一无所知。在 XSLT 方面,我是处女——完全没有经验。想知道这是否可以通过 XSLT 实现。

原样

<ns2:ProcessCommunication xmlns:ns2="http://URL">
   <ns2:communication>
      <ns2:CommunicationTemplateAbbreviation>INV</ns2:CommunicationTemplateAbbreviation>
      <ns2:CommunicationValues>
         <ns2:CommunicationValue>
            <ns2:FinancialValue>205029</ns2:FinancialValue>
            <ns2:Title>Net</ns2:Title>
         </ns2:CommunicationValue>
      </ns2:CommunicationValues>
      <ns2:CustomFields>
         <ns2:CustomField>
            <ns2:Name>SomeValue</ns2:Name>
            <ns2:Answer>
               <ns2:Value>1</ns2:Value>
            </ns2:Answer>
         </ns2:CustomField>
         <ns2:CustomField>
            <ns2:Name>Transaction Currency</ns2:Name>
            <ns2:Answer>
               <ns2:Value>EUR</ns2:Value>
            </ns2:Answer>
         </ns2:CustomField>
      </ns2:CustomFields>
   </ns2:communication>
</ns2:ProcessCommunication>

成为:

<ns2:ProcessCommunication xmlns:ns2="http://URL">
   <ns2:communication>
      <ns2:CommunicationTemplateAbbreviation>INV</ns2:CommunicationTemplateAbbreviation>
      <ns2:CommunicationValues>
         <ns2:CommunicationValue>
            <ns2:FinancialValue>205029</ns2:FinancialValue>
            <ns2:Title>Net</ns2:Title>
         </ns2:CommunicationValue>
      </ns2:CommunicationValues>
      <ns2:CustomFields>
         <ns2:CustomField>
            <ns2:Name>SomeValue</ns2:Name>
            <ns2:Answer>
               <ns2:Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">1</ns2:Value>
            </ns2:Answer>
         </ns2:CustomField>
         <ns2:CustomField>
            <ns2:Name>Transaction Currency</ns2:Name>
            <ns2:Answer>
               <ns2:Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">EUR</ns2:Value>
            </ns2:Answer>
         </ns2:CustomField>
      </ns2:CustomFields>
   </ns2:communication>
</ns2:ProcessCommunication>

还有一个额外的

i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema"

在节点中,值。

我只能走到这里,这没什么用。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>

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

<xsl:template match="node()">
<xsl:copy>
        <xsl:attribute name="i:type">a:string</xsl:attribute>
        <xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

我尝试使用<xsl:template match="Answer/Value">,但这不起作用。

4

1 回答 1

1

您只是缺少 XSLT 本身的名称空间声明。这应该有效:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"  xmlns:ns2="http://URL">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

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

<xsl:template match="ns2:Answer/ns2:Value">
<xsl:copy>
        <xsl:attribute name="i:type">a:string</xsl:attribute>
        <xsl:attribute name="xmlns:a">http://www.w3.org/2001/XMLSchema</xsl:attribute>
        <xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
于 2013-09-17T01:21:30.933 回答