如何在 'p' 元素中处理 'Underscore Line'
How to handle 'Underscore Line' within the 'p' Element
如何处理 p 标签中的下划线。这是我正在使用的代码。谁能帮我解决这个问题。
输入 XML: 和 XSLT 代码:
https://xsltfiddle.liberty-development.net/a9HjZX/1
输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<p>Number of Shares: _______</p>
<p>__________ Name of Company.</p>
<p>Number of Shares: _______Type of Stock: ________________</p>
<p>Certificate No: __________Company.</p>
</root>
预期输出:
<?xml version="1.0" encoding="UTF-8"?><root>
<p>Number of Shares: <inline-supplementary-material content-type="7_sld">_______</inline-supplementary-material></p>
<p><inline-supplementary-material content-type="10_sld">__________</inline-supplementary-material> Name of Company.</p>
<p>Number of Shares: <inline-supplementary-material content-type="7_sld">_______</inline-supplementary-material>Type of Stock: <inline-supplementary-material content-type="16_sld">________________</inline-supplementary-material></p>
<p>Certificate No: <inline-supplementary-material content-type="10_sld">__________</inline-supplementary-material> Company.</p>
</root>
XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="root">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template name="GetNoOfOccurance">
<xsl:param name="String"/>
<xsl:param name="SubString"/>
<xsl:param name="Counter" select="0" />
<xsl:variable name="sa" select="substring-after($String, $SubString)" />
<xsl:choose>
<xsl:when test="$sa != '' or contains($String, $SubString)">
<xsl:call-template name="GetNoOfOccurance">
<xsl:with-param name="String" select="$sa" />
<xsl:with-param name="SubString" select="$SubString" />
<xsl:with-param name="Counter" select="$Counter + 1" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$Counter" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="p[parent::*]">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="@*"/>
<xsl:variable name="num">
<xsl:call-template name="GetNoOfOccurance">
<xsl:with-param name="String" select="."/>
<xsl:with-param name="SubString" select="'_'"/>
</xsl:call-template>
</xsl:variable>
.............
</xsl:element>
</xsl:template>
我会使用 analyze-string
(在 XSLT 3 中)或 xsl:analyze-string
(在 XSLT 2 中),这是 XSLT 3 示例:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
expand-text="yes"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="p[matches(., '_+')]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="analyze-string(., '_+')" mode="match"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*:match" mode="match">
<inline-supplemental-material content-type="{string-length()}_sld">{.}</inline-supplemental-material>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/a9HjZX/2
如果您希望 p
具有您可能需要在文本节点上匹配的子元素:
<xsl:template match="p[matches(., '_+')]//text()">
<xsl:apply-templates select="analyze-string(., '_+')" mode="match"/>
</xsl:template>
<xsl:template match="*:match" mode="match">
<inline-supplemental-material content-type="{string-length()}_sld">{.}</inline-supplemental-material>
</xsl:template>
如何处理 p 标签中的下划线。这是我正在使用的代码。谁能帮我解决这个问题。
输入 XML: 和 XSLT 代码: https://xsltfiddle.liberty-development.net/a9HjZX/1
输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<p>Number of Shares: _______</p>
<p>__________ Name of Company.</p>
<p>Number of Shares: _______Type of Stock: ________________</p>
<p>Certificate No: __________Company.</p>
</root>
预期输出:
<?xml version="1.0" encoding="UTF-8"?><root>
<p>Number of Shares: <inline-supplementary-material content-type="7_sld">_______</inline-supplementary-material></p>
<p><inline-supplementary-material content-type="10_sld">__________</inline-supplementary-material> Name of Company.</p>
<p>Number of Shares: <inline-supplementary-material content-type="7_sld">_______</inline-supplementary-material>Type of Stock: <inline-supplementary-material content-type="16_sld">________________</inline-supplementary-material></p>
<p>Certificate No: <inline-supplementary-material content-type="10_sld">__________</inline-supplementary-material> Company.</p>
</root>
XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="root">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template name="GetNoOfOccurance">
<xsl:param name="String"/>
<xsl:param name="SubString"/>
<xsl:param name="Counter" select="0" />
<xsl:variable name="sa" select="substring-after($String, $SubString)" />
<xsl:choose>
<xsl:when test="$sa != '' or contains($String, $SubString)">
<xsl:call-template name="GetNoOfOccurance">
<xsl:with-param name="String" select="$sa" />
<xsl:with-param name="SubString" select="$SubString" />
<xsl:with-param name="Counter" select="$Counter + 1" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$Counter" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="p[parent::*]">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="@*"/>
<xsl:variable name="num">
<xsl:call-template name="GetNoOfOccurance">
<xsl:with-param name="String" select="."/>
<xsl:with-param name="SubString" select="'_'"/>
</xsl:call-template>
</xsl:variable>
.............
</xsl:element>
</xsl:template>
我会使用 analyze-string
(在 XSLT 3 中)或 xsl:analyze-string
(在 XSLT 2 中),这是 XSLT 3 示例:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
expand-text="yes"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="p[matches(., '_+')]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="analyze-string(., '_+')" mode="match"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*:match" mode="match">
<inline-supplemental-material content-type="{string-length()}_sld">{.}</inline-supplemental-material>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/a9HjZX/2
如果您希望 p
具有您可能需要在文本节点上匹配的子元素:
<xsl:template match="p[matches(., '_+')]//text()">
<xsl:apply-templates select="analyze-string(., '_+')" mode="match"/>
</xsl:template>
<xsl:template match="*:match" mode="match">
<inline-supplemental-material content-type="{string-length()}_sld">{.}</inline-supplemental-material>
</xsl:template>