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 用XSLT替换某些属性值_Xml_Xslt - Fatal编程技术网

Xml 用XSLT替换某些属性值

Xml 用XSLT替换某些属性值,xml,xslt,Xml,Xslt,在互联网上搜索了一段时间后,我找不到这个问题的答案。我的知识和经验都是新手 我的xml文件: <figuregroup id="1408-46-00-4506"> <figure id="1408-46-00-4507" href="http://A_Internet_Location/Images/img3.tif" height="5.098

在互联网上搜索了一段时间后,我找不到这个问题的答案。我的知识和经验都是新手

我的xml文件:

<figuregroup id="1408-46-00-4506">
                 <figure id="1408-46-00-4507"
                         href="http://A_Internet_Location/Images/img3.tif"
                         height="5.098in"
                         width="2.798in"
                         align="acenter"
                         placement="break"
                         orient="port"/>
                 <title id="Pt2Ch3-1408-46-00-4508">fig title</title>
</figuregroup> 

无花果标题
我希望xslt处理后的xml文件如下所示:

<figuregroup id="1408-46-00-4506">
                     <figure id="1408-46-00-4507"
                             href="img3.tif"
                             height="5.098in"
                             width="2.798in"
                             align="acenter"
                             placement="break"
                             orient="port"/>
                     <title id="Pt2Ch3-1408-46-00-4508">fig title</title>
</figuregroup> 

无花果标题
唯一的变化是href属性减少为img3.tif

我尝试过的是:

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

   <xsl:template match="figure[contains(@href, 'http://A_Internet_Location/Images/')]">


    <xsl:copy>
        <xsl:choose>
            <xsl:when test="contains(@href, 'http://A_Internet_Location/Images/')">
        <xsl:call-template name="remove">
             <xsl:with-param name="value" select="@href"/>
        </xsl:call-template> 
            </xsl:when>
        </xsl:choose>
        <xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template> 

    <xsl:template name="remove">
    <xsl:param name="value"/>
    <xsl:value-of select="concat(substring-before($value, 'http://A_Internet_Location/Images/'), substring-after($value, 'http://A_Internet_Location/Images/'))"/>
</xsl:template>

这一切都是正确的,但并没有减少href属性。
非常感谢所有帮助

如果属性值中有未知数量的
/
需要清除,请执行以下操作。如果样式表同时适用于XSLT1.0和XSLT2.0,那么定义命名模板是一个好主意

命名模板
递归子字符串
具有参数
$string
$anchor
。它输出
$string
中上次出现
$anchor
之后的
$string
子字符串

样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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

    <xsl:template match="figure/@href">
        <xsl:attribute name="href">
            <xsl:call-template name="recursive-substring">
                <xsl:with-param name="anchor" select="'/'"/>
                <xsl:with-param name="string" select="."/>
            </xsl:call-template>
        </xsl:attribute>
    </xsl:template>

    <xsl:template name="recursive-substring">
        <xsl:param name="anchor"/>
        <xsl:param name="string"/>

        <xsl:choose>
            <xsl:when test="contains($string,$anchor)">
                <xsl:call-template name="recursive-substring">
                    <xsl:with-param name="anchor" select="'/'"/>
                    <xsl:with-param name="string" select="substring-after($string,$anchor)"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$string"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<figuregroup id="1408-46-00-4506">
    <figure id="1408-46-00-4507" href="img3.tif" height="5.098in" width="2.798in" align="acenter" placement="break" orient="port"/>
    <title id="Pt2Ch3-1408-46-00-4508">fig title</title>
</figuregroup>

XML输出

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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

    <xsl:template match="figure/@href">
        <xsl:attribute name="href">
            <xsl:call-template name="recursive-substring">
                <xsl:with-param name="anchor" select="'/'"/>
                <xsl:with-param name="string" select="."/>
            </xsl:call-template>
        </xsl:attribute>
    </xsl:template>

    <xsl:template name="recursive-substring">
        <xsl:param name="anchor"/>
        <xsl:param name="string"/>

        <xsl:choose>
            <xsl:when test="contains($string,$anchor)">
                <xsl:call-template name="recursive-substring">
                    <xsl:with-param name="anchor" select="'/'"/>
                    <xsl:with-param name="string" select="substring-after($string,$anchor)"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$string"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<figuregroup id="1408-46-00-4506">
    <figure id="1408-46-00-4507" href="img3.tif" height="5.098in" width="2.798in" align="acenter" placement="break" orient="port"/>
    <title id="Pt2Ch3-1408-46-00-4508">fig title</title>
</figuregroup>

无花果标题

好的,好的,这不是xslt

#!/usr/bin/perl
use XML::DT;                       # attributes: %v; content: $c; tag: $q
use strict;

print dt($ARGV[0], figure => sub{ $v{href} =~ s!.*/!!; toxml } );
可能需要调整替换
s!。*/


免责声明:我是XML::DT

的作者之一,OP想要的是
XSLT
解决方案,而不是
Perl
解决方案。您的答案与此无关。您是否有任何理由编写
version=“2.0”
样式表,然后将
用作XSLT 2.0特有的功能?您的介绍中说,“如果样式表同时适用于XSLT1.0和XSLT2.0,那么定义命名模板是一个好主意”,但在这种情况下,我希望发布的代码能够显示一个适用于XSLT1.0的解决方案。或者,如果您真的想使用XSLT 2.0,那么您可以简单地使用
@MartinHonnen。这确实是一个错误,谢谢-我一有时间就会纠正它。@MartinHonnen谢谢您的评论,现在应该可以修复了。