Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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文件添加或修改节点?_Xml_Xslt - Fatal编程技术网

如何基于另一个XML文件添加或修改节点?

如何基于另一个XML文件添加或修改节点?,xml,xslt,Xml,Xslt,我有两个文件(文件1): 1234 1. X99 30/10/1985 123456|5 1. 100 4. 31 01/02/1999 654321|1 2. 2. 654321|2 6. 1. 及(文件二): 123456|501 111112|131 对于文件2中的owninst,我需要更新文件1中的模式。如果File1中没有模式,那么我需要在ELQ节点之后添加一个模式 我确信这在XML样式表中并不困难,但似乎做不到 编辑2015年10月5日11:03 因此,多亏了@BKI,XS

我有两个文件(文件1):


1234
1.
X99
30/10/1985
123456|5
1.
100
4.
31
01/02/1999
654321|1
2.
2.
654321|2
6.
1.
及(文件二):


123456|501
111112|131
对于文件2中的owninst,我需要更新文件1中的模式。如果File1中没有模式,那么我需要在ELQ节点之后添加一个模式

我确信这在XML样式表中并不困难,但似乎做不到


编辑2015年10月5日11:03

因此,多亏了@BKI,XSLT现在在缺失的地方插入了模式,这很好。我已设法调整文件以进行解析。下一个问题是,它删除了实例标记,因此所有内容都向上移动到学生级别。我试过摆弄它,但不能让它不这么做。有人能帮忙吗?这是我现在拥有的代码:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

<xsl:key name="studentRefs" match="STUDENT" use="OWNINST"/>
<xsl:variable name="studentRefs" select="document('MCDATE - students to amend.xml')"/>

<xsl:template match="Instance">
    <xsl:apply-templates select="*">
        <xsl:with-param name="id" select="OWNINST"/>
    </xsl:apply-templates>
    <xsl:if test="count(MCDATE)= 0">
        <xsl:call-template name="checkMcdate">
            <xsl:with-param name="id" select="OWNINST"/>
        </xsl:call-template>
     </xsl:if> 
</xsl:template> 

        <xsl:template match="MCDATE">
            <xsl:param name="id"/>
            <xsl:call-template name="checkMcdate">
                <xsl:with-param name="id" select="$id"/>
                <xsl:with-param name="default" select="."/>
            </xsl:call-template>
         </xsl:template>

            <xsl:template name="checkMcdate">
                <xsl:param name="id"/>
                <xsl:param name="default" select="''"/>
                <xsl:for-each select="$studentRefs/studentstoamend">
                    <xsl:choose>
                        <xsl:when test="count(key('studentRefs', $id)) = 1">
                            <xsl:copy-of select="key('studentRefs', $id)/MCDATE"/>
                        </xsl:when>
                        <xsl:when test="not($default='')">
                            <MCDATE><xsl:value-of select="$default"/></MCDATE>
                        </xsl:when>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:template>

</xsl:stylesheet>

这应该让您开始学习。没有解析。它至少会让你了解一些概念

<xsl:key name="studentRefs" match="STUDENT" use="OWNINST"/>
<xsl:variable name="studentRefs" select="document('studentRefs.xml')"/>

<xsl:template match="instance">
   <xsl:apply-templates select="*">
      <xsl:with-param name="id" select="OWNINST"/>
   </xsl:apply-templates>
   <xsl:if test="count(MODE)= 0">
   <xsl:call-template name="checkMode">
      <xsl:with-param name="id" select="OWNINST"/>
   </xsl:call-template>
...

<xsl:template match="MODE">
   <xsl:param name="id"/>
   <xsl:call-template name="checkMode">
      <xsl:with-param name="id" select="$id"/>
      <xsl:with-param name="default" select="."/>
   </xsl:call-template>

<xsl:template name="checkMode">
  <xsl:param name="id"/>
  <xsl:param name="default" select="''"/>
  <xsl:for-each select="$studentRefs/studentstoamend">
    <xsl:choose>
     <xsl:when test="count(key('studentRefs', $id)) = 1)>
        <xsl:copy-of select="key('studentRefs', $id)/MODE"/>
     </xsl:when>
     <xsl:when test="not($default='')">
      <MODE><xsl:value-of select="$default"/></MODE>
     </xsl:when>
    </xsl:choose>
  </xsl:for-each>
 </xsl:template>

...

说得好,迈克尔道歉!“如果File1中没有模式,那么我需要在ELQ节点后添加一个。”当File2中没有匹配的实例时,这个添加的节点应该具有什么值?非常感谢,但它不起作用,正如您所说,它没有被解析,我已经尝试过,但无法运行。它显示与元素类型关联的属性“test”的值“xsl:when”不能包含‘这是它有问题的位’哦,它可以工作,只是它删除了实例标记,并将其中的所有内容都放在学生级别
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

<xsl:key name="studentRefs" match="STUDENT" use="OWNINST"/>
<xsl:variable name="studentRefs" select="document('MCDATE - students to amend.xml')"/>

<xsl:template match="Instance">
    <xsl:apply-templates select="*">
        <xsl:with-param name="id" select="OWNINST"/>
    </xsl:apply-templates>
    <xsl:if test="count(MCDATE)= 0">
        <xsl:call-template name="checkMcdate">
            <xsl:with-param name="id" select="OWNINST"/>
        </xsl:call-template>
     </xsl:if> 
</xsl:template> 

        <xsl:template match="MCDATE">
            <xsl:param name="id"/>
            <xsl:call-template name="checkMcdate">
                <xsl:with-param name="id" select="$id"/>
                <xsl:with-param name="default" select="."/>
            </xsl:call-template>
         </xsl:template>

            <xsl:template name="checkMcdate">
                <xsl:param name="id"/>
                <xsl:param name="default" select="''"/>
                <xsl:for-each select="$studentRefs/studentstoamend">
                    <xsl:choose>
                        <xsl:when test="count(key('studentRefs', $id)) = 1">
                            <xsl:copy-of select="key('studentRefs', $id)/MCDATE"/>
                        </xsl:when>
                        <xsl:when test="not($default='')">
                            <MCDATE><xsl:value-of select="$default"/></MCDATE>
                        </xsl:when>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:template>

</xsl:stylesheet>
<xsl:key name="studentRefs" match="STUDENT" use="OWNINST"/>
<xsl:variable name="studentRefs" select="document('studentRefs.xml')"/>

<xsl:template match="instance">
   <xsl:apply-templates select="*">
      <xsl:with-param name="id" select="OWNINST"/>
   </xsl:apply-templates>
   <xsl:if test="count(MODE)= 0">
   <xsl:call-template name="checkMode">
      <xsl:with-param name="id" select="OWNINST"/>
   </xsl:call-template>
...

<xsl:template match="MODE">
   <xsl:param name="id"/>
   <xsl:call-template name="checkMode">
      <xsl:with-param name="id" select="$id"/>
      <xsl:with-param name="default" select="."/>
   </xsl:call-template>

<xsl:template name="checkMode">
  <xsl:param name="id"/>
  <xsl:param name="default" select="''"/>
  <xsl:for-each select="$studentRefs/studentstoamend">
    <xsl:choose>
     <xsl:when test="count(key('studentRefs', $id)) = 1)>
        <xsl:copy-of select="key('studentRefs', $id)/MODE"/>
     </xsl:when>
     <xsl:when test="not($default='')">
      <MODE><xsl:value-of select="$default"/></MODE>
     </xsl:when>
    </xsl:choose>
  </xsl:for-each>
 </xsl:template>