Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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转换的查询_Xml_Xslt - Fatal编程技术网

关于XML到XML转换的查询

关于XML到XML转换的查询,xml,xslt,Xml,Xslt,有两种方法可以将图形放置在输入xml文件中(注意:输入的基本形式): 1.对于1图: <xref id="F2">Figure 2</xref> <xref id="F5">Figures 5</xref>-<xref id="F8">8</xref> 图2 2.对于超过1个数字: <xref id="F2">Figure 2</xref> <xref id="F5">Figure

有两种方法可以将图形放置在输入xml文件中(注意:输入的基本形式):

1.对于1图:

<xref id="F2">Figure 2</xref>
<xref id="F5">Figures 5</xref>-<xref id="F8">8</xref>
图2
2.对于超过1个数字:

<xref id="F2">Figure 2</xref>
<xref id="F5">Figures 5</xref>-<xref id="F8">8</xref>
图5-8
如上所示,缺少外部参照ID 6和7

现在在我的输出xml文件中,第一个文件必须保持原样,但第二个文件必须更改为以下内容:

<xref id="F5 F6 F7 F8">Figures 5-8<xref>
图5-8

转换代码必须是通用的。不知道这是否可能。欢迎提供任何帮助或建议。谢谢。

好的,这是一个XSLT,使用如图所示的XML测试,使用saxon 9 he。它现在基于作为父对象,但对于任何包含外部参照的对象,这都是可以修复的

$ java -cp /extra/saxon/saxon9he.jar net.sf.saxon.Transform -s:fig.xml -xsl:fig.xsl
fig.xml:

<data>
  <p>
    <xref id="2">Figure 2</xref>
    <xref id="5">Figures 5</xref>-<xref id="8">8</xref>
    <xref id="10">Figure 10</xref>
    <xref id="15">Figures 15</xref>-<xref id="18">18</xref>
  </p>
</data>


图2
图5-8
图10
图15-18

根据修订/详细规范第三次修改——图xsl:

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xy="/x/y"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                exclude-result-prefixes="xy xs">
<xsl:output method="xml" indent="yes" />

<xsl:template match="*">  
  <xsl:copy>
    <xsl:apply-templates select="*"/>
  </xsl:copy>
</xsl:template>

<xsl:function name="xy:preF" as="xs:string*">
  <xsl:param name = "nums" as="xs:integer*"/>
  <xsl:for-each select="$nums">
    <xsl:value-of select="concat('F',.)"/>
  </xsl:for-each>
</xsl:function>

<xsl:template match="xref[starts-with(text(), 'Figure ')]">  
  <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="xref[starts-with(text(), 'Figures ')]">
  <xsl:variable name="a" as="xs:integer" select="substring( @id, 2 ) cast as xs:integer"/>
  <xsl:variable name="b" as="xs:integer" select="substring( following-sibling:: [name()='xref'][1]/@id, 2 ) cast as xs:integer"/>
  <xsl:variable name="Fab" as="xs:string*" select="xy:preF($a to $b)"/>
  <xref>
    <xsl:attribute name="id">
      <xsl:value-of select="$Fab"/>
    </xsl:attribute>
    <xsl:value-of select="concat('Figures ',$a,'-',$b)"/>
  </xref>
</xsl:template>

<xsl:template match="xref[not( starts-with(text(), 'Figure'))]">
</xsl:template>

</xsl:stylesheet>

以下样式使用XSLT 2.0版:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

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

    <xsl:template match="text()[.='-'][preceding-sibling::*[1][name()='xref']][following-sibling::*[1][name()='xref']]">
        <xsl:variable name="lower" select="preceding-sibling::*[1][name()='xref']/@id"/>
        <xsl:variable name="upper" select="following-sibling::*[1][name()='xref']/@id"/>
        <xref>
            <xsl:attribute name="id">
                <xsl:for-each select="(xs:integer($lower) to xs:integer($upper))">
                    <xsl:if test="position() &gt; 1">
                        <xsl:text> </xsl:text>
                    </xsl:if>
                    <xsl:value-of select="concat('F', .)"/>
                </xsl:for-each>
            </xsl:attribute>
            <xsl:value-of select="preceding-sibling::*[1][name()='xref']"/>
            <xsl:value-of select="."/>
            <xsl:value-of select="following-sibling::*[1][name()='xref']"/>
        </xref>
    </xsl:template>

    <xsl:template match="xref[following-sibling::text()[.='-']]|xref[preceding-sibling::text()[.='-']]"/>
</xsl:stylesheet>

当应用于输入时,如

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <p>
        <xref id="2">Figure 2</xref>
    </p>
    <p>
        <xref id="5">Figures 5</xref>-<xref id="8">8</xref>
    </p>
</data>


图2

图5-8

输出为:

<?xml version="1.0" encoding="UTF-8"?>
<data>
   <p>
      <xref id="2">Figure 2</xref>
   </p>
   <p>
      <xref id="F5 F6 F7 F8">Figures 5-8</xref>
   </p>
</data>


图2

图5-8


您必须为元素指定父元素。第二种形式是否总是一对,中间有一个连字符(-)作为文本节点浮动?“Figure”或“Figures”是可变的还是固定的?父元素是
p
标记。对于第二种形式,连字符将始终位于如上所示的同一位置,
Figures
变量对于多个
xref
s是固定的。@laune,父元素不一定是
p
标记。但是在我目前的情况下是这样的。兄弟,这很好,但是如果父母不是
p
?如果输入是这样的:
图3…一些文本..图5-8

嗯,我在问这个问题。所以“数字”中的“s”是唯一表明另一个数字即将到来的指标?当然,给我点时间,我现在正在开会。它在工作。嘿,我已经更新了输入。我发现了一个带有前缀F的@id的示例输入。
$a到$b
现在将如何工作?很抱歉给您添麻烦。这很好用。嘿,我已经更新了输入。我发现了一个带有前缀F的@id的示例输入。
(xs:integer($lower)到xs:integer($upper))
现在将如何工作?很抱歉给您添麻烦。@JoelM.Lamsen-Yikes,我一定是在尝试修补我的样式表时弄乱了您的样式表。很抱歉