首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于XML到XML转换的查询

关于XML到XML转换的查询
EN

Stack Overflow用户
提问于 2014-04-21 14:01:38
回答 2查看 51关注 0票数 0

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

1.图1:

代码语言:javascript
复制
<xref id="F2">Figure 2</xref>

2.对于1个以上的图:

代码语言:javascript
复制
<xref id="F5">Figures 5</xref>-<xref id="F8">8</xref>

如上所述,缺少外部参照ids 6和7。

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

代码语言:javascript
复制
<xref id="F5 F6 F7 F8">Figures 5-8<xref>

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

EN

回答 2

Stack Overflow用户

发布于 2014-04-21 14:40:15

好的,这是一个XSLT,使用saxon 9He使用XML进行了测试,如下所示。它现在的基础是

作为父对象,但这可以针对包含外部参照的任何内容进行修复。

代码语言:javascript
复制
$ java -cp /extra/saxon/saxon9he.jar net.sf.saxon.Transform -s:fig.xml -xsl:fig.xsl

fig.xml:

代码语言:javascript
复制
<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>

根据修订/详细规格修改了第三次 -- fig.xsl:

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2014-04-21 15:05:26

以下样式集使用XSLT 2.0版:

代码语言:javascript
复制
<?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>

当应用于像这样的输入

代码语言:javascript
复制
<?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>

输出为:

代码语言:javascript
复制
<?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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23191737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档