Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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,输出日期: <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1"> <product> <sku>SKU 11077</sku> <imgs> <image>https://testtest.com/_data/products/sku-11077sku-11077.jpg</ima

输出日期:

<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1">
<product>
<sku>SKU 11077</sku>
<imgs>
<image>https://testtest.com/_data/products/sku-11077sku-11077.jpg</image>
<image>https://testtest.com/_data/products/sku-11077sku-11077,1.jpg</image>
<image>https://testtest.com/_data/products/sku-11077sku-11077,2.jpg</image>
<image>https://testtest.com/_data/products/sku-11077sku-11077,3.jpg</image>
<image>https://testtest.com/_data/products/sku-11077sku-11077,4.jpg</image>
</imgs>
</product>
</products>
我尝试通过代码执行以下操作:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:g="http://base.google.com/ns/1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="products">
<xsl:element name="products">
  <xsl:for-each select="product">
    <xsl:element name="product">
         <xsl:element name="sku">
             <xsl:value-of select="sku"/>
         </xsl:element>
     <xsl:template match="imgs">
    <xsl:copy>
      <xsl:for-each select="image">
        <xsl:element name="image{position()}">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
                    
    </xsl:element>
  </xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

我收到问题:

但是在粘贴代码之后,我们得到:警告:XSLTProcessor::importStylesheet():编译错误:第107行/home/fs/domains/public_html/pub/line 18元素模板/home/fs/domains/public_html/vendor/firebear/importextort/Model/Output/Xslt.php

有人能帮我格式化吗?

编辑(不需要imgs元素)

我们只需将
xsl:apply templates
select=“imgs”
一起使用,以便下一个模板匹配将捕获您的imgs:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:g="http://base.google.com/ns/1.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="products">
    <xsl:element name="products">
      <xsl:for-each select="product">
        <xsl:element name="product">
          <xsl:element name="sku">
            <xsl:value-of select="sku"/>
          </xsl:element>
          <xsl:apply-templates select="imgs"/>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

  <xsl:template match="imgs">
    <xsl:for-each select="image">
      <xsl:element name="image{position()}">
        <xsl:value-of select="."/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

但是,当您仅更改这些图像元素时,可以使用以下通用复制模板使其更简单:

<?xml version="1.0" encoding="UTF-8"?>
<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="yes"/>

  <!-- Identity template : copy all nodes and attributes -->   
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="imgs">
    <xsl:for-each select="image">
      <xsl:element name="image{position()}">
        <xsl:value-of select="."/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

编辑(不需要imgs元素)

我们只需将
xsl:apply templates
select=“imgs”
一起使用,以便下一个模板匹配将捕获您的imgs:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:g="http://base.google.com/ns/1.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="products">
    <xsl:element name="products">
      <xsl:for-each select="product">
        <xsl:element name="product">
          <xsl:element name="sku">
            <xsl:value-of select="sku"/>
          </xsl:element>
          <xsl:apply-templates select="imgs"/>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

  <xsl:template match="imgs">
    <xsl:for-each select="image">
      <xsl:element name="image{position()}">
        <xsl:value-of select="."/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

但是,当您仅更改这些图像元素时,可以使用以下通用复制模板使其更简单:

<?xml version="1.0" encoding="UTF-8"?>
<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="yes"/>

  <!-- Identity template : copy all nodes and attributes -->   
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="imgs">
    <xsl:for-each select="image">
      <xsl:element name="image{position()}">
        <xsl:value-of select="."/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

模板不能嵌套

——根据您更改预期结果而编辑---

AFAICT,您想要的结果可以非常简单地通过以下方式产生:

XSLT1.0

<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="yes"/>

<xsl:template match="/products">
    <xsl:copy>
        <xsl:copy-of select="@version"/>
        <xsl:for-each select="product">
            <xsl:copy>
                <xsl:copy-of select="sku"/>
                <xsl:for-each select="imgs/image">
                    <xsl:element name="image{position()}">
                        <xsl:value-of select="."/>
                    </xsl:element>
                </xsl:for-each>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

或者,如果您愿意:

<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="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="imgs">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="image">
    <xsl:element name="image{position()}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>



注意:不要在满足条件的地方使用
xsl:element

模板不能嵌套

——根据您更改预期结果而编辑---

AFAICT,您想要的结果可以非常简单地通过以下方式产生:

XSLT1.0

<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="yes"/>

<xsl:template match="/products">
    <xsl:copy>
        <xsl:copy-of select="@version"/>
        <xsl:for-each select="product">
            <xsl:copy>
                <xsl:copy-of select="sku"/>
                <xsl:for-each select="imgs/image">
                    <xsl:element name="image{position()}">
                        <xsl:value-of select="."/>
                    </xsl:element>
                </xsl:for-each>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

或者,如果您愿意:

<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="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="imgs">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="image">
    <xsl:element name="image{position()}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>



注意:不要在a就足够的地方使用
xsl:element

是数字标签Hm,已经有一段时间了,但我不确定是否可以在模板中使用模板…是的,这是错误,我们正在尝试解决它。因为内容落后,下面的照片落后于标签是的,我很有信心您需要使用
,请尝试帮助我们回答并将此代码添加到我们的代码中好吗?然后我们尝试在我们的网站。是数字标签嗯,已经有一段时间了,但我不确定你是否可以有一个模板中的模板…是的,这是错误,我们正在努力解决它。因为内容落后,下面的照片落后于标签是的,我很有信心您需要使用
,请尝试帮助我们回答并将此代码添加到我们的代码中好吗?然后我们在我们的网站上试试。michael.hor257k,我感谢你的努力。我试过所有的方法。但我仍然无法获得照片,因为要下载这些照片,我需要生成这些照片,否则我恐怕你现在已经失去了我。你可以看到上面的代码产生了你问题中指定的确切结果(在我写这篇评论时你更改了它之前):我不知道你说的“下载这些照片”是什么意思。michael.hor257k,我感谢你的努力。我试过所有的方法。但我仍然无法获得照片,因为要下载这些照片,我需要生成这些照片,否则我恐怕你现在已经失去了我。你可以看到上面的代码产生了你问题中指定的确切结果(在我写这篇评论时你更改了它之前):我不知道你说的“下载这些照片”是什么意思。@Siebie Jongebloed我感谢你的努力。我试过所有的方法。但我仍然无法获取照片,因为要下载这些照片,我需要在我不知道如何生成html之前生成这些照片。又是新问题…@Siebie Jongebloed我感谢你的努力。我试过所有的方法。但我仍然无法获取照片,因为要下载这些照片,我需要在我不知道如何生成html之前生成这些照片。又是一个新问题。。。