我有"文字"单独的父标记的标记,因为该文本标记内容的某些时间将为空,那时我希望该标记具有单独的输出和内容元素" text"需要在单独的标签中:
我的输入XML是:
<introText>
<text/>
</introText>
<directionText>
<text>CLICK ON EACH CATEGORY TO GET STARTED, AND WHEN YOU ARE FINISHED, EVALUATE YOUR LISTS. WHICH LIST IS LONGER?</text>
</directionText>
XSL我用作:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://json.org/" xmlns:mf="http://example.com/mf" exclude-result-prefixes="#all">
<xsl:template match="introText">
"introText": {
<xsl:apply-templates/>
},
</xsl:template>
<xsl:template match="directionText">
"directionText": {
<xsl:apply-templates/>
}
</xsl:template>
<xsl:param name="length" as="xs:integer" select="100"/>
<xsl:param name="pattern" as="xs:string" select="concat('((.{1,', $length, '})( |$))')"/>
<xsl:param name="sep" as="xs:string" select="' + '"/>
<xsl:function name="mf:break" as="xs:string">
<xsl:param name="input" as="xs:string"/>
<xsl:variable name="result">
<xsl:analyze-string select="$input" regex="{$pattern}">
<xsl:matching-substring>
<xsl:value-of select="concat('"', regex-group(2), ' ', '"')"/>
<xsl:if test="position() ne last()">
<xsl:value-of select="$sep"/>
</xsl:if>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:sequence select="$result"/>
</xsl:function>
<xsl:template match="text">
"text": <xsl:sequence select="mf:break(normalize-space(string-join(node()/serialize(., $ser-params), '')))"/>,
</xsl:template>
</xsl:stylesheet>
我的输出为:
introText: {
"text": ""
},
directionText: {
"text": "CLICK ON EACH CATEGORY TO GET STARTED, AND WHEN YOU ARE" +
"FINISHED, EVALUATE YOUR LISTS. WHICH LIST IS LONGER?",
}
但我想要空元素&#34; text&#34;必须像下面的输出文件
预期输出文件:
introText: {
"text":' "" '
},
directionText: {
"text": "CLICK ON EACH CATEGORY TO GET STARTED, AND WHEN YOU ARE" +
"FINISHED, EVALUATE YOUR LISTS. WHICH LIST IS LONGER?",
}
额外的单引号需要覆盖双引号,如果该文本为空,同样很多&#34; text&#34;标签进入文章,所以我想写&#34;文本&#34;单独的模板。提前致谢
答案 0 :(得分:0)
如果你想分别处理一个空的<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<button type="button" id="mymodal" class="btn btn-primary btn-lg">
Launch demo modal
</button>
元素,只需添加一个新模板来匹配这样一个元素,如下所示:
text
<xsl:template match="text[not(normalize-space())]">
"text": ' "" ',
</xsl:template>
意味着它还会选择只包含空格的normalize-space()
个节点。