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

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 xsl:split并获取param的X位置_Xml_Xslt_Xpath_Split_Param - Fatal编程技术网

Xml xsl:split并获取param的X位置

Xml xsl:split并获取param的X位置,xml,xslt,xpath,split,param,Xml,Xslt,Xpath,Split,Param,xsl:我需要拆分并获取X位置参数,@class属性第二个参数: 输入XML: <root> <div class="zone zona_central ui-sortable"> <div class="region contenedor_3col ui-sortable"> <div style="position: relative; left: 0px; top: 0px;" class="dest

xsl:我需要拆分并获取X位置参数,@class属性第二个参数:

输入XML:

<root>
    <div class="zone zona_central ui-sortable">
        <div class="region contenedor_3col ui-sortable">
            <div style="position: relative; left: 0px; top: 0px;" class="destacado">
                <p class="id_contenido">567662</p>
                <p class="tipo_contenido">destacado</p>
                <p class="titulo">destacado: Home Actualidad ES</p>
            </div>
        </div>
    </div>
</root>

567662

德斯塔卡多

destacado:家庭实现

输出我需要的XML:

<zone type="zona_central">
    <region type="contenedor_3col">
        <destacado>
            <id_contenido>567662</id_contenido>
            <tipo_contenido>destacado</tipo_contenido>
            <titulo>destacado: Home Actualidad ES</titulo>
        </destacado>
    </region>
</zone>

567662
德斯塔卡多
destacado:家庭实现
我有一个xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">    
    <xsl:for-each select="div[contains(@class, 'zone')]">       
        <zone style="">
            <xsl:for-each select="div[contains(@class, 'region')]">
                <region style="">
                    <xsl:for-each select="div[contains(@class, 'destacado')]">
                        <destacado>                         
                            <id_contenido><xsl:value-of select="p[@class='id_contenido']"/></id_contenido>
                            <tipo_contenido><xsl:value-of select="p[@class='tipo_contenido']"/></tipo_contenido>
                            <titulo><xsl:value-of select="p[@class='titulo']"/></titulo>                            
                        </destacado>
                    </xsl:for-each>
                </region>
            </xsl:for-each>                             
        </zone>
    </xsl:for-each>
</xsl:template> 
</xsl:stylesheet>

使用以前的XSL输出XML,我不知道如何获得CLASS属性的第二个参数:(


567662
德斯塔卡多
destacado:家庭实现
解决方案:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">    
    <xsl:for-each select="div[contains(@class, 'zone')]">       
        <xsl:element name="zone">
            <xsl:attribute name="type">
                <xsl:value-of select="substring-before(substring-after(@class, ' '), ' ')"/>
            </xsl:attribute>            
            <xsl:for-each select="div[contains(@class, 'region')]">
                <xsl:element name="region">
                    <xsl:attribute name="type">
                        <xsl:value-of select="substring-before(substring-after(@class, ' '), ' ')"/>
                    </xsl:attribute>

                    <xsl:for-each select="div[contains(@class, 'destacado')]">
                        <destacado>                         
                            <id_contenido><xsl:value-of select="p[@class='id_contenido']"/></id_contenido>
                            <tipo_contenido><xsl:value-of select="p[@class='tipo_contenido']"/></tipo_contenido>
                            <titulo><xsl:value-of select="p[@class='titulo']"/></titulo>                            
                        </destacado>
                    </xsl:for-each>
                </xsl:element>              
            </xsl:for-each>                             
        </xsl:element>
    </xsl:for-each>
</xsl:template> 
</xsl:stylesheet>

您可以使用
substring-before()
substring-after()
在空格处拆分类属性的值

substring-before(substring-after(@class, ' '), ' ')
将在
@class
中给出第二个标记。这假设您的标记由单个空格(而不是一般的“空白”)分隔。在代码中,您将把它放在属性值模板中:

<zone type="{substring-before(substring-after(@class, ' '), ' ')}">

再次返回第二个以空格分隔的标记,但分隔符的模式(“”)可以是任何regexp。

更通用的XSLT 1.0解决方案,带有模板

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

<xsl:template match="*[@class]">
  <xsl:variable name="name">
    <xsl:choose>
      <xsl:when test="contains(@class, ' ')">
        <xsl:value-of select="substring-before(@class, ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@class"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="type">
    <xsl:variable name="tail" select="substring-after(@class, ' ')"/>
    <xsl:choose>
      <xsl:when test="contains($tail, ' ')">
        <xsl:value-of select="substring-before($tail, ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$tail"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:element name="{$name}">
    <xsl:if test="string($type)">
      <xsl:attribute name="type">
        <xsl:value-of select="$type"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

它可以工作,但我不知道如何将它放入attribute:facepalm:@ZiTAL:好的,我添加了一个示例来演示如何将它放入ATV中的XSLT中。我在发布之前编写了解决方案,tokenize对我不起作用,但第一个对,非常感谢,我将解决方案放在问题的底部:)@ZiTAL:请阅读如何。即使
class
属性包含前导或尾随空格、制表符、一行多个空格等,您也可以使用
normalize space
使此解决方案起作用。例如,如果
class
包含值
“此处奇怪的间距”
,则可以使用表达式
子字符串before(substring after)(normalize space(@class),“”),“”)
+1来提取“间距”,以实现通用性,并显示XSLT 2.0答案,即使OP当前正在使用XSLT 1.0。
tokenize(@class, ' ')[2]
<xsl:template match="root">
  <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="*[@class]">
  <xsl:variable name="name">
    <xsl:choose>
      <xsl:when test="contains(@class, ' ')">
        <xsl:value-of select="substring-before(@class, ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@class"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="type">
    <xsl:variable name="tail" select="substring-after(@class, ' ')"/>
    <xsl:choose>
      <xsl:when test="contains($tail, ' ')">
        <xsl:value-of select="substring-before($tail, ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$tail"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:element name="{$name}">
    <xsl:if test="string($type)">
      <xsl:attribute name="type">
        <xsl:value-of select="$type"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>
<xsl:template match="root">
  <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="*[@class]">
  <xsl:variable name="class" select="tokenize(@class, '\s')"/>
  <xsl:element name="{$class[1]}">
    <xsl:if test="count($class) > 1">
      <xsl:attribute name="type" select="$class[2]"/>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>