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
Json 需要使用XSLT使用文件夹名和标题创建文件名_Json_Xml_Xslt - Fatal编程技术网

Json 需要使用XSLT使用文件夹名和标题创建文件名

Json 需要使用XSLT使用文件夹名和标题创建文件名,json,xml,xslt,Json,Xml,Xslt,我想通过使用文件夹名和带有hypen符号的文件名来创建链接。如果我的文件夹名包含items/item.xsl 我的输入XML是: <Settings> <code>MGT</code> <url>http://tneb.com</url> </Settings> <page/> <counter> <enabled>true</enabled> <text>Mana

我想通过使用文件夹名和带有hypen符号的文件名来创建链接。如果我的文件夹名包含items/item.xsl

我的输入XML是:

<Settings>
<code>MGT</code>
<url>http://tneb.com</url>
</Settings>
<page/>
<counter>
<enabled>true</enabled>
<text>Management Plan</text>
</counter>
在文件夹名(/items)中用作(item.XSL)的XSL I:

但我需要在设置和计数器文件夹之间创建一个pdf链接,如下所示:

"Settings": {

"code": "MGT",

"url": "http://tneb.com",

},
pdf: 'files/items-Management-Plan.pdf',

"counter": {

"enabled": "true",

"text": "Management Plan ", 

},
我需要创建pdf链接,其中,items表示文件夹名称,Management Plan表示输入文件中的文本名称。在文本名称的空格之间,输出中填充了hypen符号


请对此提出建议。提前感谢。

您应该添加一个根模板:

<xsl:template match="/">
    <xsl:variable name="filename" select="replace(counter/text, ' ', '-')" />

    <xsl:apply-templates select="Settings" />

    <!-- The new PDF line -->
    <xsl:text>"pdf": </xsl:text>
    <xsl:value-of select="concat( '&quot;', 'files/items-', $filename, '.pdf', '&quot;', ',&#xA;' )" />       

    <xsl:apply-templates select="Counter" />
</xsl:template>
<xsl:template match="Settings">
  "Settings": {
    <xsl:apply-templates/>
  },

  "pdf:" "<xsl:value-of select="concat('files/',$folder,'-',$PDF-name-part,'.pdf')"/>"

</xsl:template>

“pdf”:

这将在设置和计数器输出之间添加所需的行。

获取样式表的URI作为变量:

<xsl:variable name="stylesheet-uri" as="xs:string" select="base-uri(document(''))"/>

值为/path/to/items/items.xsl

然后将其拆分为路径组件:

<xsl:variable name="uri-components" as="xs:string+" select="tokenize($stylesheet-uri,'/')">

值为,例如(“路径”、“到”、“项”、“items.xsl”)

文件夹名称是最后的第二个组件:

<xsl:variable name="folder" as="xs:string" select="$uri-components[count($uri-components) - 1]"/>

PDF名称的第二部分是文本元素的值,其中空格字符替换为连字符:

<xsl:variable name="PDF-name-part" as="xs:string" select="translate(//text/text(), ' ', '-')"/>

最后,将这些位放在设置模板的末尾:

<xsl:template match="/">
    <xsl:variable name="filename" select="replace(counter/text, ' ', '-')" />

    <xsl:apply-templates select="Settings" />

    <!-- The new PDF line -->
    <xsl:text>"pdf": </xsl:text>
    <xsl:value-of select="concat( '&quot;', 'files/items-', $filename, '.pdf', '&quot;', ',&#xA;' )" />       

    <xsl:apply-templates select="Counter" />
</xsl:template>
<xsl:template match="Settings">
  "Settings": {
    <xsl:apply-templates/>
  },

  "pdf:" "<xsl:value-of select="concat('files/',$folder,'-',$PDF-name-part,'.pdf')"/>"

</xsl:template>

“设置”:{
},
“pdf:”

您的第一个代码段具有不匹配的
,因此它不是XML。第二个“JSON”输出有一个不带引号的
pdf
属性名,因此它不是JSON。至于创建JSON,您已经在样式表上使用了
version=“3.0”
,如果您真的使用了该版本,那么有更好的方法来构建JSON,因为XSLT/XPath 3.0/3.1有映射和数组,可以将它们序列化为JSON。现在我编辑了@MartinHonnen。除了json输出之外,使用XSLT进行正常转换是否可以做到这一点?请考虑向我们显示包含
设置
计数器
元素的父元素。为此,您可以编写一个模板,例如,
,“pdf”:“files/items Management Plan.pdf”,
。谢谢@somedumby。