我有一个包含XML值的变量
<xsl:variable name="resp">
<TABLE>
<news_id>39</news_id>
<news_title>Strada – TSO</news_title>
<news_content>Extending the support to IFA’s family in form of income security by paying last drawn trail paid to IFA before the demise.</news_content>
<news_type>1</news_type>
<doc_url>upload/docs/12345.pdf</doc_url>
<doc_size>185859</doc_size>
<news_date>2014-09-19 12:54:11.0</news_date>
<is_enabled>1</is_enabled>
</TABLE>
<xsl:variable>
如何解析此变量中的值。请帮忙。
答案 0 :(得分:0)
我要拥有这个结构,你这样做:
<?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="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:variable name="resp">
<TABLE>
<news_id>39</news_id>
<news_title>Strada – TSO</news_title>
<news_content>Extending the support to IFA’s family in form of income security by paying last drawn trail paid to IFA before the demise.</news_content>
<news_type>1</news_type>
<doc_url>upload/docs/12345.pdf</doc_url>
<doc_size>185859</doc_size>
<news_date>2014-09-19 12:54:11.0</news_date>
<is_enabled>1</is_enabled>
</TABLE>
</xsl:variable>
<!-- using copy-of to get the structure of the XML -->
<xsl:copy-of select="$resp" />
</xsl:template>
</xsl:stylesheet>
如果您想要变量中的文本,请执行以下操作:
<?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="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:variable name="resp">
<TABLE>
<news_id>39</news_id>
<news_title>Strada – TSO</news_title>
<news_content>Extending the support to IFA’s family in form of income security by paying last drawn trail paid to IFA before the demise.</news_content>
<news_type>1</news_type>
<doc_url>upload/docs/12345.pdf</doc_url>
<doc_size>185859</doc_size>
<news_date>2014-09-19 12:54:11.0</news_date>
<is_enabled>1</is_enabled>
</TABLE>
</xsl:variable>
<variable>
<!-- using value-of to get the values of the XML inserting it into variable element -->
<xsl:value-of select="$resp"/>
</variable>
</xsl:template>
</xsl:stylesheet>
如果您想从变量中的元素中获取不同的值,请执行以下操作:
<?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="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:variable name="resp">
<TABLE>
<news_id>39</news_id>
<news_title>Strada – TSO</news_title>
<news_content>Extending the support to IFA’s family in form of income security by paying last drawn trail paid to IFA before the demise.</news_content>
<news_type>1</news_type>
<doc_url>upload/docs/12345.pdf</doc_url>
<doc_size>185859</doc_size>
<news_date>2014-09-19 12:54:11.0</news_date>
<is_enabled>1</is_enabled>
</TABLE>
</xsl:variable>
<!-- If you want to get the value of news_id inside the variable you do this -->
<newsId>
<xsl:value-of select="$resp/TABLE/news_id"/>
</newsId>
<!-- Below is a shorter xpath-syntax -->
<newsTitle>
<xsl:value-of select="$resp//news_title"/>
</newsTitle>
</xsl:template>
</xsl:stylesheet>
希望这有帮助。