Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于另一个标记向XML标记添加属性';s值_Xml_Xslt - Fatal编程技术网

基于另一个标记向XML标记添加属性';s值

基于另一个标记向XML标记添加属性';s值,xml,xslt,Xml,Xslt,我正在绞尽脑汁研究xslt。。。正在尝试转换以下xml: <employees> <employee> <employeeNumber>1234</employeeNumber> <startdate>01/02/2003</startdate> <activeFlag>true</activeFlag> <firstnam

我正在绞尽脑汁研究xslt。。。正在尝试转换以下xml:

<employees>
    <employee>
        <employeeNumber>1234</employeeNumber>
        <startdate>01/02/2003</startdate>
        <activeFlag>true</activeFlag>
        <firstname>Erik</firstname>
        <address>
            <addressline1>123 Main</addressline1>
            <zip>07016</zip>
            <state>New Jersey</state>
            <city>My City</city>
        </address>
    </employee>
</employees>

1234
01/02/2003
真的
埃里克
123主要
07016
新泽西州
我的城市
这样做(即取出activeFlag标记值并将其放入employee标记的属性中)


1234
01/02/2003
埃里克
123主要
07016
新泽西州
我的城市
我尝试了以下XSLT,但它什么也没做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="employees/employee">       
        <employee active="{activeFlag}"/>
    </xsl:template>

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


有什么想法吗?

此XSLT 1.0样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="employee">
    <xsl:copy>
      <xsl:if test="activeFlag">
        <xsl:attribute name="active"><xsl:value-of select="activeFlag"/></xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>      
    </xsl:copy>
  </xsl:template>

  <xsl:template match="activeFlag"/>

</xsl:stylesheet>

此简短而简单(无明确的条件指令)的转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="employee">
   <employee active="{activeFlag}">
     <xsl:apply-templates select="node()|@*"/>
   </employee>
 </xsl:template>

 <xsl:template match="activeFlag"/>
</xsl:stylesheet>
<employees>
    <employee>
        <employeeNumber>1234</employeeNumber>
        <startdate>01/02/2003</startdate>
        <activeFlag>true</activeFlag>
        <firstname>Erik</firstname>
        <address>
            <addressline1>123 Main</addressline1>
            <zip>07016</zip>
            <state>New Jersey</state>
            <city>My City</city>
        </address>
    </employee>
</employees>
<employees>
  <employee active="true">
    <employeeNumber>1234</employeeNumber>
    <startdate>01/02/2003</startdate>
    <firstname>Erik</firstname>
    <address>
      <addressline1>123 Main</addressline1>
      <zip>07016</zip>
      <state>New Jersey</state>
      <city>My City</city>
    </address>
  </employee>
</employees>
 <xsl:template match="employee">
   <employee active=
    "{concat(activeFlag,
             substring('false',
                       1 div not(activeFlag))
             )
      }">
     <xsl:apply-templates select="node()|@*"/>
   </employee>
 </xsl:template>

好的,非常感谢你们俩。。。还有一个后续问题(很抱歉没有包括在OP中)。我试图使用此方法将xmlns、xmlns:xsi和xsi:schemaLocation属性添加到employees标记中,但结果出错了。这些是特殊属性还是什么?@ErikSorensen:
xmlns=
不是属性——这是名称空间声明。如果名称空间uri是静态已知的(如果不是在XSLT 1.0中),那么创建名称空间声明没有任何问题,需要使用涉及xxx:node-set()扩展的技巧来创建动态名称空间。@ErikSorensen:很高兴我的回答对您有用。为什么不考虑接受它呢?
<employees>
    <employee>
        <employeeNumber>1234</employeeNumber>
        <startdate>01/02/2003</startdate>
        <activeFlag>true</activeFlag>
        <firstname>Erik</firstname>
        <address>
            <addressline1>123 Main</addressline1>
            <zip>07016</zip>
            <state>New Jersey</state>
            <city>My City</city>
        </address>
    </employee>
</employees>
<employees>
  <employee active="true">
    <employeeNumber>1234</employeeNumber>
    <startdate>01/02/2003</startdate>
    <firstname>Erik</firstname>
    <address>
      <addressline1>123 Main</addressline1>
      <zip>07016</zip>
      <state>New Jersey</state>
      <city>My City</city>
    </address>
  </employee>
</employees>
 <xsl:template match="employee">
   <employee active=
    "{concat(activeFlag,
             substring('false',
                       1 div not(activeFlag))
             )
      }">
     <xsl:apply-templates select="node()|@*"/>
   </employee>
 </xsl:template>