Selecting string containing apostroph with XPath - xslt-1.0

In order to generate CSS via XSLT I am using the following Expression:
<xsl:param name="default-font-family" select="'Times New Roman'" />
This produces the string Times New Roman instead of 'Times New Roman' which is what I need for use in CSS. Trying to enclose the name of the font in &apos; or ' did not work:
<xsl:param name="default-font-family" select="'&apos;Times New Roman&apos;'" />
<xsl:param name="default-font-family" select="''Times New Roman''" />
Can somebody please help my finding a proper expression for this?
Many thanks in advance!

Either declare a variable containing and apostrophe use that, or enter text between opening and closing param tags. Examples as follows:
<xsl:variable name="apostrophe">
<xsl:text>'</xsl:text>
</xsl:variable>
<xsl:param name="default-font-family" select="concat($apostrophe, 'Times New Roman', $apostrophe)" />
Or simply:
<xsl:param name="default-font-family">
<xsl:text>'Times New Roman'</xsl:text>
</xsl:param>
Both when selected will give the result you require.

Use:
<xsl:param name="default-font-family" select='"&apos;Times New Roman&apos;"' />
This simple transformation outputs the parameter so defined:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="default-font-family" select='"&apos;Times New Roman&apos;"' />
<xsl:template match="/">
<xsl:value-of select="$default-font-family"/>
</xsl:template>
</xsl:stylesheet>
When applied on any XML document (not used), the wanted, correct result is produced:
'Times New Roman'

Related

BizTalk Map: xslt 1.0 sum "Cannot implicitly convert type 'string' to 'int'"

Get error:
Cannot implicitly convert type 'string' to 'int'
on this code:
<TotalInvoiceCost>
<xsl:value-of select="sum(//*[local-name()='InvoiceTotal'])" />
</TotalInvoiceCost>
Then tried this:
<TotalInvoiceCost>
<xsl:value-of select="sum(number(//*[local-name()='InvoiceTotal']))" />
</TotalInvoiceCost>
but get this error:
Argument 1 of function 'sum()' cannot be converted to a node-set.
Cut Down Sample Data with Structure (all values are numeric):
<TAR210 xmlns="demo">
<DummyHeaderGroup xmlns=""/>
<Invoice xmlns="">
<Level1>
<InvoiceTotal>1075</InvoiceTotal>
</Level1>
<Level1>
<InvoiceTotal>595</InvoiceTotal>
</Level1>
</Invoice>
</TAR210>
In http://www.xpathtester.com/xpath, this works fine:
sum(//*[local-name()="InvoiceTotal"])
Example of XSLT in context:
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var"
exclude-result-prefixes="msxsl var userCSharp" version="1.0"
xmlns:ns0="demo"
xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:template match="/">
<ns0:TAR210>
<DummyHeaderGroup>
<Level0>
<TotalInvoiceCost>
<xsl:value-of select="sum(//*[local-name()='InvoiceTotal'])" />
</TotalInvoiceCost>
</Level0>
</DummyHeaderGroup>
</ns0:TAR210>
</xsl:template>
</xsl:stylesheet>
Well, the real world program is typically more complex than what we post on the web when we have a problem; I tried to simplify. The error had nothing to do with the code I posted.
To me, it seems like the biggest issue is that the error from the "Test Map" in Visual Studio doesn't give any hint to the line number with the error.
<Level0>
<BatchNumberLeaveBlank></BatchNumberLeaveBlank>
<InvoiceSendDateCCYYMM>
<!-- Get current CCYYMM -->
<!-- Current-DateTime doesn't exist in XSLT 1.0, had to use C#
<xsl:value-of select="concat(substring(current-dateTime(),1,4),substring(current-dateTime(),6,2))"/>
-->
<xsl:value-of select="userCSharp:GetDateCCYYMM()"/>
</InvoiceSendDateCCYYMM>
<DefaultValueN>N</DefaultValueN>
<TotalInvoiceCost>...
Then I had the C# code below:
<msxsl:script language="C#" implements-prefix="userCSharp">
<![CDATA[ int lineCount = 0;
public int GetDateCCYYMM()
{
return DateTime.Now.ToString("yyyyMMdd");
}
]]>
</msxsl:script>
Obviously, the "public int" should have been "public string" above.
The only way I know how to tackle these problems is the "divide and conquer" method.
I made a copy of the XSLT, named it same with _Debug.xslt, changed the BizTalk map to use it instead, and started ripping out lines of code left and right until the error goes away. Even removing the call to the c# didn't remove the error, so it must have been running a .NET compile or syntax check on the code.

Expanding multi-line snippet adds extra line at the bottom

Is there any way to prevent the addition of an extra line below a multi-line snippet in VB.NET?
(hit tab key to expand snippet...)
I've double checked that the snippet itself does not have an extra line at the end. Also, this seems to be VB-specific.
Just close the ]] after the end of your snipped. The ¿end¿ part sets the caret at this position after inserting the snipped.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>tryt</Title>
<Author>[USERNAME]</Author>
<Description>
</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>tryt</Shortcut>
</Header>
<Snippet>
<Code Language="vb" Delimiter="¿"><![CDATA[Try
¿end¿
Catch ex As Exception
Throw New Exception(ex) 'or do some other stuff
End Try]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

Trouble passing XML text to RIA Services from my client

I am trying to pass the following XML text to RIA services in a query operation that returns a queryable sequence of entities in response:
<?xml version="1.0" encoding="utf-8"?>
<project>
<item type="Item" filetype="cabinet" category="EZ Workshop" name="EZW Panel Edge-Banded" height="24.000000" width="36.000000" depth="0.725000" quantity="1">
</item>
<item type="Item" filetype="cabinet" category="EZ Furniture" name="Entry Bench" height="19.000000" width="48.000000" depth="17.999999" quantity="1">
</item>
<item type="Item" filetype="cabinet" category="EZ Closet Euro Style" name="CSEKD Tall H3R 28-72W 12-24D" height="84.000000" width="54.000000" depth="19.999999" quantity="1">
</item>
<item type="Item" filetype="assembly" category="EZ Pro ManCave" name="EZ Corn Hole Game-Set" height="0" width="0" depth="0" quantity="1">
</item>
<item type="Item" filetype="assembly" category="EZ Office" name="EZ 30 Printer Stand" height="0" width="0" depth="0" quantity="1">
</item>
<item type="Item" filetype="assembly" category="Corporate Culture Pro" name="C-Table" height="0" width="0" depth="0" quantity="1">
</item>
</project>
This is the query operation:
[Query]
public IQueryable<ProjectItem> GetItemsFromImport(String a_strImportXml)
{
// Return empty sequence for now as a test.
return new ProjectItem[0].AsQueryable();
}
When I pass the full XML, I get that annoying "Not Found" exception, and the break-point in my operation is never hit. I'm using Visual Studio 2010's ASP.NET Development Server. When I get "Not Found" with that it portends bad stuff. The kicker is, when I pass an empty string, I get no exceptions at all, and the break-point is hit.
As you can see its not a tremendously long XML document. Is there some limit to the amount of data sent? Do I have to escape the document?
Thanks.
Edits:
I discovered that I only had to escape the structural characters ('<', '>', and '&') out of the document before I sent it. I'm using String.Replace to do it. Does anyone know if there is a better way to accomplish this. Something similar to Uri.EscapeDataString perhaps?
var strImportXml = a_xImport.ToString();
strImportXml = strImportXml.Replace("&", "&");
strImportXml = strImportXml.Replace("<", "<");
strImportXml = strImportXml.Replace(">", ">");
Ok, so I guess this is standing as the answer to my own question. I discovered that I only had to escape the structural characters ('<', '>', and '&') out of the document before I sent it. I'm using String.Replace to do it. Does anyone know if there is a better way to accomplish this. Something similar to Uri.EscapeDataString perhaps?
var strImportXml = a_xImport.ToString();
strImportXml = strImportXml.Replace("&", "&");
strImportXml = strImportXml.Replace("<", "<");
strImportXml = strImportXml.Replace(">", ">");
NOTE: I'd still like to know if there is a better way to do this.

LINQ-to-XML Selecting tags inside the root

I'm new to LINQ-to-XML and I'm trying to learn the query structure. I have the following XML:
<?xml version="1.0" encoding="utf-8"?>
<list>
<item>
<due>07 May 2012</due>
<name>Name</name>
<desc>Description</desc>
<colour>White</colour>
</item>
<item>
<due>12 May 2012</due>
<name>Name2</name>
<desc>Desc2</desc>
<colour>White</colour>
</item>
</list>
And I'm trying to delete <item> elements with a query:
Dim DeleteItems = From e In Root.Elements("list") Where e.Element("name").Value = Text
However it isn't working. I think it's trying to select tags called <item> in the file's root, however it needs to be selecting <item> tags in the <list> element - which I don't know how to do.
I assume that Root is you XDocument object name.
Change Elements do Descendants and it should work then.
Dim DeleteItems = From e In Root.Descendants("item") Where e.Element("name").Value = Text

How do you automatically tab ALL the selected text when using a SurroundsWith SnippetType?

Below is my SQL snippet that is used to surround existing SQL. After highlighting some SQL and inserting this snippet, I want ALL the selected text to be indented by a tab or several spaces. My snippet currently will indent just the first line of highlighted code.
How can I make is indent all the selected text?
Here is my SQL snippet.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Insert a comment header for SQL</Title>
<Shortcut>header</Shortcut>
<Description>SQL Snippet will add comment area for header to break up code</Description>
<Author>Mike Adams</Author>
<SnippetTypes>
<SnippetType>SurroundsWith</SnippetType>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>headerText</ID>
<ToolTip>Header Text</ToolTip>
<Default></Default>
<Function></Function>
</Literal>
</Declarations>
<Code Language="sql">
<![CDATA[
-- $headerText$
---------------------------------------------------------
$selected$
$end$
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>